0
votes

I've a simple sprite-kit game where I run an action (move one step) on a parent node:

SKSpriteNode *firstNode = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(100, 100)];
firstNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:firstNode.size];
firstNode.physicsBody.dynamic = YES;
firstNode.position = CGPointMake(100, 100);
[self addChild:firstNode];

SKSpriteNode *secondNode = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(100, 100)];
secondNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:secondBlock.size];
secondNode.physicsBody.dynamic = YES;
secondNode.position = CGPointMake(200, 200);
[self addChild:secondNode];

SKSpriteNode *parentNode = [SKSpriteNode node];

[parentNode addChild:firstNode];
[parentNode addChild:secondNode];

SKAction *moveOneStep = [SKAction moveBy:CGVectorMake(100,  0) duration:.1];

[parentNode runAction:moveOneStep];

but adding the firstNode as a child to parentNode causes app crash !!

Could anyone please tell me what is wrong with my code ?

Thanks,

1
If I am not wrong you have asked same question yesterday. You can edit your question instead of asking new one or you can delete old one before asking same one. - modus
Sorry, I'me new to StackOverflow Since old questions where not clear I deleted them - Omar Albeik
Reading HELP section would help to improve your SO experience a lot. - modus

1 Answers

0
votes

Read the error messages in the Xcode Console. It will say something along the lines of "tried to add node with existing parent".

Breaking it down to the relevant code, you're doing this, which is illegal:

[self addChild:firstNode];
[self addChild:secondNode];

[parentNode addChild:firstNode];
[parentNode addChild:secondNode];

A node can't be the child of two parents. You have to decide whether they should be children of self or parentNode.