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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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-20-2011, 01:31 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default Cannot make bricks on a grid move. something wrong with my methods

Hey guys!

Iīm trying to create a kind of a puzzle game, where I have several bricks on a grid (6x6). The bricks can only move if there is a empty space located next to it. In other words, there is just one empty space all through the game and you use that one to move the bricks around on the grid.

I have an issue with my methods though. Iīm pretty sure that the correct point isnīt being "sent" to the other methods from my getPieceAtPoint method. In that method I specify which brick on the grid is being touched, but as I said... I think I declared the rest wrong.

I think as well that I did something wrong with the if-statements in my validemode method, but Iīm not sure what except that I suspect I shouldnīt have used center but something else entirely.

The code is successfully built and running without errors or warning but the movement doesnīt seem to work

Can any one help me?

Thanx in advance!

IBOutlet UIImageView *grid[BRICKWIDTH][BRICKHEIGHT];
CGPoint blankPosition;

Code:
//Get the point on the screen where the touch was made
-(void) getPieceAtPoint:(CGPoint) point{
	
	CGRect touchRect = CGRectMake(point.x, point.y, 1.0, 1.0);
	
	NSLog(@"getPieceAtPoint");
	for (int y = 0; y < BRICKHEIGHT; y++){
				
		for (int x = 0; x < BRICKWIDTH; x++){
				
			if( CGRectIntersectsRect([grid[x][y] frame], touchRect) ){
			
				[self movePiece:grid[x][y] withAnimation:YES];
				NSLog(@"X:  %d" , x);
				NSLog(@"Y:  %d" , y);
			}	
		}
	}
}
Code:
-(ShuffleMove) validMove:(UIImageView *) brickgrid{
	
	NSLog(@"ValidMove");
	
	// blank spot above current piece 
	if( brickgrid.center.x == blankPosition.x && brickgrid.center.y == blankPosition.y+1 ){
		return UP; 
		NSLog(@"UP");
	}
	
	// bank splot below current piece
	if( brickgrid.center.x == blankPosition.x && brickgrid.center.y == blankPosition.y-1 ){
		return DOWN; 
		NSLog(@"Down");
	}
	
	// bank spot left of the current piece
	if( brickgrid.center.x == blankPosition.x+1 && brickgrid.center.y == blankPosition.y ){
		return LEFT;
		NSLog(@"Left");
	}
	
	// bank spot right of the current piece
	if( brickgrid.center.x == blankPosition.x-1 && brickgrid.center.y == blankPosition.y ){
		return RIGHT; 
		NSLog(@"Right");
	}
	
	return NONE;
}
Code:
-(void) movePiece: (UIImageView *)brickgrid inDirectionX: (int) dx inDirectionY: (int) dy withAnimation: (BOOL) animate{ 
	
	NSLog(@"MovePiece to Direction");
	brickgrid.center = CGPointMake( brickgrid.center.x+dx,brickgrid.center.y+dy); 
	 
	blankPosition = CGPointMake( blankPosition.x-dx, blankPosition.y-dy );
	
	int x = brickgrid.center.x; 
	int y = brickgrid.center.y;
	
	if( animate ){
		[UIView beginAnimations:@"frame" context:nil];
	}
			
	brickgrid.frame = CGRectMake((BRICKWIDTH)*x, (BRICKHEIGHT)*y, 
									 BRICKWIDTH, BRICKHEIGHT );
	if( animate ){
		[UIView commitAnimations];
	}
}
Code:
-(void) movePiece:(UIImageView *)brickgrid withAnimation:(BOOL) animate{
	
	NSLog(@"MovePiece with Animation");
	
	switch ( [self validMove:brickgrid] ) {
		case UP:
			[self movePiece:brickgrid 
			   inDirectionX:0 inDirectionY:-1 withAnimation:animate];
			NSLog(@"UP");
			break;
		case DOWN:
			[self movePiece:brickgrid 
			   inDirectionX:0 inDirectionY:1 withAnimation:animate];
			NSLog(@"DOWN");
			break;
		case LEFT:
			[self movePiece:brickgrid
			   inDirectionX:-1 inDirectionY:0 withAnimation:animate];
			NSLog(@"LEFT");
			break;
		case RIGHT:
			[self movePiece:brickgrid 
			   inDirectionX:1 inDirectionY:0 withAnimation:animate];
			NSLog(@"RIGHT");
			break;
		default:
			break;
	}
Chris1979 is offline   Reply With Quote
Old 03-20-2011, 05:06 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

i would use NSMutableArray, and not C-style array.
__________________
dany_dev is offline   Reply With Quote
Old 03-20-2011, 05:17 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany88 View Post
i would use NSMutableArray, and not C-style array.
Ok... and will that also solve the issue with the if statements in the validmode method or just the issue that I have with not being able to get the other methods to get the correct point of the bricks?
Chris1979 is offline   Reply With Quote
Old 03-20-2011, 05:44 AM   #4 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

which is the problem on that method?

what is ShuffleMove?
__________________
dany_dev is offline   Reply With Quote
Old 03-20-2011, 06:03 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany88 View Post
which is the problem on that method?

what is ShuffleMove?
Shufflemove? oops.. wrong code... That was the experimental one.

this should be the correct one

Code:
-(int) validMove:(UIImageView *) brickgrid{
	
	NSLog(@"ValidMove");
	
	// blank spot above current piece 
	if( brickgrid.center.x == blankPosition.x && brickgrid.center.y == blankPosition.y+1 ){
		return UP; 
		NSLog(@"UP");
	}
	
	// bank splot below current piece
	if( brickgrid.center.x == blankPosition.x && brickgrid.center.y == blankPosition.y-1 ){
		return DOWN; 
		NSLog(@"Down");
	}
	
	// bank spot left of the current piece
	if( brickgrid.center.x == blankPosition.x+1 && brickgrid.center.y == blankPosition.y ){
		return LEFT;
		NSLog(@"Left");
	}
	
	// bank spot right of the current piece
	if( brickgrid.center.x == blankPosition.x-1 && brickgrid.center.y == blankPosition.y ){
		return RIGHT; 
		NSLog(@"Right");
	}
	
	return NONE;
}
Iīm tually not sure... but I think I shouldnīt use center in the if statments in validemode... just a feeling I have.

Last edited by Chris1979; 03-20-2011 at 06:19 AM.
Chris1979 is offline   Reply With Quote
Old 03-20-2011, 12:31 PM   #6 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

sorry, but i don't understand what the method should do, and what you are trying to do.
__________________
dany_dev is offline   Reply With Quote
Old 03-20-2011, 12:59 PM   #7 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
sorry, but i don't understand what the method should do, and what you are trying to do.
The method is used for directions... in other words which direction the bricks should move... but only if they are next to a empty space. In other words the method checks if the "accessed" brick is next to a empty space

Last edited by Chris1979; 03-20-2011 at 01:19 PM.
Chris1979 is offline   Reply With Quote
Old 03-21-2011, 12:07 AM   #8 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
sorry, but i don't understand what the method should do, and what you are trying to do.
I modified my code a little by putting in a new CGPoint called currentPosition, which is (should be) the current position of the brick that was touched but I still canīt get it to work...

...maybe Iīm doing it all wrong.

the error message that I now get is error: subscripted value is neither array nor pointer

Itīs obvious why I get that specificmessage on the currentPosition = brickgrid[x][y]; in the FOR-loops but I have no idea how to be able to get the grid[x][y] from the getAtPoint method to the other methods...
...to be able to move that specific brick that was touched

You said earlier that it would be better with a NSMutableArray but how do you create a grid with an NSMutableArray?

Sorry if I sound clueless.
The thing is that I cannot find another way to make a grid than the way I made it

(or make the pieces inside the grid move when touched by the player)

Maybe there are other (better) ways to make all of this?

CGPoint currentPosition;

Code:
-(int) validMove:(UIImageView *) brickgrid{
	
	NSLog(@"ValidMove");
	
	for (int y = 0; y < BRICKHEIGHT; y++){
		
		for (int x = 0; x < BRICKWIDTH; x++){
			
			currentPosition = brickgrid[x][y];
		}
	}
	
	
	// blank spot above current brick 
	if( currentPosition.x == blankPosition.x && currentPosition.y == blankPosition.y+1 ){
		return UP; 
		NSLog(@"UP");
	}
	
	// bank splot below current brick
	if( currentPosition.x == blankPosition.x && currentPosition.y == blankPosition.y-1 ){
		return DOWN; 
		NSLog(@"Down");
	}
	
	// bank spot left of the current brick
	if( currentPosition.x == blankPosition.x+1 && currentPosition.y == blankPosition.y ){
		return LEFT;
		NSLog(@"Left");
	}
	
	// bank spot right of the current brick
	if( currentPosition.x == blankPosition.x-1 && currentPosition.y == blankPosition.y ){
		return RIGHT; 
		NSLog(@"Right");
	}
	
	return NONE;
}
Code:
-(void) movePiece: (UIImageView *)brickgrid inDirectionX: (int) dx inDirectionY: (int) dy withAnimation: (BOOL) animate{ 
	
	NSLog(@"MovePiece to Direction");
	
	for (int y = 0; y < BRICKHEIGHT; y++){
		
		for (int x = 0; x < BRICKWIDTH; x++){
			
			currentPosition = brickgrid[x][y];
		}
	}
	
	currentPosition = CGPointMake( currentPosition.x+dx,currentPosition.y+dy); 
	 
	blankPosition = CGPointMake( blankPosition.x-dx, blankPosition.y-dy );
	
	int x = currentPosition.x; 
	int y = currentPosition.y;
	
	if( animate ){
		[UIView beginAnimations:@"frame" context:nil];
	}
			
	brickgrid.frame = CGRectMake((BRICKWIDTH)*x, (BRICKHEIGHT)*y, 
									 BRICKWIDTH, BRICKHEIGHT );
	if( animate ){
		[UIView commitAnimations];
	}
}
Chris1979 is offline   Reply With Quote
Old 03-22-2011, 01:07 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
sorry, but i don't understand what the method should do, and what you are trying to do.
I think I found a solution to my problem.

I havenīt tried it out yet but Iīll let you know how it turned out as soon as I try it.
Chris1979 is offline   Reply With Quote
Old 03-22-2011, 11:49 PM   #10 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
sorry, but i don't understand what the method should do, and what you are trying to do.
Iīve tried out my solution and it worked. All of the methods got the X- & Y-coordinates from the getPieceAtPoint method.

The solution I tried was to send the X- & Y-coordinates instead of the UIImageView to the rest of my methods. I dodnīt know why I didnīt try this in first place.

In other words... I modified the "method entry" so they are now getting (int) brickX YPosition: (int) brickY instead of getting the (UIImageView *)brickgrid.

The last issue that I now have is to make the bricks animate upon touch. To switch places with the blank area on the grid.

I figured out that the problem lies in the method below and in the row which is slashed (//).

Code:
-(void) movePiece: (int) brickX YPosition: (int) brickY inDirectionX: (int) dx inDirectionY: (int) dy withAnimation: (BOOL) animate{ 
	
	NSLog(@"MovePiece in Direction");
	NSLog(@"brickX: %d", brickX);
	NSLog(@"brickY: %d", brickY);
	NSLog(@"dx: %d", dx);
	NSLog(@"dy: %d", dy);
	
	currentPosition = CGPointMake( brickX+dx,brickY+dy); 
	 
	blankPosition = CGPointMake( blankPosition.x-dx, blankPosition.y-dy );
	
	int x = currentPosition.x; 
	int y = currentPosition.y;
	
	NSLog(@"x: %d", x);
	NSLog(@"y: %d", y);
	
	if( animate ){
		[UIView beginAnimations:@"frame" context:nil];
	}
	
	//grid[x][y].frame = CGRectMake((BRICKWIDTH)*x, (BRICKHEIGHT)*y, 
	//								 BRICKWIDTH, BRICKHEIGHT );
	if( animate ){
		[UIView commitAnimations];
	}
}
Any suggestions?

My way of thinking with the // part got the bricks to move to the upper left corner of the screen and minimize along the way (instead of moving to the empty location on the grid and staying in the same size)

Last edited by Chris1979; 03-23-2011 at 02:18 AM.
Chris1979 is offline   Reply With Quote
Old 03-24-2011, 12:54 AM   #11 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
sorry, but i don't understand what the method should do, and what you are trying to do.
I was able to solve the brick-movement part (the // part in the code above) by modifying it like this

Code:
grid[brickX][brickY].frame = CGRectMake((Dx * 48)+52, (Dy * 49)+7, 
									 50, 50 );
the bricks move now upon touch (swap places with the blank area) but a new issue appeared... with the first touch. I will create a new thread for that part.

Thanx for all the help. I appreciate it
Chris1979 is offline   Reply With Quote
Old 03-24-2011, 02:47 AM   #12 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

nice work, i'm happy that you found a solution.

I'm sorry to haven't helped you, but really i still miss something to understand the problem that you had.

However good work, finally someone that can solve a problem keeping trying himself.
__________________
dany_dev is offline   Reply With Quote
Old 03-24-2011, 04:56 AM   #13 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
nice work, i'm happy that you found a solution.

I'm sorry to haven't helped you, but really i still miss something to understand the problem that you had.

However good work, finally someone that can solve a problem keeping trying himself.

No worries, you helped me more than you think, both with this thread and the previous one.

the problem was that I didnīt know how to send the X-Y coordinates from the getpieceatpoint method to the rest of my methods so they could perform their "job" (in other words move the brick that I touched and swap itīs current location on the grid with the location of the blank space).

I think my previous explanations sucked... sorry for that.. and hopefully this one is better and could give you an perspective of what the problem was

Thanx again... and take care


now... on to the next issue.. hehe...
Chris1979 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: 419
5 members and 414 guests
chemistry, hussain1982, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,922
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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