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.
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.
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.
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.
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
}
}
#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
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!
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.
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.
- (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
}
}
}
}
- (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
}
}
}
}
- (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.
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)