Cocos-2d iphone重置游戏板
我正在做一个棋盘游戏,当一个人赢了,我想让棋盘自行复位。如果有帮助的话,我正在为iPhone使用cocos2d。我有一个重置方法,重置所有变量和片段数组。它会重置一次,然后在下一次一人获胜后,它不会重置棋盘。有没有办法解决这个问题? 下面是.m文件中的方法。 //方法Cocos-2d iphone重置游戏板,iphone,objective-c,arrays,variables,cocos2d-iphone,Iphone,Objective C,Arrays,Variables,Cocos2d Iphone,我正在做一个棋盘游戏,当一个人赢了,我想让棋盘自行复位。如果有帮助的话,我正在为iPhone使用cocos2d。我有一个重置方法,重置所有变量和片段数组。它会重置一次,然后在下一次一人获胜后,它不会重置棋盘。有没有办法解决这个问题? 下面是.m文件中的方法。 //方法 如果要使用该层/CCScene超过1轮(它不会被另一个中间CCScene(如菜单)替换),则应尝试在init函数之外添加/删除精灵 你可以这样做 - (void)resetLevel: { //remove old childr
如果要使用该层/CCScene超过1轮(它不会被另一个中间CCScene(如菜单)替换),则应尝试在init函数之外添加/删除精灵 你可以这样做
- (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和工件实例分配(alloc),因为您必须做额外的工作来释放它们。您的图层通过添加它们来保留它们在哪里释放工件对象?正如@LearnCos2D所述,在重置方法中没有释放先前工件的代码。(即,使用cleanup从父层中删除旧的子块)在remove childrenwithcleanup中您的“this”是什么?很抱歉,我的意思是“self”
- (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];
}