I am adding a sprite to a parent and it shows up on the screen. However despite my zPosition parameter the child is on top of it's parent. I need to get it as defined in the zPosition.
It will be placed correctly if just adding the sprite to self but not as a child to "hjNode".
The current result is that d5Node, the child, is placed above the hjNode (parent).
The zPosition works among the added child's when adding additional child's.
When reading the programming guide i get the feeling, unless i missed something, that this may be a problem.
Would someone know if this is possible?
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.userInteractionEnabled = YES;
SKSpriteNode *hjNode = [SKSpriteNode spriteNodeWithImageNamed:@"Hj"];
hjNode.position = CGPointMake(150, 300);
hjNode.zPosition = 100;
hjNode.name = @"hjNode";
[self addChild:hjNode];
SKSpriteNode *d5Node = [SKSpriteNode spriteNodeWithImageNamed:@"D5"];
//d5Node.position = CGPointMake(170, 320);
d5Node.position = CGPointMake(-10, -20);
d5Node.zPosition = 1;
d5Node.name = @"d5Node";
[hjNode addChild:d5Node];
}
return self;
}
To add to this, if you want to do it the original way described you would give the children a negative zPosition.
According to the SpriteKit Programming Guide, the zPosition of children are relative to the height of the parent. From the question, d5Node, actually has a zPosition = 101 not 1. Setting the zPosition = -1 will result in a zPosition of 99.
It should be possible, but even if it is not there's a better solution that gives you greater flexibility in the long run.
Instead of adding two child sprites to a sprite node, create a regular SKNode and add all three sprites as children to it. That way you're free to re-arrange them in whatever way you see fit, while all three will follow the position changes of their parent node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With