网格问题中的砖块

时间:2011-04-15 06:48:22

标签: iphone cocoa-touch xcode

在开始提问之前,让我用砖解释这个概念。

游戏是一款益智游戏。我有一个6x6的网格。除了一个之外,我在网格的所有方格上都有砖块。空方块(在游戏开始时始终以5x5开始)的原因是,如果砖块旁边有空方块,则只能移动。他们将移动的方向将朝向空方。换句话说,他们将在触摸时与空方块交换位置。你可以说运动和视觉部分看起来几乎像滑块图片游戏。虽然游戏概念不同。

我的第一个问题是,由于某种原因,我只能朝着一个方向移动砖头。我应该可以在所有四个方向(向上,向下,向左,向右)移动它们。我已经检查过NSLog,看到只有屏幕上的UIImages才更新到新位置。不是代码中的实际位置。换句话说......代码似乎认为即使在屏幕上的砖块移动到另一个方块之后,砖块仍然处于起始位置。在代码中“更新”位置的最佳方法是什么,以便我可以移动到四个方向中的任何一个?

我的第二个问题是,当我用NSLog检查时,位于网格上4x5位置的砖块(在空方块的左侧)似乎以“1的价格”获得两个位置。这导致砖变得“迷失方向”并且不能正常移动,这只是一步。砖应该只得到一个位置,即x = 4 y = 5而不是x = 4 y = 5,x = 5 y = 5.我已将问题定位到网格上的空方块。没有它,砖块运动很好。有没有办法解决这个问题,因为空方块是游戏的一部分?

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, 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];
    }

}

1 个答案:

答案 0 :(得分:0)

尝试替换该行:

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

使用:

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

该代码更新了“代码中的实际位置”。