对于我在Objective C Sprite Kit框架中开发的游戏,我的滚动背景(WAVE)存在问题。我正在使用书中的方法' iOS游戏教程' (强烈推荐 - 信息丰富的书)作为指导,因为我是新手。
我把一切都搞好了,但是在关闭游戏之后......我的iPhone已经坐了大约20-30分钟 - 我回到游戏中并且滚动BG空白5-15秒然后它加载和滚动。奇怪的是,当我第一次测试游戏时 - 它立即加载没有问题。我也有节点/图形,每次都没有问题,它们都会快速加载。
以下是每帧更新的主要代码段:
- (void)moveWave
{
CGPoint waveVelocity = CGPointMake(-wave_speed, 0);
CGPoint amtToMove = CGPointMultiplyScalar(waveVelocity, _dt);
_waveLayer.position = CGPointAdd(_waveLayer.position, amtToMove);
[_waveLayer enumerateChildNodesWithName:@"wave"
usingBlock:^(SKNode *node, BOOL *stop){
SKSpriteNode * wave = (SKSpriteNode *) node;
CGPoint waveScreenPos = [_waveLayer convertPoint:wave.position
toNode:self];
if (waveScreenPos.x <= -wave.size.width) {
wave.position = CGPointMake(wave.position.x+wave.size.width*2,
wave.position.y);
}
}];
}
任何帮助或洞察为什么回到iPhone闲置20分钟然后重新加载游戏,只有滚动的BG丢失但是在5-15秒之后,它开始并且看起来很好。再次,当我第一次加载..或退出5分钟然后回到游戏..一切正常,并立即加载。
编辑 - 我知道它是滚动功能/方法,因为我试图只是加载BG图像而不滚动它甚至在我退出游戏20多分钟并且回来之后加载也很好。嗯..即使滚动的BG没有加载,我也能玩游戏。滚动BG总是最终加载 - 通常是5-10秒后。
答案 0 :(得分:1)
将以下代码添加到1)当用户按下主页按钮时暂停游戏; 2)当用户点击应用程序的图标时恢复游戏。
// Register to be notified when the app is about to change to background state
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
在应用进入后台模式之前调用此方法。
- (void) applicationWillResignActive:(NSNotification *)notification
{
// Pause the game
self.scene.view.paused = YES;
UIApplication *app = [UIApplication sharedApplication];
// Register to be notified when app is about to become the foreground app
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForegroundOrDidBecomeActive:)
name:UIApplicationWillEnterForegroundNotification
object:app];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillResignActiveNotification
object:app];
}
在应用程序进入前台模式之前调用此方法。
- (void) applicationWillEnterForegroundOrDidBecomeActive:(NSNotification *)notification
{
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillEnterForegroundNotification
object:app];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
// Add tap recognizer
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.delegate = (id)self;
[self.view addGestureRecognizer:tapGesture];
}
当用户点击屏幕时,此方法将恢复游戏。
- (void) handleTapGesture:(UITapGestureRecognizer *)gesture
{
// Remove the tap recognizer
[self.view removeGestureRecognizer:gesture];
// Resume the game
self.scene.view.paused = NO;
}
答案 1 :(得分:1)
这绝对是增量时间。
我找到了最好的临时/解决方案。
这限制了_dt ......
if (_dt > 1) {
_dt = currentTime - _lastUpdateTime;
}
这对我的目的来说非常有用,因为在实际场景中_dt实际上不应该达到1以上 - 有更深入的方法可以在游戏退出时存储值,恢复...但这是最简单的imo