SpriteKit添加子视图

时间:2014-06-07 11:21:46

标签: ios objective-c sprite-kit

我正在尝试使用addSubview为我的游戏实现暂停菜单。但是当我触摸暂停按钮时,视图会显示前一个场景中的子视图 - DTStartMenu场景,而不是实际游戏场景。 以下是ViewController.m中的代码:

-(void)viewDidLayoutSubviews{
    [super viewDidLoad];
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [DTStartMenu sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}
StartMenu.m中的代码

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    SKView * skView = (SKView *)self.view;
    SKScene *gameScene = [DTGame sceneWithSize:skView.bounds.size];
    gameScene.scaleMode = SKSceneScaleModeAspectFill;

   // [self.view presentScene:gameScene transition:[SKTransition fadeWithDuration:1]];
    [self.view presentScene:gameScene];
}

DTGame.m中的代码

-(void) didMoveToView:(SKView *)view {

    skview = (SKView*)self.view;

}
-(void)pauseGame{
    CGRect rect = CGRectMake(90, 40, 300, 200);
    pauseMenu = [[DTPauseMenu alloc] initWithFrame:rect];

    [skview addSubview:pauseMenu];
}

1 个答案:

答案 0 :(得分:0)

常见问题。您不检查视图中是否已设置场景。试试这个:

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView * skView = (SKView *)self.view;
    if (skView.scene == nil)
    {
        // Setup and present the scene here....
        [skView presentScene:scene];
    }
}

请注意,我将它从viewDid ..更改为viewWill ..并且还调用了正确的超级方法(您的实现称为viewDidLoad)。