2012-08-12 110 views
1

我正在製作一個棋盤遊戲,當一個人獲勝時我希望棋盤重置自己。如果有幫助,我使用iPhone的cocos2d。我有一個重置所有變量和塊數組的重置方法。它在下一次重置一次後,一個人獲勝並不會重置主板。有想法該怎麼解決這個嗎? 這是.m文件中的方法。 //方法Cocos-2d iphone重置遊戲板

-(void) resetGame { 
self.isTouchEnabled = YES; 

if((self=[super init])) { 


    self.isTouchEnabled = YES; 

    turn = 2; 

    pieces = [[NSMutableArray alloc] initWithObjects: 
       [[Piece alloc] pieceWithType:1 player:1 row:1 col:3], // bSphere1 
       [[Piece alloc] pieceWithType:1 player:1 row:2 col:3], // bSphere2 
       [[Piece alloc] pieceWithType:2 player:1 row:1 col:2], // bSquare1 
       [[Piece alloc] pieceWithType:2 player:1 row:3 col:3], // bSquare2 
       [[Piece alloc] pieceWithType:2 player:1 row:0 col:3], // bSquare3 
       [[Piece alloc] pieceWithType:1 player:2 row:0 col:4], // wSphere1 
       [[Piece alloc] pieceWithType:1 player:2 row:2 col:4], // wSphere2 
       [[Piece alloc] pieceWithType:2 player:2 row:1 col:4], // wSquare1 
       [[Piece alloc] pieceWithType:2 player:2 row:3 col:4], // wSquare2 
       [[Piece alloc] pieceWithType:2 player:2 row:2 col:5], // wSquare3 
       nil]; 

    // add background before pieces 
    CCSprite *bg = [CCSprite spriteWithFile:@"grid.png"]; 
    [bg setPosition:ccp(240, 160)]; 
    [self addChild:bg z:0]; 

    // add all the pieces 
    for(Piece *piece in pieces) { 
     [self addChild:piece]; 
    } 

} 

} 
+1

你在哪裏發佈Piece對象? – LearnCocos2D 2012-08-12 09:19:39

+0

由於@ LearnCocos2D提到,在重置方法中沒有用於釋放先前塊的代碼。 (即用清理從父層刪除舊的子件) – giorashc 2012-08-13 06:21:36

回答

0

如果你要使用層/ CCScene超過1輪(它不是由像菜單的另一箇中間CCScene替代),那麼你應該嘗試添加/外去除精靈初始化函數

,你可以這樣做

- (void)resetLevel: { 

//remove old children 
[this removeAllChildrenWithCleanup:YES]; 

//Add new children 
pieces = [[NSMutableArray alloc] initWithObjects: 
    [[Piece alloc] pieceWithType:1 player:1 row:1 col:3], // bSphere1 
    ....... 
    nil]; 

    // add background before pieces 
    CCSprite *bg = [CCSprite spriteWithFile:@"grid.png"]; 
    [bg setPosition:ccp(240, 160)]; 
[self addChild:bg z:0]; 

    // add all the pieces 
    for(Piece *piece in pieces) { 
     [self addChild:piece]; 
    } 

}

的代碼沒有經過測試,你可能需要做一些改變

你還應該避免保留NsMutableArray和Piece實例分配(alloc),因爲你必須做額外的工作來釋放它們。你的圖層通過添加它們來保留它們

+0

在刪除childrenwithcleanup中,你的「this」是什麼? – josephhaggerty 2012-08-14 03:12:25

+0

抱歉,我的意思是'自我' – yannicuLar 2012-08-14 06:24:40