1
votes

I've been stuck on this problem for about a day now and I've been scouring the forums as well as trying my own fixes.

My problem lies with adding some SKSpriteNodes to the scene on top of the background when the game is launched. The problem is that its random! Some times the SpriteNodes are visible other times they aren't and I cant seem to figure out why. I've NSlogged and also paid attention to the node count, all of my SpriteNodes load into the scene but the matter of their visibility is a coin toss! Sometimes I can see two of them sometimes I can only see one of them and sometimes none of them. I'm including the method that I'm using to make the SpriteNodes:

- (void) setupMenuButtons
{
    //initialize storyButton texture,size,and position
    SKTexture *storyButtonTexture = [SKTexture textureWithImageNamed:@"storybutton"];
    storyButton = [SKSpriteNode spriteNodeWithTexture:storyButtonTexture size:CGSizeMake(self.frame.size.width/2.84,
                                                                                         self.frame.size.height/4.26)];
    storyButton.position = CGPointMake(self.frame.size.width - (storyButton.size.width/2),self.frame.size.height/1.25 );
    [menuLayer addChild:storyButton];
    NSLog(@"Added story");
    //initialize creditsButton texture,size,and position
    SKTexture *creditsButtonTexture = [SKTexture textureWithImageNamed:@"creditsbutton"];
    creditsButton = [SKSpriteNode spriteNodeWithTexture:creditsButtonTexture size:CGSizeMake(self.frame.size.width/2.84,
                                                                                             self.frame.size.height/4.26)];
    creditsButton.position = CGPointMake(self.frame.size.width - (creditsButton.size.width/2),self.frame.size.height/2 );
    [menuLayer addChild:creditsButton];
    NSLog(@"Added credits");
    //initialize usicButton texture,size,and position
    SKTexture *musicButtonTexture = [SKTexture textureWithImageNamed:@"musicbuttonOFF"];
    musicButton = [SKSpriteNode spriteNodeWithTexture:musicButtonTexture size:CGSizeMake(self.frame.size.width/2.84,
                                                                                         self.frame.size.height/9.84)];
    musicButton.position = CGPointMake(self.frame.size.width - (musicButton.size.width/2),self.frame.size.height/3.45 );

    [menuLayer addChild:musicButton];
       NSLog(@"Added music");
}

and the method inwhich I pause and resume my game:

- (void)pauseGame
{
    self.scene.paused = YES;
    backgroundLayer.paused = YES;
    menuLayer.paused = YES;
    [player pause];
    isPaused = YES;
    NSLog(@"paused");
}

- (void)resumeGame
{
    backgroundLayer.paused = NO;
    menuLayer.paused = NO;
    [player play];
    isPaused = NO;
    NSLog(@"resumed");
}
1

1 Answers

0
votes

One way to fix your problem is to specify the zPosition of all your sprites. Simply add these lines:

background.zPosition = 0.0;
storyButton.zPosition = 10.0;
creditsButton.zPosition = 10.0;
musicButton.zPosition = 10.0;

Another way would be to add the background to a SKNode with a zPosition of 0.0 and all the buttons to another node with a zPosition of 10.0 (which I think you almost did)... Could you be missing these two lines?

backgroundLayer.zPosition = 0.0;
menuLayer.zPosition = 10.0;

I hope this helps you. :)