SpriteKit 节点在启动时渲染到场景中

SpriteKit nodes rendering into scene late on startup

我已经被这个问题困扰了大约一天,我一直在搜索论坛并尝试自己的修复。

我的问题在于在游戏启动时在背景顶部的场景中添加一些 SKSpriteNode。问题是它是随机的!有时 SpriteNodes 是可见的,有时它们是不可见的,我似乎无法弄清楚为什么。我已经 NSlogged 并注意了节点数,我所有的 SpriteNodes 都加载到场景中,但它们的可见性问题就像掷硬币一样!有时我能看到其中两个,有时我只能看到其中一个,有时 none 个。 我包括了我用来制作 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");
}

以及我暂停和恢复游戏的方法:

- (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");
}

解决问题的一种方法是指定所有精灵的 zPosition。只需添加这些行:

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

另一种方法是将背景添加到 zPosition 为 0.0 的 SKNode,并将所有按钮添加到 zPosition 为 10.0 的另一个节点(我想你几乎做到了)。 .. 你能漏掉这两行吗?

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

希望对您有所帮助。 :)