Reputation: 25
I am building a basic clone of breakout using C# and XNA. All the code seems to be working well except when trying to create and draw the blocks to the screen.
In the LoadContent() method I used a for loop I tried to add 10 rectangles to a rectangle list (just for testing) with changing X and Y values.
int xChange = 0;
int yChange = 0;
//loop iterates 10 times as a test
for (int i = 0; i == 10; i++)
{
rectList.Add(new Rectangle(100+xChange, 200+ yChange, 40, 80));
xChange += 100;
yChange += 50;
}
Then in the draw method I used a foreach loop to draw each of these:
foreach (Rectangle rect in rectList)
{
spriteBatch.Draw(block, rect, Color.White);
}
When I run this code nothing draws and I'm not sure why. Full code can be found at http://pastebin.com/TNbzgUqn.
EDIT: By adding a rectangle manually (not in a loop) I got a rectangle to draw. This narrows it down to the for loop in which the rectangles are added to the list.
//The code below draws, the code above does not
Rectangle test1 = new Rectangle(200, 200, 40, 80);
rectList.Add(test1);
Upvotes: 0
Views: 2062
Reputation: 46
From MSDN:
By using a for loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to false.
Your for loop is saying: starting from 0, do the following while i is equal to 10. Which will always be false. You should instead use:
for(int i = 0; i <= 10; i++)
{
// your code here
}
Upvotes: 3