2014-06-07 102 views
1

我正在嘗試使用addSubview爲我的遊戲實現一個暫停菜單。但是,當我觸摸暫停按鈕時,視圖將在前一場景中顯示子視圖 - DTStartMenu場景,而不是實際的遊戲場景。 這裏是ViewController.m代碼:SpriteKit添加子視圖

-(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]; 
} 

回答

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中)。

+0

有道理。工作。謝謝 ! – mihnea2kx