Reputation: 315
In my GameScene.m I have a method called wentToBackGround. This method is called by applicationWillResignActive in AppDelegate:
GameScene *gameScene = [[GameScene alloc]init];
[gameScene wentToBackground];
In wentToBackGround I move my player sprite (just to test if it works) like so:
-(void)wentToBackground {
NSLog(@"BG");
self.player.position = CGPointMake(CGRectGetMidX(self.frame), 1000);
}
The NSLog works, however the players position remains the same. Is this due to the fact that SpriteKit automatically pauses everything once it enters the BG. How do I work around this. I eventually want there to be a pause menu that opens as soon as the user leaves the app. How do I do this properly?
I think it also might not work, because I made a new instance of the GameScene. How do I use the old instance? (the old instance was created in another scene, the TitleScene)
Upvotes: 1
Views: 633
Reputation: 535304
I think it also might not work, because I made a new instance of the GameScene
Correct, you got it! Well done. Keep in mind (and I think you are now grasping this) that a class is just a template. The objects in your app are instances, and you can make many instances of one class. So when you say
[[GameScene alloc]init]
you are making a new instance. That's legal but pointless; sending it the wentToBackground
message does nothing of any use, because it isn't in your interface. The GameScene instance you want to talk to, the one in your interface, is elsewhere.
Getting a reference to a particular existing instance can be tricky. You sometimes have to arrange things in advance to make it possible. However, I think you can avoid the whole problem here. UIApplication has not only an app delegate method but also a notification for letting you know that you will resign active. So just have the GameScene register for that notification, and now you don't have to involve the app delegate at all.
Here's the documentation on that notification:
Any object can register for a notification, so this is a way to establish that your GameScene wants to be informed of deactivation.
Upvotes: 2