473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

class calling kinda problem

Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<5; i++)                // for loop for creating a background of tiles
  2. {
  3.   x=10; 
  4.  
  5.   Graphics g = canvas.getGraphics();           // call Tile to draw a tile
  6.   Tile[] tile = new Tile[10];                  // intialise tile
  7.   tile[i] = new Tile(1,x,y);             // a new tile for every i
  8.   tile[i].Draw(g);                       // draw it
  9.   x=x+50;                           //change x to move next square along    
  10. }


this is supposed to create a line along the X axis of squares that is in thge class Tile but it only creates 1 the for loop worksand in theory the x should go 10 60 110 an have at least 3 squares but i dont know why it doesnt work any help is greatly appreciated.
Dec 8 '09 #1
1 1470
chaarmann
785 Expert 512MB
You are initialising the tiles-array inside your loop, so you are initialising them 5 times! That makes no sense. You should put that line outside the loop and if you only want 5 squares (the counter "i" in the for-loop), you should set the array size to 5 and not 10!
Also methods start with lowercase, not uppercase, so you should use "Tile.draw()" instead of "Tile.Draw()".
Also x is reset every time to 10 at the beginning of the loop, but you want x to count up, so put it outside the loop! That's why you get all 5 tiles drawn exactly over each other, because the coordinates are the same in every loop.

Anyway, you don't need the array if you can forget the tile-object after drawing.
So what you want is probably following:
Expand|Select|Wrap|Line Numbers
  1. int x=10;
  2. for (int i=0; i<5; i++) // for loop for creating a background of tiles
  3. {
  4.    Graphics g = canvas.getGraphics(); // call Tile to draw a tile
  5.    Tile tile = new Tile(1, x, y); // a new tile for every i
  6.    tile.draw(g); // draw it
  7.    x += 50; // increase x-coordinate to move next square along 
  8. }
  9.  
By the way:
It is a very good programming style to make a comment after every line, as you did.
Dec 9 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: Steve | last post by:
Hello -- I've been struggling with this problem for over a day now. I'd like to know (without passing parameters) which class, and preferably, which method of that class has called my function....
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
2
by: macracan | last post by:
It has been discussed before, but I still can't find a solution and I have a need. So here goes: The problem: I'm writing a something to handle messages from XWindows. The idea is to have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.