开发者

bricks in a grid issue

Let me explain the concept with the bricks before starting with the questions.

The game is a puzzle game. I have a grid which is 6x6. I have bricks on all squares of the grid except for one. The reason for the empty square (which always starts at 5x5 in the beginning of the game) is that the bricks will only be able to move if they have the empty square next to them. The direction they will move to will be towards the empty square. In other words, they will then swap places with the empty square upon touch. You could say that the movement and visual parts looks almost like the slider picture game. The game concept though is different.

My first question is that I for some reason can only move the bricks in one direction. I should be able to move them in all four directions (UP, DOWN, LEFT, RIGHT). I´ve checked with NSLog and saw that it´s only the UIImages on the screen that update to new positions. not the actual position inside the code. in other words... the code seem to think that the bricks still are on the start position even after the brick on the screen has moved to another square. What is the best way to "update" positions inside the code so I can move to any of the four directions?

My second question is that the brick that is located at the 4x5 position on the grid (which is left of the empty square) seem to get two positions "at the price of one" when I check with NSLog. this causes the brick to become "disoriented" and will not move properly which is only one step. the brick should only get one position which is x= 4 y= 5 and not x=4 y= 5, x= 5 y = 5. I´ve located the issue to the empty square on the grid. without it the bricks movement is fine. Is there a way to solve this issue since the empty square is a part of the game?

IBOutlet UIImageView *grid[BRICKWIDTH][BRICKHEIGHT];
NSString *brickTypes[6];

//Putting the bricks on the grid
-(void)createbricks{    

    brickTypes[0] = @"Ch'en.png"; 
    brickTypes[1] = @"K'ank'in.png"; 
    brickTypes[2] = @"K'ayab'.png"; 
    brickTypes[3] = @"kej.png";
    brickTypes[4] = @"kumk'u.png";
    brickTypes[5] = @"mak.png";


    //empty spot in the bottom right corner of the grid
    blankPosition = CGPointMake(5, 5);

    int Bx = blankPosition.x;
    int By = blankPosition.y;

    NSLog(@"CreateBricks, Startposition, blankPosition.x = Bx: %d", Bx);
    NSLog(@"CreateBricks, Startposition, blankPosition.y = By: %d", By);


    for (int y = 0; y < BRICKHEIGHT; y++)
    {
        for (int x = 0; x < BRICKWIDTH; x++) 
        {

            CGPoint orgPosition = CGPointMake(x,y);

            if( blankPosition.x == orgPosition.x && blankPosition.y == orgPosition.y ){
                continue; 
            }

            UIImage *image = [UIImage imageNamed: brickTypes [rand() % 6]];
            grid[x][y] = [[[UIImageView alloc] initWithImage:image] autorelease];
            CGRect newFrame = grid[x][y].frame; 
            newFrame.origin = CGPointMake((x * 48)+52, (y * 49)+7);
                grid[x][y].frame = newFrame;

            [self.view addSubview:grid[x][y]];

            NSLog(@"Grid x: %d", x);
            NSLog(@"Grid y: %d", y);
            [image release];


        }

    }

}

//Valid moves: UP, DOWN, LEFT or RIGHT

-(Move) 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;
}


//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, Xposition, brickX: %d", brickX);
    NSLog(@"MovePiece in Direction, 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);

    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);

    blankPosition = CGPointMake(blankPosition.x-dx, blankPosition.y-dy);

    int Bx = blankPosition.x;
    int By = blankPosition.y;

    NSLog(@"MovePiece in Direction开发者_JAVA百科, Movedposition, blankPosition.x (blankPosition.x-dx) = Bx: %d", Bx);
    NSLog(@"MovePiece in Direction, Movedposition, 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];
    }

    [sharedSoundManager playSoundWithKey:@"movingbricks" gain:0.13f pitch:1.0f shouldLoop:NO];
}


//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");

            NSLog(@"-----------------------------------------------");

            break;

        case DOWN:

            [self movePiece:brickX YPosition: brickY inDirectionX:0 inDirectionY:1 withAnimation:animate];

            NSLog(@"DOWN");

            NSLog(@"-----------------------------------------------");

            break;

        case LEFT:

            [self movePiece:brickX YPosition: brickY inDirectionX:-1 inDirectionY:0 withAnimation:animate];

            NSLog(@"LEFT");

            NSLog(@"-----------------------------------------------");

            break;

        case RIGHT:

            [self movePiece:brickX YPosition: brickY inDirectionX:1 inDirectionY:0 withAnimation:animate];

            NSLog(@"RIGHT");

            NSLog(@"-----------------------------------------------");

            break;

        default:
            break;
    }
}


//Get the point on the screen (but inside the grid) where the touch was made

-(void) getPieceAtPoint:(CGPoint) point{

    CGRect touchRect = CGRectMake(point.x, point.y, 1.0, 1.0);

    NSLog(@"getPieceAtPoint, point.x:  %d" , point.x);
    NSLog(@"getPieceAtPoint, point.y:  %d" , point.y);

    for (int y = 0; y < BRICKHEIGHT; y++){

        for (int x = 0; x < BRICKWIDTH; x++){

            if( CGRectIntersectsRect([grid[x][y] frame], touchRect) ){

                NSLog(@"getPieceAtPoint, Forloop, X:  %d" , x);
                NSLog(@"getPieceAtPoint, Forloop, Y:  %d" , y);

                [self movePiece:x YPosition:y withAnimation:YES];

            }
        }
    }
}


    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"touchesEnded");
        if(!GamePaused){

        int Bx = blankPosition.x;
        int By = blankPosition.y;

        NSLog(@"touchesEnded, blankPosition.x = Bx: %d", Bx);
        NSLog(@"touchesEnded, blankPosition.y = By: %d", By);

        int Dx = currentPosition.x; 
        int Dy = currentPosition.y;

        NSLog(@"touchesEnded, CurrentPosition.x = Dx: %d", Dx);
        NSLog(@"touchesEnded, CurrentPosition.y = Dy: %d", Dy);

        UITouch *touch = [touches anyObject];
        CGPoint currentTouch = [touch locationInView:self.view];    
        [self getPieceAtPoint:currentTouch];
    }

}


Try replacing the line:

grid[brickX][brickY].frame = CGRectMake((Dx * 48)+52, (Dy * 49)+7, 50, 50 );

With:

grid[Dx][Dy] = grid[brickX][brickY];
grid[Dx][Dy].frame = CGRectMake((Dx * 48)+52, (Dy * 49)+7, 50, 50 );
grid[brickX][brickY] = NULL;

That code updates the 'actual position in the code'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜