0
votes

Is there anyway I can animate a child node independent of it's parent?

For example

//create parent node
SKNode* parent = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(100,100)];
[myScene addChild:parent];

//create child node (add to parent)
SKNode* child = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(50,50)];
child.position = CGPointMake(0,0);
[parent addChild:child];

//attempt to run action on child
[child runAction:[SKAction moveByX:25 y:25 duration:1.0f]];

Nothing happens. However, if I run the same action on the parent, it works as expected.

How can I run an action on a child nodes independent of the parent? This must be possible (e.g. wings flapping on a bird, tires turning on a car, landing gear deploy on an airplane, etc.).

If this is a limitation of sprite kit, can you recommend another game engine framework that does not have these limitations?

The ONLY way I am able to reposition a node is if it is directly added to a scene. Child nodes cannot be repositioned. This is a huge limitation in this framework. Very disappointing for sure.

UPDATE:

I discovered that if I remove the physics body (child.physicsBody = nil) I can change the position (child.position = newPosition) but I cannot run an action that changes the position ([child runAction: [SKAction moveByX:25 y:25 duration:1.0f]).

Thanks!!

1
Try setting a different color for the child node to verify this.ZeMoon
Yes, I can set the child node to a different color. Additionally, actions like [SKAction hide] works fine.user3335999
So, did u see the action working after changing the color of the child node?ZeMoon
No it didn't work. I am able to change the color, hide the node, etc. However, I am not able to move the node, either by setting its position or by executing an action to move the node. That child node cannot be moved. No errors, no exceptions. Any attempt to reposition that child node results in a no-op.user3335999
Your code won't compile because you're 1) mixing SKNode and SKSpriteNode and 2) missing ]'s in your UIColor method calls. SpriteKit fully supports running actions on children nodes. Your parent node is a child of the scene.0x141E

1 Answers

0
votes

Solution:

If I run ANY action to the child node before I add it to the parent, I can subsequently run actions that change the position of the child node.

For example:

[child runAction:[SKAction waitForDuration:0.0]];

[parent addChild:child];

//some time later...
[child runAction:[SKAction moveByX:25 y:25 duration:1.0f]];

I have no idea why this works, but it does. If I do NOT run an action on the child node before I add it to the parent subsequent actions that attempt to move the node do not work.