【问题标题】:SKScene duplicate after a presentViewControllerSKScene 在 presentViewController 之后重复
【发布时间】:2014-03-18 10:26:46
【问题描述】:

我不明白为什么我的不起作用。

用例:

  1. 测试 1: 我启动游戏,从第一个场景点击播放按钮 (presentScene),然后我进入第二个场景。没问题。
  2. 测试 2: 我启动游戏,点击第一个场景中的选项按钮 (presentViewController)。在 Optin 控制器上,我单击以关闭 ViewControllerAnimated。如果我想点击播放按钮,屏幕上没有变化,但游戏很明显。我不明白为什么场景会留在屏幕上,为什么没有过渡。

没有错误输出。

代码控制器:

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showParam) name:kNotificationShowParam object:nil];
    //...
    SKView * skView = (SKView *)self.view;
    SKScene * scene = [[MenuScene alloc ]initWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:scene];
}

- (void)showParam
{
    ParamViewController *ParamView = [[crazyButterflyParamViewController alloc]init];
    [self presentViewController: ParamView animated: YES completion:^ {
    }];
}

代码参数控制器:

//method on button
-(void)goToMenu
{
    [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:^{
    }];
}

代码菜单场景:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch* touch in touches)
    {
        if ([_button containsPoint:[touch locationInNode:_button.parent]])
        {
            SKTransition *transition = [SKTransition fadeWithDuration:0.4];
            SKScene *scene = [[GameScene alloc] initWithSize:self.size];
            scene.scaleMode = SKSceneScaleModeAspectFill;
            [self.scene.view presentScene:scene transition:transition];
            break;
        }


        if ([_buttonParam containsPoint:[touch locationInNode:_buttonParam.parent]])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowParam object:nil];
            break;
        }
    }
}

【问题讨论】:

  • 检查 self.view.window.rootViewController 是否真的是您要关闭的控制器。验证 viewDidLoad 没有被第二次调用,如果是,则表明创建了一个新的 SKView。检查内存泄漏和对象没有在它们应该释放的地方,特别是 SK 视图控制器。这个问题听起来好像您在原始 SKView 仍在内存中时创建了第二个 SKView。
  • 我在viewDidLoad之后加了一个断点,但是只调用了一次。

标签: objective-c sprite-kit skscene


【解决方案1】:

现在好了!

我已将通知替换为来自菜单场景的呼叫:

if ([_buttonParam containsPoint:[touch locationInNode:_buttonParam.parent]])
    {
        //[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowParam object:nil];
        [self.view.window.rootViewController presentViewController:_paramController animated: YES completion:^(void) {
            SKView * skView = (SKView *)self.view;
            MyScene *MenuScene = [[MyScene alloc]initWithSize:skView.bounds.size];
            MenuScene.scaleMode = SKSceneScaleModeAspectFill;
            [skView presentScene:MenuScene];
        }];
        break;
    }

这样,完成后就可以找到原来的控制器了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 2017-09-17
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多