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 10-08-2009, 05:40 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Default Creating A Grid

We need a grid for our game. How would you make a grid on... well in our case its a map. How would you make a grid on a map so objects like an image (most likely a Building) could be snapped on to the grid.

Can anyone tell us or mention a tutorial?
Thanks
KLB-Tronics is offline   Reply With Quote
Old 10-09-2009, 11:16 AM   #2 (permalink)
mox
Registered Member
 
mox's Avatar
 
Join Date: May 2009
Posts: 33
Default

Quote:
Originally Posted by KLB-Tronics View Post
We need a grid for our game. How would you make a grid on... well in our case its a map. How would you make a grid on a map so objects like an image (most likely a Building) could be snapped on to the grid.

Can anyone tell us or mention a tutorial?
Thanks
I made a Bomberman like game just as a proof of concept last week.

I have used a 2D int array

int myArray[width][height];

I was accessing it with myArray[x][y] to know the value of a specific case on my grid.

Here is a youtube video I did to show a friend
YouTube - 2009-10-04 - iPhone Bomberman development

I saw some example of people doing a calculation like that to get the position in the array: myArray[(yValue * Height) + xValue)]

I guess there are better ways to do it.. maybe more Object Oriented..

--Ben
mox is offline   Reply With Quote
Old 10-09-2009, 07:46 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Default

Quote:
Originally Posted by mox View Post
I made a Bomberman like game just as a proof of concept last week.

I have used a 2D int array

int myArray[width][height];

I was accessing it with myArray[x][y] to know the value of a specific case on my grid.

Here is a youtube video I did to show a friend
YouTube - 2009-10-04 - iPhone Bomberman development

I saw some example of people doing a calculation like that to get the position in the array: myArray[(yValue * Height) + xValue)]

I guess there are better ways to do it.. maybe more Object Oriented..

--Ben
THANKS alot! One question. How can the image be placed in the array. Like would you tell the
image to be placed at like myarray[100][150]?

Last edited by KLB-Tronics; 10-09-2009 at 11:35 PM.
KLB-Tronics is offline   Reply With Quote
Old 10-10-2009, 01:14 PM   #4 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
Default

Quote:
Originally Posted by KLB-Tronics View Post
THANKS alot! One question. How can the image be placed in the array. Like would you tell the
image to be placed at like myarray[100][150]?
I'm not sure how mox approached it, but I used a subclass of UIView and made an array of these. Make a subclass called MapCell or GridCell with the info you need to draw the cell. Then locate them in the correct spots.

Code like this would place your custom views in the correct spot.
Code:
#define CellSize 50

MapCell *cells[5][6];
int numRows = 5;
int numColumns = 6;
int r, c;
	
for(r=0; r<numRows; r++)
{
	for(c=0; c<numColumns; c++)
	{
		cells[r][c] = [[MapCell alloc] initWithFrame:CGRectMake((c*CellSize), (r*CellSize), CellSize, CellSize)];
	}
}
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps

Last edited by kierster; 10-11-2009 at 12:11 AM.
kierster is offline   Reply With Quote
Old 10-10-2009, 11:39 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Default

Quote:
Originally Posted by kierster View Post
I'm not sure how mox approached it, but I used a subclass of UIView and made an array of these. Make a subclass called MapCell or GridCell with the info you need to draw the cell. Then locate them in the correct spots.

Code like this would place your custom views in the correct spot.
Code:
#define CellSize 50

MapCell *cells[r][c];
int numRows = 5;
int numColumns = 6;
int r, c;
	
for(r=0; r<numRows; r++)
{
	for(c=0; c<numColumns; c++)
	{
		cells[r][c] = [[MapCell alloc] initWithFrame:CGRectMake((c*CellSize), (r*CellSize), CellSize, CellSize)];
	}
}
This is great. Thanks. Can you help explain the code. Its kinda confusing to me
KLB-Tronics is offline   Reply With Quote
Old 10-11-2009, 12:10 AM   #6 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
Default

Quote:
Originally Posted by KLB-Tronics View Post
This is great. Thanks. Can you help explain the code. Its kinda confusing to me
Code:
#define CellSize 50 //Here is where you define the size of your cell, if you want a rectangular shaped cell, you would define a width and height

MapCell *cells[5][6];  //cells[row][column] - This is what your grid cells will be stored in, ie. cells[1][0] would be the 2nd row and 1st column
int numRows = 5;
int numColumns = 6;
int r, c;
	
for(r=0; r<numRows; r++) //increment through all rows
{
	for(c=0; c<numColumns; c++) //increment through all columns
	{
		cells[r][c] = [[MapCell alloc] initWithFrame:CGRectMake((c*CellSize), (r*CellSize), CellSize, CellSize)]; //this makes your cell stores it, and locates it
		[myView addSubview:cells[r][c]]; //this code will display it on a given view
	}
}
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps
kierster is offline   Reply With Quote
Old 10-27-2009, 08:48 AM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Linslade, UK
Posts: 29
Default

Quote:
#define CellSize 50 //Here is where you define the size of your cell, if you want a rectangular shaped cell, you would define a width and height

MapCell *cells[5][6]; //cells[row][column] - This is what your grid cells will be stored in, ie. cells[1][0] would be the 2nd row and 1st column
int numRows = 5;
int numColumns = 6;
int r, c;

for(r=0; r<numRows; r++) //increment through all rows
{
for(c=0; c<numColumns; c++) //increment through all columns
{
cells[r][c] = [[MapCell alloc] initWithFrame:CGRectMake((c*CellSize), (r*CellSize), CellSize, CellSize)]; //this makes your cell stores it, and locates it
[myView addSubview:cells[r][c]]; //this code will display it on a given view
}
}
hi i tried this but couldn't get it to work, so i declared MapCell as a uiimageview in my viewcontroller.h file but now it says cells is undeclared. could you explain what MapCell is please

cheers
ng93
ng93 is offline   Reply With Quote
Old 10-27-2009, 04:05 PM   #8 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
Default

Quote:
Originally Posted by ng93 View Post
hi i tried this but couldn't get it to work, so i declared MapCell as a uiimageview in my viewcontroller.h file but now it says cells is undeclared. could you explain what MapCell is please

cheers
ng93
MapCell could be a subclass of a UIView or a UIImageView.
and cells[] has to be declared before you can use it,
MapCell IS NOT a thing made by apple its just an example name for a UIView subclass I made up, it can be whatever you need to use for your project. If UIImageView works for you, use it!
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps
kierster is offline   Reply With Quote
Old 01-14-2010, 10:03 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 6
Default grid help

Quote:
Originally Posted by kierster View Post
MapCell could be a subclass of a UIView or a UIImageView.
and cells[] has to be declared before you can use it,
MapCell IS NOT a thing made by apple its just an example name for a UIView subclass I made up, it can be whatever you need to use for your project. If UIImageView works for you, use it!
Could someone help please.
After creating the grid,
How would you add an image into each cell block using the touch screen on each cell.
In other words, if i touch cell[1][3] then cell[2][1], how can i add an image into it.
evmacdev is offline   Reply With Quote
Old 01-14-2010, 12:07 PM   #10 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Linslade, UK
Posts: 29
Default

i haven't had time to test it but this should work:

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint touchPosition = [touch locationInView:self.view];
	CGFloat cellHeight = 20;
	CGFloat cellWidth = 20;
	int amountCellsWidth = 50;
	int amountCellsHeight = 50;
	for (int x=0; x<amountCellsWidth; x++) {
		for (int y=0; y<amountCellsHeight; y++) {
			if (touchPosition.y > y*cellHeight && touchPosition.y < (y+1)*cellHeight && touchPosition.x > x*cellWidth && touchPosition.x < (x+1)*cellWidth) {
				[cell[x][y] setImage:yourImage];
			}
		}
	}
}
ng93 is offline   Reply With Quote
Old 01-15-2010, 04:55 PM   #11 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 6
Default

Quote:
Originally Posted by ng93 View Post
i haven't had time to test it but this should work:

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint touchPosition = [touch locationInView:self.view];
	CGFloat cellHeight = 20;
	CGFloat cellWidth = 20;
	int amountCellsWidth = 50;
	int amountCellsHeight = 50;
	for (int x=0; x<amountCellsWidth; x++) {
		for (int y=0; y<amountCellsHeight; y++) {
			if (touchPosition.y > y*cellHeight && touchPosition.y < (y+1)*cellHeight && touchPosition.x > x*cellWidth && touchPosition.x < (x+1)*cellWidth) {
				[cell[x][y] setImage:yourImage];
			}
		}
	}
}
Thanks for response but I am still learning as i go.
Could you explain to me what each line is doing?
Much appreciated as i am sure this will help others.
evmacdev is offline   Reply With Quote
Old 01-17-2010, 12:14 PM   #12 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Linslade, UK
Posts: 29
Default

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //run when the user taps the screen
	UITouch *touch = [touches anyObject]; //the users touch (or first touch if they are using > one finger)
	CGPoint touchPosition = [touch locationInView:self.view]; //the position of the touch
	CGFloat cellHeight = 20; //the width of the individual cells (all cells must be the same width and height for this to work)
	CGFloat cellWidth = 20; //the height of the individual cells
	int amountCellsWidth = 50; //how many cells you have in each row
	int amountCellsHeight = 50; //how many cells you have in each column
	for (int x=0; x<amountCellsWidth; x++) { //repeats the following code for the integer amountCellsWidth
		for (int y=0; y<amountCellsHeight; y++) {//repeats the following code for the integer amountCellsHeight
			if (touchPosition.y > y*cellHeight && touchPosition.y < (y+1)*cellHeight && touchPosition.x > x*cellWidth && touchPosition.x < (x+1)*cellWidth) { //works out if the touch was to the right of the cell's x position, to the left of the cells x position + the cells width, underneath the cells y position and above the cells y position + the cells height
				[cell[x][y] setImage:yourImage]; //sets the tapped cells image to the UIImage yourImage
			}
		}
	}
}
hope this helps
ng93
ng93 is offline   Reply With Quote
Old 01-27-2010, 08:58 AM   #13 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 6
Default

Quote:
Originally Posted by ng93 View Post
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //run when the user taps the screen
	UITouch *touch = [touches anyObject]; //the users touch (or first touch if they are using > one finger)
	CGPoint touchPosition = [touch locationInView:self.view]; //the position of the touch
	CGFloat cellHeight = 20; //the width of the individual cells (all cells must be the same width and height for this to work)
	CGFloat cellWidth = 20; //the height of the individual cells
	int amountCellsWidth = 50; //how many cells you have in each row
	int amountCellsHeight = 50; //how many cells you have in each column
	for (int x=0; x<amountCellsWidth; x++) { //repeats the following code for the integer amountCellsWidth
		for (int y=0; y<amountCellsHeight; y++) {//repeats the following code for the integer amountCellsHeight
			if (touchPosition.y > y*cellHeight && touchPosition.y < (y+1)*cellHeight && touchPosition.x > x*cellWidth && touchPosition.x < (x+1)*cellWidth) { //works out if the touch was to the right of the cell's x position, to the left of the cells x position + the cells width, underneath the cells y position and above the cells y position + the cells height
				[cell[x][y] setImage:yourImage]; //sets the tapped cells image to the UIImage yourImage
			}
		}
	}
}
hope this helps
ng93
Thanks will try it
evmacdev is offline   Reply With Quote
Old 03-19-2010, 12:11 PM   #14 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 6
Default

Quote:
Originally Posted by ng93 View Post
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //run when the user taps the screen
	UITouch *touch = [touches anyObject]; //the users touch (or first touch if they are using > one finger)
	CGPoint touchPosition = [touch locationInView:self.view]; //the position of the touch
	CGFloat cellHeight = 20; //the width of the individual cells (all cells must be the same width and height for this to work)
	CGFloat cellWidth = 20; //the height of the individual cells
	int amountCellsWidth = 50; //how many cells you have in each row
	int amountCellsHeight = 50; //how many cells you have in each column
	for (int x=0; x<amountCellsWidth; x++) { //repeats the following code for the integer amountCellsWidth
		for (int y=0; y<amountCellsHeight; y++) {//repeats the following code for the integer amountCellsHeight
			if (touchPosition.y > y*cellHeight && touchPosition.y < (y+1)*cellHeight && touchPosition.x > x*cellWidth && touchPosition.x < (x+1)*cellWidth) { //works out if the touch was to the right of the cell's x position, to the left of the cells x position + the cells width, underneath the cells y position and above the cells y position + the cells height
				[cell[x][y] setImage:yourImage]; //sets the tapped cells image to the UIImage yourImage
			}
		}
	}
}
hope this helps
ng93
Sdk doesn't like this statement "[cell[x][y] setImage:yourImage];"
as 'cell' is undefined. Should cell be defined in the header as an 'int' or somewhere else.
evmacdev is offline   Reply With Quote
Old 03-19-2010, 01:37 PM   #15 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Linslade, UK
Posts: 29
Default

Quote:
Originally Posted by evmacdev View Post
Sdk doesn't like this statement "[cell[x][y] setImage:yourImage];"
as 'cell' is undefined. Should cell be defined in the header as an 'int' or somewhere else.
opps i made a typo

if your using the same code as originally posted it should be cells[x][y] (i think so anyway although its a long time since ive used this code)
ng93 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: 250
23 members and 227 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, FAED, fredidf, iDifferent, iph_s, JamesCahall, JasonR, mer10, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, timle8n1, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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