2011-04-15 45 views
0

讓我在開始問題之前用磚塊解釋這個概念。網格問題中的磚

遊戲是一款益智遊戲。我有一個6x6的網格。除了一格之外,我在網格的所有格子上都有磚塊。空方塊(總是從遊戲開始時的5x5開始)的原因是,如果磚塊旁邊有空方塊,磚塊才能移動。他們將轉向的方向將朝向空方。換句話說,他們隨後會與空白的廣場交換位置。你可以說運動和視覺部分看起來就像滑塊圖片遊戲。遊戲概念雖然不同。

我的第一個問題是,我出於某種原因只能向一個方向移動磚塊。我應該能夠在所有四個方向上移動它們(向上,向下,向左,向右)。我檢查過NSLog,看到它只是屏幕上的UIImage更新到新的位置。而不是代碼中的實際位置。換句話說......代碼似乎認爲,即使屏幕上的磚塊移動到另一個方塊後,磚塊仍然處於起始位置。在代碼中「更新」位置的最好方法是什麼,以便我可以移動到四個方向中的任何一個?

我的第二個問題是,當我檢查NSLog時,位於網格4x5位置(這是空的方形左側)的磚似乎得到兩個「以一個價格」的位置。這導致磚塊變得「迷失方向」而不能正確移動,這只是一步。磚塊應該只能得到一個位置,即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]; 
    } 

} 
+0

這不是論壇,沒有「線索」。如果此問題與其他一些問題(您自己或他人的問題)相關,請添加指向相關問題和/或答案的鏈接。 – Mat 2011-04-15 06:56:14

+0

對不起「線程」部分。我剛剛聽到我的朋友的每個人都把這個東西稱爲線程:)這個問題與任何其他問題都沒有關係 – iphonedevonthemake 2011-04-15 07:05:36

回答

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; 

該代碼更新 '中的代碼實際位置'。

+0

ok!感謝名單!我今晚試試看,回覆你:) – iphonedevonthemake 2011-04-15 10:40:08

+0

嘿!它在兩個問題上都很好用! Thanx的幫助!希望有朝一日能夠得到回報:D我想知道另外一件事情,但是我創造的益智遊戲是連續3種類型的遊戲。這個更新會使用「即時匹配」代碼來處理我即將寫入的內容,還是需要修改任何內容? – iphonedevonthemake 2011-04-16 07:48:54