I have an issue with a demo game I'm working on. All I'm trying to do is to pause the game when it enters the background and restart it when it becomes active. I've tried the solution from this link https://stackoverflow.com/questions/19014012/sprite-kit-the-right-way-to-multitask
1: SpriteKit- the right way to multitask and this link SpriteKit - how to correctly pause and resume app
and a bunch of other ones, but I had no success with any of them. I've even tried the following in app delegate:
#import "AppDelegate.h"
#import <SpriteKit/SpriteKit.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
// pause sprite kit
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// resume sprite kit
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
and nothing happens. I pause and unpause in my scene as follow:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:_bgLayer];
[self moveZombieToward:touchLocation];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if (node == _pauseButton) {
[_pauseButton setTexture:[SKTexture textureWithImageNamed:@"pausebutton"]];
_pauseButtonPressed = YES;
_playButtonPressed = NO;
self.scene.paused = YES;
}
if (node == _playButton) {
[_playButton setTexture:[SKTexture textureWithImageNamed:@"playButton"]];
_playButtonPressed = YES;
_pauseButtonPressed = NO;
self.scene.paused = NO;
}
}
edit:
- (void)moveZombieToward:(CGPoint)location
{
[self startZombieAnimation];
_lastTouchLocation = location;
CGPoint offset = CGPointSubtract(location, _zombie.position);
CGPoint direction = CGPointNormalize(offset);
_velocity = CGPointMultiplyScalar(direction, ZOMBIE_MOVE_POINTS_PER_SEC);
}
- (void)update:(NSTimeInterval)currentTime
{
if (_lastUpdateTime) {
_dt = currentTime - _lastUpdateTime;
} else {
_dt = 0;
}
_lastUpdateTime = currentTime;
if (_pauseButtonPressed) {
_lastUpdateTime = 0;
return;
}