我正在尝试在我目前正在开发的游戏中向游戏层添加多个动画精灵。为此,我创建了一个实例 sprite: bunsen,并且对于需要添加的每个 bunsen,我运行这个循环:
else if (blockValue == 2 || blockValue == 3 || blockValue == 4 || blockValue == 5) {
bunsen = [CCSprite spriteWithSpriteFrameName:@"15_1.png"];
if (blockValue == 2) {
bunsen.rotation = 90;
}
else if (blockValue == 3) {
bunsen.rotation = 180;
}
else if (blockValue == 4) {
bunsen.rotation = 270;
}
float tileY = screenSize.height-((countery*startData4)+startData4/2+7.5)/2;
float tileX = ((counterx*startData4)+startData4/2+5)/2;
bunsen.position = ccp(tileX,tileY);
tileName = [NSString stringWithFormat:@"%i.png",blockValue];
[bunsen runAction:bunsens];
[bunsenAnimation addChild:bunsen];
}
如果一个级别上只有一个本生,这很好用,但是当有多个本生时,只有最后一个被添加的本生是动画的,我假设这是因为所有以前的本生实例都被设置回[CCSprite spriteWithSpriteFrameName:@"15_1.png"];
所以我然后将我的代码更改为:
else if (blockValue == 2 || blockValue == 3 || blockValue == 4 || blockValue == 5) {
CCSprite *bunsen = [CCSprite spriteWithSpriteFrameName:@"15_1.png"];
if (blockValue == 2) {
bunsen.rotation = 90;
}
else if (blockValue == 3) {
bunsen.rotation = 180;
}
else if (blockValue == 4) {
bunsen.rotation = 270;
}
float tileY = screenSize.height-((countery*startData4)+startData4/2+7.5)/2;
float tileX = ((counterx*startData4)+startData4/2+5)/2;
bunsen.position = ccp(tileX,tileY);
tileName = [NSString stringWithFormat:@"%i.png",blockValue];
[bunsen runAction:bunsens];
[bunsenAnimation addChild:bunsen];
}
注意这次本生的本地声明,所有这些结果是没有一个精灵变成动画......如果我bunsen = [CCSprite spriteWithSpriteFrameName:@"15_1.png"];
在 else if 循环开始之前移动到,那么我会得到一个错误:已经添加了孩子或其他东西线,所以基本上我需要做的是重新声明本生,这样它就不会在不停止先前添加的本生上的动作“本生”的情况下抛出一个孩子已经添加的错误,但是我想不出任何简单的方法这样做……有人吗?