movement of brick works only one direction and only once
Hey guys!
I have a problem with my code below. The bricks (that move upon touch and only if they are next to a blank space) seem only to be able to move once... once you moved them in one direction you cannot use them again.
They are supposed to be able to move every time they are next to the blank space
Any suggestions how to solve this?
Code:
-(int) validMove:(int) brickX Yposition: (int) brickY{
NSLog(@"validmode, Current Xposition, brickX: %d", brickX);
NSLog(@"validmode, Current Yposition, brickY: %d", brickY);
// blank spot above current brick
if( brickX == blankPosition.x && brickY == blankPosition.y+1 ){
return UP;
NSLog(@"UP");
}
// bank splot below current brick
if( brickX == blankPosition.x && brickY == blankPosition.y-1 ){
return DOWN;
NSLog(@"Down");
}
// bank spot left of the current brick
if( brickX == blankPosition.x+1 && brickY == blankPosition.y ){
return LEFT;
NSLog(@"Left");
}
// bank spot right of the current brick
if( brickX == blankPosition.x-1 && brickY == blankPosition.y ){
return RIGHT;
NSLog(@"Right");
}
return NONE;
}
Code:
//Animation of the direction of the movement
-(void) movePiece: (int) brickX YPosition: (int) brickY inDirectionX: (int) dx inDirectionY: (int) dy withAnimation: (BOOL) animate{
NSLog(@"MovePiece in Direction, Current Xposition, brickX: %d", brickX);
NSLog(@"MovePiece in Direction, Current Yposition, brickY: %d", brickY);
NSLog(@"MovePiece in Direction, inDirectionX, dx: %d", dx);
NSLog(@"MovePiece in Direction, inDirectionY, dy: %d", dy);
currentPosition = CGPointMake( brickX+dx,brickY+dy);
blankPosition = CGPointMake( blankPosition.x-dx, blankPosition.y-dy );
int Dx = currentPosition.x;
int Dy = currentPosition.y;
NSLog(@"MovePiece in Direction, CurrentPosition.x (BrickX+dx) = Dx: %d", Dx);
NSLog(@"MovePiece in Direction, CurrentPosition.y (BrickY+dy) = Dy: %d", Dy);
int Bx = blankPosition.x;
int By = blankPosition.y;
NSLog(@"MovePiece in Direction, blankPosition.x (blankPosition.x-dx) = Bx: %d", Bx);
NSLog(@"MovePiece in Direction, blankPosition.y (blankPosition.y-dy) = By: %d", By);
if( animate ){
[UIView beginAnimations:@"frame" context:nil];
}
//Moving the brick to itīs new location
grid[brickX][brickY].frame = CGRectMake((Dx * 48)+52, (Dy * 49)+7,
50, 50 );
if( animate ){
[UIView commitAnimations];
}
//brick SFX upon movement
NSString *path = [[NSBundle mainBundle] pathForResource:@"movingbricks" ofType:@"m4a"];
brickaudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[brickaudio play];
brickaudio.volume = 0.13;
}
Code:
//Which direction the brick will move to
-(void) movePiece:(int) brickX YPosition: (int) brickY withAnimation:(BOOL) animate{
NSLog(@"MovePiece with Animation, Current Xposition, brickX: %d", brickX);
NSLog(@"MovePiece with Animation, Current Xposition, brickY: %d", brickY);
switch([self validMove:brickX Yposition:brickY]){
case UP:
[self movePiece: brickX YPosition: brickY
inDirectionX:0 inDirectionY:-1 withAnimation:animate];
NSLog(@"UP");
break;
case DOWN:
[self movePiece:brickX YPosition: brickY
inDirectionX:0 inDirectionY:1 withAnimation:animate];
NSLog(@"DOWN");
break;
case LEFT:
[self movePiece:brickX YPosition: brickY
inDirectionX:-1 inDirectionY:0 withAnimation:animate];
NSLog(@"LEFT");
break;
case RIGHT:
[self movePiece:brickX YPosition: brickY
inDirectionX:1 inDirectionY:0 withAnimation:animate];
NSLog(@"RIGHT");
break;
default:
break;
}
}
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);
for (int y = 0; y < BRICKHEIGHT; y++){
for (int x = 0; x < BRICKWIDTH; x++){
if( CGRectIntersectsRect([grid[x][y] frame], touchRect) ){
[self movePiece:x YPosition:y withAnimation:YES];
NSLog(@"getPieceAtPoint, Forloop, X: %d" , x);
NSLog(@"getPieceAtPoint, Forloop, Y: %d" , y);
NSLog(@"-----------------------------------------------");
}
}
}
}
So what behavior are you seeing? You click a brick and it says "UP" and moves up, then you click it the same brick again and in says "UP" again instead of "DOWN" ?
When you click the brick, do your logs print the correct brickX and brickY the first time? Do they print the correct numbers the second time?
So what behavior are you seeing? You click a brick and it says "UP" and moves up, then you click it the same brick again and in says "UP" again instead of "DOWN" ?
When you click the brick, do your logs print the correct brickX and brickY the first time? Do they print the correct numbers the second time?
The first time you click on a brick (which is next to the empty space), it moves to that direction. The log says for example Up if the empty space is above the brick (and the X-Y coordinatates of the bricks current position as well as the X-Y coordinates of the empty space) and the brick then moves up one step. Placing the empty space below it. so far so good.
But...
the second time you click on that same brick (after it moved) it should say down and then go down but it doesnīt even enter that part of the code. it looks like it just stays at the touchesend method and the log only says the X-Y coordinates of the brick that was touched. nothing more seems to happen.
itīs like that part of the code has been "turned off" for that specific brick after it moved once.
the second time you click on that same brick (after it moved) it should say down and then go down but it doesnīt even enter that part of the code. it looks like it just stays at the touchesend method and the log only says the X-Y coordinates of the brick that was touched.
Does it give the CORRECT x,y for the brick that is touched the second time, or does it give the same number that you got the first time?
Quote:
nothing more seems to happen. itīs like that part of the code has been "turned off" for that specific brick after it moved once.
So validMove: is not getting run, or validMove: is returning none?
It gives the X-Y coordinates that the brick had the first time
Thought so. You're keeping your brick pointers in a 2d array, and trying to use that array to figure out where the images are on the screen. But you're never moving the pointers in the array; they always stay in the same place.
In your example you click on bricks[4][5], which causes it to move to a new position on the screen. When you click on the same image again it's still in the slot bricks[4][5] even though you moved to to a different place on the screen; then I expect validMove returns NONE because 4,5 matches the position of blankPosition.
I think you need to move your bricks around the 2D array in addition to moving them around the screen. You are updating the view (the screen) but not the model (the 2D array).
It gives the X-Y coordinates that the brick had the first time
Thought so. You're keeping your brick pointers in a 2d array, and trying to use that array to figure out where the images are on the screen. But you're never moving the pointers in the array; they always stay in the same place.
In your example you click on bricks[4][5], which causes it to move to a new position on the screen. When you click on the same image again it's still in the slot bricks[4][5] even though you moved to to a different place on the screen; then I expect validMove returns NONE because 4,5 matches the position of blankPosition.
I think you need to move your bricks around the 2D array in addition to moving them around the screen. You are updating the view (the screen) but not the model (the 2D array).
It gives the X-Y coordinates that the brick had the first time
Thought so. You're keeping your brick pointers in a 2d array, and trying to use that array to figure out where the images are on the screen. But you're never moving the pointers in the array; they always stay in the same place.
In your example you click on bricks[4][5], which causes it to move to a new position on the screen. When you click on the same image again it's still in the slot bricks[4][5] even though you moved to to a different place on the screen; then I expect validMove returns NONE because 4,5 matches the position of blankPosition.
I think you need to move your bricks around the 2D array in addition to moving them around the screen. You are updating the view (the screen) but not the model (the 2D array).
I think I forgot to put this part of the declaration in the thread earlier
That is the suggestion - to set bricks[5][5] to point to the brick that is now in that position, and set bricks[4][5] to point to nil (since it is now the blank square.)
That is the suggestion - to set bricks[5][5] to point to the brick that is now in that position, and set bricks[4][5] to point to nil (since it is now the blank square.)
Let me rephrase my question
where (and how) do I do that?
Sorry If I sound clueless... the thing is.... that it feels like Iīve been hitting my head on a wall these past few days trying to solve this issue (and one other issue as well).
Adding the logs was a good idea; at least now you know which parts of your code are getting run, and can explain it to others.
I'm not sure handing out code will help though; If I give you a line of code, will you be OK from now on or will you need to ask for the next line after that? It seems like you have a lot of code already but you don't understand what it does.
You should probably as a "why" question next, not a "how" question; some query that will help you understand what your program is already doing. Either that or start a simpler project and come back to this one later. You may have bitten off more than you can chew for your first game.
I know you said you were following a tutorial, but either the tutorial is broken, your work doesn't match the tutorial, or you're trying to change it without understanding it. The best way to use tutorials, especially for beginners, is to (1) copy the tutorial exactly (2) question each line to figure out what it does (3) make small changes to confirm your understanding (4) adapt it to your needs.
I think you've made your work harder by skipping to #4. That's what's making your head hurt.
Adding the logs was a good idea; at least now you know which parts of your code are getting run, and can explain it to others.
I'm not sure handing out code will help though; If I give you a line of code, will you be OK from now on or will you need to ask for the next line after that? It seems like you have a lot of code already but you don't understand what it does.
You should probably as a "why" question next, not a "how" question; some query that will help you understand what your program is already doing. Either that or start a simpler project and come back to this one later. You may have bitten off more than you can chew for your first game.
I know you said you were following a tutorial, but either the tutorial is broken, your work doesn't match the tutorial, or you're trying to change it without understanding it. The best way to use tutorials, especially for beginners, is to (1) copy the tutorial exactly (2) question each line to figure out what it does (3) make small changes to confirm your understanding (4) adapt it to your needs.
I think you've made your work harder by skipping to #4. That's what's making your head hurt.
I understand what you mean but I think you misunderstood me
Iīm not asking for a line of code etc just if you saw where in the code I could do the changes. Itīs always better looking at it from another angle
itīs not that I donīt understand the code I wrote itīs just that I have one other issue as well which Iīm trying to solve simultaneously so I have stared myself blind on both.
Yes, I followed a tutorial for the moving brick part of the game and yes, I have made some changes to adapt it to my own code (which may have caused the issue) but the rest of the code is by own design.
I have solved all the issues I had so far except these two and that drives me crazy... since Iīm not able find where the problem lies and how to solve it (on both issues).
In this case I know (thanks to you) that the problem is in the grid[][] and the pointer. Now I just need to be able to see past my "blind spot" and find the solution.
hope I donīt sound to confusing or anything. I just tried to explain the reason for this thread and answer to your reply.
Adding the logs was a good idea; at least now you know which parts of your code are getting run, and can explain it to others.
I'm not sure handing out code will help though; If I give you a line of code, will you be OK from now on or will you need to ask for the next line after that? It seems like you have a lot of code already but you don't understand what it does.
You should probably as a "why" question next, not a "how" question; some query that will help you understand what your program is already doing. Either that or start a simpler project and come back to this one later. You may have bitten off more than you can chew for your first game.
I know you said you were following a tutorial, but either the tutorial is broken, your work doesn't match the tutorial, or you're trying to change it without understanding it. The best way to use tutorials, especially for beginners, is to (1) copy the tutorial exactly (2) question each line to figure out what it does (3) make small changes to confirm your understanding (4) adapt it to your needs.
I think you've made your work harder by skipping to #4. That's what's making your head hurt.
hey again!
I think Iīve just solved half of it...
..the correct X-Y coordinates are shown now in the logs... now itīs just the animation that doesnīt work...