I'm pretty new working with SpriteKit. I have a working example of an game I want to develop. I have some time based movements of sprites, with these I mean:
SKSpriteNode * track = (SKSpriteNode *) node;
CGPoint trackVelocity = CGPointMake(0, -objectVelocity);
CGPoint amtToMove = CGPointMultiplyScalar(trackVelocity,_dt);
track.position = CGPointAdd(track.position, amtToMove);
where "_dt" is:
-(void)update:(CFTimeInterval)currentTime {
if (_lastUpdateTime)
{
_dt = currentTime - _lastUpdateTime;
}
else
{
_dt = 0;
}
_lastUpdateTime = currentTime;
}
The problem I'm having is that when the user goes to background and comes back to the app a long time later that _dt is HUGE, so all the sprites that are moved with the _dt variable are gone from screen and never come back... I can't find a way of setting this _dt to a correct value.
How can I achieve this?
Thanks a lot!!
I already have in the AppDelegate the following added:
- (void)applicationWillResignActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
}
And it pauses the game, but the _dt
that is set in the update is still being changed to a HUGE number and all my sprites go out of screen.
Here is the output of when going to background and coming back: NSLog(@"%f %f",_lastUpdateTime,_dt);
2014-06-30 08:34:00.988 TestingSpriteKit[26303:60b] 224490.130906 0.033162
2014-06-30 08:34:19.761 TestingSpriteKit[26303:60b] 224508.904119 18.773212
2014-06-30 08:34:19.804 TestingSpriteKit[26303:60b] 224508.947477 0.043359