Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-17-2009, 08:16 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 42
Default 8x8 Grid from Array

I wanted to do something similar to the following thread:
http://www.iphonedevsdk.com/forum/ip...id-images.html

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:

brickArray *brickLevel1 = {
{0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,1,1},
{1,0,1,0,0,1,0,1},
{1,0,2,0,0,2,0,1},
{1,0,2,0,0,2,0,1},
{1,0,1,0,0,1,0,1},
{1,1,0,0,0,0,1,1},
{1,1,3,1,1,3,1,1}
};

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.

[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x]

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.
fulvio is offline   Reply With Quote
Old 03-17-2009, 08:49 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 29
Default

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.
rhfb is offline   Reply With Quote
Old 03-17-2009, 09:11 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 42
Default

Quote:
Originally Posted by rhfb View Post
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:

brickArray *brickLevel1 = {
{0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,1,1},
{1,0,1,0,0,1,0,1},
{1,0,2,0,0,2,0,1},
{1,0,2,0,0,2,0,1},
{1,0,1,0,0,1,0,1},
{1,1,0,0,0,0,1,1},
{1,1,3,1,1,3,1,1}
};

I'm sure I could figure out the switch statement logic to replace the 1 with blue.png and the 2 with yellow.png, etc.

I'm just not 100% on the way to create that array and loop through it properly? Any ideas?

Last edited by fulvio; 03-17-2009 at 11:29 PM.
fulvio is offline   Reply With Quote
Old 03-17-2009, 11:54 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 29
Default

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)

Code:
int brickLayout[8][8] = {
	{0,0,0,0,0,0,0,0},
	{1,1,0,0,0,0,1,1},
	{1,0,1,0,0,1,0,1},
	{1,0,2,0,0,2,0,1},
	{1,0,2,0,0,2,0,1},
	{1,0,1,0,0,1,0,1},
	{1,1,0,0,0,0,1,1},
	{1,1,3,1,1,3,1,1}
};
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.

Code:
switch(thisBrickValue) {
    case 1:
        brick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"yellow.png"]]];
        break;
    case 2:
        brick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"blue.png"]]];
        break;
//ECT ECT ECT
    default:
        NSLog(@"ERROR!!!");
}
Hope this code helps you to get it up and running, if not just reply and I'll try to help some more.
rhfb is offline   Reply With Quote
Old 03-18-2009, 03:24 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 35
Default need solution

hi,

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

thanx
antilantil is offline   Reply With Quote
Old 03-18-2009, 04:18 PM   #6 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
Default

perhaps something along the lines of:
Code:
// 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
Mike
orchiss is offline   Reply With Quote
Old 03-19-2009, 02:07 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 35
Default

hi

thanx for reply

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.
antilantil is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 452
13 members and 439 guests
Alexander_john, clerisysolutions, dapis, dre, Harolano, Hyde, leesdesjardins, Modecor, MozyMac, Objective Zero, rianneadams, RickSDK, samtakoy
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 157,849
Threads: 88,914
Posts: 379,294
Top Poster: BrianSlick (7,072)
Welcome to our newest member, Harolano
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0