Ios SpriteKit添加子视图
我正在尝试使用addSubview实现游戏的暂停菜单。但当我按下暂停按钮时,视图会显示前一个场景DTStartMenu场景的子视图,而不是实际的游戏场景。 以下是ViewController.m中的代码:Ios SpriteKit添加子视图,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我正在尝试使用addSubview实现游戏的暂停菜单。但当我按下暂停按钮时,视图会显示前一个场景DTStartMenu场景的子视图,而不是实际的游戏场景。 以下是ViewController.m中的代码: -(void)viewDidLayoutSubviews{ [super viewDidLoad]; SKView * skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCo
-(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];
}
在startmenus.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];
}
常见问题。不检查场景是否已在视图中设置。试试这个:
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
SKView * skView = (SKView *)self.view;
if (skView.scene == nil)
{
// Setup and present the scene here....
[skView presentScene:scene];
}
}
请注意,我将其从viewDid更改为。。查看将。。并调用正确的超级方法(您的实现称为viewDidLoad)。很有意义。工作。谢谢您!