I'm not actually sure how best to explain this. Basically I'm in the process of creating an Arkanoid clone type game where there are several bricks on the screen. A ball bounces around the screen bumping off walls and when it finally collides with the paddle (controlled by the user) it changes velocity and eventually hits one of the bricks that appears above. So far this portion of the game has been completed.
I have 9 different colored images/bricks named (yellow.png, red.png, blue.png, etc).
What I wanted to do for each level is change the position of the bricks depending on the following (as an example of one of the levels) might be the following:
The following is a representation of an 8x8 grid of bricks.
The "x" will be replaced with a number (as seen above) and then the number will be associated with a unique colored brick. The numbers range from 1 - 9.
I am wanting to loop through this array and dynamically fill the correct image in its place depending on the brickLevel1 array?
The following is the current loop I'm using to display an 8x8 grid with just ONE color, because that's all I'm capable of doing at the moment. I wasn't sure how to implement my idea properly and loop through and render each image in its correct place.
Code:
Code:
int x = 0, y = 0, brick_width = 39, brick_height = 23;
for (int i=0; i < 64; i++)
{
x = (brick_width+1)*floor(i%8);
y = (brick_height+1)*floor(i/8);
brick = (UIImageView *)[brickArray objectAtIndex:i];
brick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"yellow.png", i]]];
brick.transform = CGAffineTransformMakeTranslation(x, y);
if (CGRectIntersectsRect(ball.frame, brick.frame))
ballVelocity.y = -ballVelocity.y;
[self.view addSubview:brick];
}
I hope some of what I'm trying to achieve makes sense. If anyone could lead me in the right direction that would be greatly appreciated.
Well if you can get the value of each brick (using similar code to your x and y values (i%8, i/8)) You could use a switch statement to create the correct imaged brick. I don't know how exactly the code would look since I haven't used switch statements in Obj-C yet. So basically you need to do two things.
a) get the correct value for that brick (some people would use two for loops to do the same thing you are doing with just one)
b) use that value to assign the image to the view.
Well if you can get the value of each brick (using similar code to your x and y values (i%8, i/8)) You could use a switch statement to create the correct imaged brick. I don't know how exactly the code would look since I haven't used switch statements in Obj-C yet. So basically you need to do two things.
a) get the correct value for that brick (some people would use two for loops to do the same thing you are doing with just one)
b) use that value to assign the image to the view.
Yeah, see that's kind of the idea I was thinking of. But I'm not sure how to create such an array that looks like the following and loop through it as well:
I don't understand exactly what the brickArray is referring to. Code to create an array of int values looks like this (again, I think... very rusty on C and new to Obj-C)
Then you index into the array by using your i value in the same way you assigned the position.
Code:
int thisBrickValue = brickLayout[i%8][i/8];
Though you may have to switch the values if things start to look funky. Or it may be perfectly fine, just kinda strange to me because I'm used to row major ordered arrays and not column major ordered.
Using the value you get from the array you can then use a switch statement to create the correct image.
have you any solution for that , i am also developing a game in which i have to make array such as you asked. So if you have some solution than help me
help will be grateful
// store the colors
/*
say red=1
blue = 2, etc
*/
NSArray *colors = [NSArray arrayWithObjects: @"red",@"blue",@"green",@"orange",@"purple",@"white",nil];
your loop
NSString *name = [colors objectAtIndex: value from brickLayout ];
// create the name based on the index and the colors array
NSString *imgName = [NSString stringWithFormat: @"%@%d.png",name,your_array_index];
// assign your image
UIImage *img = [UIImage imageNamed:imgName];
end loop
i have a 15X8 board .i want to place some uiimageview objects at some position of the board depending on the level. previously i created objects like view1 ,view2 ......so on statically but it seems like so odd i want to create the using loop and want to place these in a array. Help me to come out of this.