动画多个精灵Cocos2d

时间:2012-10-31 00:18:08

标签: objective-c ios animation cocos2d-iphone

我正在尝试将多个动画精灵添加到我正在制作的游戏中的游戏玩家。为此,我创建了一个实例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];

            }

如果一个级别上只有一个bunsen,这可以正常工作,但是当有多个Bunsen时,只有最后一个Bunsen被添加了动画,我假设这是因为所有以前的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声明,所有这一切导致没有精灵变得动画......如果我将bunsen = [CCSprite spriteWithSpriteFrameName:@"15_1.png"];移动到else if循环开始之前那么我得到一个错误:孩子已经添加或者沿着这些线添加了什么,所以基本上我需要能够做的就是重新启用bunsen所以它不会抛出一个孩子已经添加错误而不停止在之前添加的bunsen上的动作'bunsens',但是我想不出任何简单的方法这样做......任何人?

1 个答案:

答案 0 :(得分:1)

您无法在多个节点上运行相同的操作。只需改变

runAction:bunsens

runAction:[bunsens copy]