2012-08-12 67 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