gpt4 book ai didi

ios - 暂停返回后滚动 BG 丢失(iOS-SpriteKit-iphone)

转载 作者:行者123 更新时间:2023-11-29 10:39:28 26 4
gpt4 key购买 nike

我在 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 秒后。

最佳答案

将以下代码添加到 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;
}

关于ios - 暂停返回后滚动 BG 丢失(iOS-SpriteKit-iphone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25212790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com