0

我有两个 SKScene SceneASceneB.

我在每个场景中实现了两种方法( willMoveFromView, )。didMoveToView

假设,我目前在SceneA并且我想SceneB通过这个代码块来展示:

SceneB *sceneB  = [SceneB sceneWithSize:self.view.bounds.size];
sceneB.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:sceneB];

当我呈现这个场景时,我可以看到:

sceneA willMoveFromView
sceneB didMoveToView
sceneA init
sceneB willMoveFromView
sceneA didMoveToView

最后我回到第一个SceneA!没有其他代码可以SceneASceneB. 我不知道该怎么办,请帮忙

更新:

正如@LearnCocos2D 所说,viewWillLayoutSubviews每次我呈现场景时都会强制viewWillLayoutSubviews使用以下代码呈现场景A

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView* skView = (SKView*)self.view;
SceneA *sceneA = [SceneA sceneWithSize:self.view.bounds.size];
sceneA.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:sceneA];
}
4

1 回答 1

0

正如@LearnCocos2D 所说, viewWillLayoutSubviews被反复强制,并且我曾经在每次调用它时都创建新场景。所以我需要检查 SKView.scene 属性是否不为零。这是代码:

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView* skView = (SKView*)self.view;
if(!skView.scene){
SceneA *sceneA = [SceneA sceneWithSize:self.view.bounds.size];
sceneA.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:sceneA];
}
}
于 2014-05-02T17:13:28.183 回答