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

时间:2016-01-13 00:13:55

标签: ios objective-c sprite-kit skspritenode

我已经坚持了这个问题大约一天了,我一直在搜索论坛以及尝试我自己的修复。

我的问题在于在游戏启动时向场景中添加一些SKSpriteNodes。问题是它随机!有时SpriteNodes在其他时候是不可见的,我似乎无法弄清楚为什么。我已经记录了NSlog,并且还注意了节点数,我的所有SpriteNode都加载到场景中,但是他们的可见性问题就是投掷硬币!有时我可以看到其中两个,有时我只能看到其中一个,有时看不到它们。 我正在使用我用来制作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");
}

1 个答案:

答案 0 :(得分:0)

解决问题的一种方法是指定所有精灵的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;

我希望这会对你有所帮助。 :)