simonthumper
simonthumper

Reputation: 1844

Animating Multiple Sprites Cocos2d

I am trying to add multiple animated sprites to the gamelayer in the game I'm currently working on. To do this I have created an instance sprite: bunsen, and for every bunsen that needs to be added I run this loop:

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];

            }

This works fine if there is only one bunsen on a level, however when there is more than one, only the last bunsen to have been added is animated, I'm assuming this is because all previous bunsen instances get set back to [CCSprite spriteWithSpriteFrameName:@"15_1.png"]; so I then change my code to:

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];

            }

N.B the local declaration of bunsen this time, all this results in is none of the sprites becoming animated... and if I move the bunsen = [CCSprite spriteWithSpriteFrameName:@"15_1.png"]; to before the start of the else if loop then I get an error: child already added or something along those lines, so basically what I need to be able to do is redeclare the bunsen so it doesn't throw a child already added error without stopping the action 'bunsens' on the previously added bunsen, however I can't think of any easy way of doing this... anybody?

Upvotes: 2

Views: 300

Answers (1)

Ben Trengrove
Ben Trengrove

Reputation: 8719

You can't run the same action on more than one node. Just change

runAction:bunsens

To

runAction:[bunsens copy]

Upvotes: 1

Related Questions