Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I place a children sprite below it's parent using zPosition

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;
} 
like image 951
PeterK Avatar asked Oct 04 '13 14:10

PeterK


2 Answers

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.

like image 80
Tony Avatar answered Oct 17 '22 03:10

Tony


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.

like image 9
LearnCocos2D Avatar answered Oct 17 '22 03:10

LearnCocos2D