0

We have been using enums to organize the zPositions of our sprites. As we started to add SKNodes with several subsprites to our game, the structure quickly began to break down. Some child sprites that were placed on the screen had to have negative values in order to be beneath other nodes with children. These negative values are hard to keep track of in relation to other sprites in separate enums.

Is there a better way to organize the zPosition of sprites (especially those with sub-sprites) than using enums?

dblarons
  • 123
  • 2
  • 11
  • I personally use zPosition with enum and ignoreSiblingOrder turned on and find no problem with it. It works much better than just hard-coding numbers in. I don't quite understand your point about negative values, that shouldn't matter. It appears your problem (and I had this problem at first as well) is that you are not planning ahead of time what the z ordering of your game will be. Instead you are doing it as you make the game which will make things very difficult. Get a solid z ordering system down first, enforce it using an enum and things should become much more clear. – Epic Byte Aug 17 '14 at 19:18
  • 1
    Actually, upon reading more into your issue, it looks like you are using multiple enums to organize your z order. I suggest using a single enum for the z - ordering of your game. – Epic Byte Aug 17 '14 at 19:24
  • @gfrs can you add your second comment as an answer so that I can mark it as correct? – dblarons Aug 26 '14 at 20:21
  • Just did, glad it helped you. – Epic Byte Aug 26 '14 at 20:36

2 Answers2

1

Upon reading more into your issue, it looks like you are using multiple enums to organize your z order. I suggest using a single enum for the z - ordering of your game.

Epic Byte
  • 33,840
  • 12
  • 45
  • 93
  • Specifically, the ignoreSiblingOrder part of your comment in combination with the single enum helped me solve my problem. Had tried each of those things on their own with no luck. – dblarons Aug 30 '14 at 21:52
0

Use layers to organize your scene:

- (instancetype)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        self.view.ignoresSiblingOrder = YES;        
        [self addLayers];
    }
    return self;
}

- (void)addLayers {
    self.backgroundLayer = [SKNode node];
    self.backgroundLayer.zPosition = 100;
    [self addChild:self.backgroundLayer];

    self.playerLayer = [SKNode node];
    self.playerLayer.zPosition = 300;
    [self addChild:self.playerLayer];

    self.enemyLayer = [SKNode node];
    self.enemyLayer.zPosition = 400;
    [self addChild:self.enemyLayer];

    // UI elements must be on top of all nodes on the scene
    self.uiLayer = [SKNode node];
    self.uiLayer.zPosition = 1000;
}

- (void)addBackgrounds {
    SKSpriteNode *backgroundNode1 = [SKSpriteNode spriteNodeWithTexture:[self backgroundTexture1]];
    backgroundNode1.zPosition = 10;
    [self.backgroundLayer addChild:backgroundNode1];

    SKSpriteNode *backgroundNode2 = [SKSpriteNode spriteNodeWithTexture:[self backgroundTexture2]];
    backgroundNode2.zPosition = 20;
    [self.backgroundLayer addChild:backgroundNode2];
}    
.... etc
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • While I do agree that organizing your nodes into layers is important and usually necessary, I believe the OP is having difficulty with the z - ordering, not necessarily the grouping of nodes. For example, the enum he mentions having trouble with is used for the zPosition. You could very easily replace your hard-coded values with an enum and it would become much more readable and maintainable. – Epic Byte Aug 17 '14 at 19:30
  • The idea is to give top level layers `100`, `200`, `300` etc. `zPositions`. The nodes, which are contained in these layers, should get `10`, `20`, `30` `zPositions`. Their child nodes should get `1`, `2`, `3` `zPositions`. This approach will eliminate the problems with overlapping nodes. – Andrey Gordeev Aug 18 '14 at 04:31