0

I have two SKScene SceneA,SceneB.

I have implemented two methods(willMoveFromView,didMoveToView) in each scene.

Assume, I am currently in SceneA and i want to present SceneB via this code block:

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

When I present this scene, I can see this:

sceneA willMoveFromView
sceneB didMoveToView
sceneA init
sceneB willMoveFromView
sceneA didMoveToView

And finally I return to first SceneA ! There is no other code that pushes SceneA from SceneB . I don't know what to do, please help

UPDATE:

As @LearnCocos2D says, viewWillLayoutSubviews is forced every time I present scene And I present sceneA in viewWillLayoutSubviews with following code

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView* skView = (SKView*)self.view;
SceneA *sceneA = [SceneA sceneWithSize:self.view.bounds.size];
sceneA.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:sceneA];
}
Igor Prusyazhnyuk
  • 133
  • 2
  • 14
  • 29
  • 2
    My guess: you implemented presenting the first scene in viewWillLayoutSubviews but you are not testing whether the skView.scene property is nil. Presenting a new scene (as does rotating device) can trigger viewWillLayoutSubviews repeatedly. – CodeSmile May 02 '14 at 16:39
  • @LearnCocos2D you are right! It forces viewWillLayoutSubviews again and again. But i don't know how to fix this, maybe I test not that I must. Please, answer my question and I will accept your answer – Igor Prusyazhnyuk May 02 '14 at 16:46

1 Answers1

0

As @LearnCocos2D says, viewWillLayoutSubviews is forced repeatedly , And I used to create new scene everytime it has been called. So I need to check if SKView.scene property is not nil. Here is the code:

-(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];
}
}
Igor Prusyazhnyuk
  • 133
  • 2
  • 14
  • 29