Dear all,
Hi, I am a newbie of xcode and objective-c and I have a few questions regarding to the code sample of a game attached below. It is written in objective C, Xcode for iphone4 simulator. It is part of the code of 'ball bounce against brick" game. Instead of creating the image by IB, the code supposes to create (programmatically) 5 X 4 bricks using 4 different kinds of bricks pictures (bricktype1.png...). I have the bricks defined in .h file properly and method written in .m.
My questions are for the following code:
- (void)initializeBricks
{
brickTypes[0] = @"bricktype1.png";
brickTypes[1] = @"bricktype2.png";
brickTypes[2] = @"bricktype3.png";
brickTypes[3] = @"bricktype4.png";
int count = 0;
for (int y = 0; y < BRICKS_HEIGHT; y++)
{
for (int x = 0; x < BRICKS_WIDTH; x++)
{
UIImage *image = [ImageCache loadImage:brickTypes[count++ % 4]]; - Line1
bricks[x][y] = [[[UIImageView alloc] initWithImage:image] autorelease];
CGRect newFrame = bricks[x][y].frame;
newFrame.origin = CGPointMake(x * 64, (y * 40) + 50);
bricks[x][y].frame = newFrame;
[self.view addSubview:bricks[x][y]];
}
}
}
1) When it is compiled, error "ImageCache undeclared" in Line 1. But I have already added the png to the project. What is the problem and how to fix it? (If possible, please suggest code and explain what it does and where to put it.)
2) How does the following in Line 1 work? Does it assign the element (name of .png) of brickType to image?
brickTypes[count ++ % 4]
For instance, returns one of the file name bricktype1.png to the image object? If true, what is the max value of "count", ends at 5? (as X increments 5 times for each Y). But then "count" will exceed the max 'index value' of brickTypes which is 3!
3) In Line2, does the image object which is being allocated has a name and linked with the .png already at this line before it is assigned to brick[x][y]?
4) What do Line3 and Line5 do? Why newFrame on left in line3 but appears on right in Line5?
5) What does Line 4 do?