Art*_*zak 7 view objective-c ios sprite-kit skview
我的最新游戏是iPhone制作的,这是由Games by Tutorials帮助制作的.
注意:来自SpriteKit的方法 - 正确的多任务方式不起作用.
所以,在我的ViewController.m文件中,我正在定义私有变量SKView*_skView.
然后,我做这样的事情:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(!_skView)
{
_skView = [[SKView alloc] initWithFrame:self.view.bounds];
_skView.showsFPS = NO;
_skView.showsNodeCount = NO;
// Create and configure the scene.
SKScene * scene = [MainMenuScene sceneWithSize:_skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[_skView presentScene:scene];
[self.view addSubview:_skView];
}
}
Run Code Online (Sandbox Code Playgroud)
我已经定义了_skView,一切正常.
但是,当我打断游戏时,它会将其状态重置为初始状态,因此,例如,如果我正在玩,有人打电话给我,游戏会切换回主菜单.它不可能是这样的.
基于我上面提到的网站,我创建了这个:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
但是一旦启动游戏就会崩溃,因为第二种方法被调用而SKView*视图为零.从self.window.rootViewController.view获取它不起作用.
我也尝试从self.window.rootViewController.view.subviews获取它,但它也不起作用.
我不能像这样定义我的SKView(在ViewController.m中):
SKView * skView = (SKView *)self.view;
Run Code Online (Sandbox Code Playgroud)
因为那时我的GameCenterController出错了.
有人可以帮我正确获取实际的skView并正确暂停吗?
jer*_*e10 10
您可以在管理SKView的ViewController中自己订阅这些通知.然后,您不必导航一些奇怪的层次结构来获取它.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
- (void)willEnterForeground {
self.skView.paused = NO;
}
- (void)willEnterBackground {
// pause it and remember to resume the animation when we enter the foreground
self.skView.paused = YES;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1378 次 |
| 最近记录: |