Iphone 网格问题中的砖块

Iphone 网格问题中的砖块,iphone,cocoa-touch,xcode,Iphone,Cocoa Touch,Xcode,在开始提问之前,让我先用砖块解释一下这个概念 这个游戏是一个益智游戏。我有一个网格是6x6。除了一个方格外,所有方格上都有砖块。游戏开始时,空方块总是从5x5开始,原因是砖块只有在旁边有空方块时才能移动。他们移动的方向是朝向空的正方形。换言之,他们将在触摸后用空方块交换位置。你可以说,运动和视觉部分看起来几乎像滑块图片游戏。但游戏的概念是不同的 我的第一个问题是,出于某种原因,我只能朝一个方向移动砖块。我应该能把它们向上、下、左、右四个方向移动。我与NSLog进行了检查,发现只有屏幕上的UIIm

在开始提问之前,让我先用砖块解释一下这个概念

这个游戏是一个益智游戏。我有一个网格是6x6。除了一个方格外,所有方格上都有砖块。游戏开始时,空方块总是从5x5开始,原因是砖块只有在旁边有空方块时才能移动。他们移动的方向是朝向空的正方形。换言之,他们将在触摸后用空方块交换位置。你可以说,运动和视觉部分看起来几乎像滑块图片游戏。但游戏的概念是不同的

我的第一个问题是,出于某种原因,我只能朝一个方向移动砖块。我应该能把它们向上、下、左、右四个方向移动。我与NSLog进行了检查,发现只有屏幕上的UIImage更新到新位置。不是代码中的实际位置。换句话说。。。代码似乎认为,即使在屏幕上的砖块移动到另一个正方形后,砖块仍处于开始位置。更新代码内部位置的最佳方法是什么,以便我可以移动到四个方向中的任何一个

我的第二个问题是,当我检查NSLog时,位于网格上4x5位置的砖块(位于空正方形的左侧)似乎以一个的价格获得了两个位置。这会导致砖块迷失方向,无法正常移动,这只是一步。砖块只能得到一个位置,即x=4y=5,而不是x=4y=5,x=5y=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];
    }

}

尝试替换该行:

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


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

这不是一个论坛,没有线程。如果此问题与您自己或其他人的一些其他问题相关,请添加相关问题和/或答案的链接。对于线程部分,我很抱歉。我只是习惯于听到我的每个朋友都把这些东西称为线索:这个问题与任何其他问题都没有关系嘿!这两个问题都很有效!谢谢你的帮助!希望有一天能报答你的恩惠:D我想知道另一件事,。。。我正在制作的益智游戏是一款3连拍的游戏。这个更新会与我将要编写的“查找匹配项”代码一起工作,还是需要修改任何内容?
grid[Dx][Dy] = grid[brickX][brickY];
grid[Dx][Dy].frame = CGRectMake((Dx * 48)+52, (Dy * 49)+7, 50, 50 );
grid[brickX][brickY] = NULL;