12

I'm looking for a way to get the current scene so that I'll be able to tell which scene is running at any time.

Thanks!

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
jkigel
  • 1,592
  • 6
  • 28
  • 49

1 Answers1

29

Check out CCDirector. You can get the running scene like this:

[[CCDirector sharedDirector] runningScene];

From the documentation of cocos2D:

-(CCScene*) runningScene [read, assign]

The current running Scene. Director can only run one Scene at the time

Sandro Meier

Community
  • 1
  • 1
Sandro Meier
  • 3,071
  • 2
  • 32
  • 47
  • Thanks, How can I compare the runningScene to other scene to check if they are the same? – jkigel Apr 22 '11 at 18:16
  • guess they are both pointers you can check if two pointer are the same or not! – Ali1S232 Apr 22 '11 at 20:57
  • Yes do it like Gajet said. Just compare the pointer by using the == operator. if (yourScene == [[CCDirector sharedDirector] runningScene]) {Do something...} – Sandro Meier Apr 23 '11 at 09:06
  • 1
    Thanks, but - if ([[CCDirector sharedDirector] runningScene] == [MenuScene scene]) { NSLog(@"MenuScene"); } - it doesn't work – jkigel Apr 23 '11 at 10:09
  • Yes that doesn't work. You need to compare with the same instance. If you use [MenuScene scene] you create a new instance and compare with the new one. Compare it directly with the one you create when you presented the scene. ;-) – Sandro Meier Apr 23 '11 at 10:55
  • First of all thank you for your patience, I will explain my self deeply. In the applicationWillResignActive, I want to display an pause scene any time the method being called, but, I want to display the pause scene only when the running scene isn't the MenuScene, so I didn't create any instance of MenuScene before. – jkigel Apr 23 '11 at 11:51
  • 4
    No Problem. You're welcome. ;-) In this case you could compare the class. [[[CCDirectory sharedDirector] runningScene] isKindOfClass:[MenuScene class]] So it should work. ;-) – Sandro Meier Apr 23 '11 at 12:37