双击让SpriteKit游戏崩溃

时间:2015-03-21 15:40:16

标签: ios8 sprite-kit skscene

我是SpriteKit游戏开发者的新手。 在我的游戏中,当角色触碰敌人时,它会在场景中显示游戏并重新启动它。

然而,在Game Over Scene显示之后,游戏中的双重使应用程序崩溃。 这是我在MainGameScene中的代码。

- (void)gameOver
{
    GameOver *over = [[GameOver alloc] initWithSize:self.size];
    SKTransition *trans = [SKTransition flipHorizontalWithDuration:0.5];
    [self.view presentScene:over transition:trans];
}

这是GameOver Scene。

- (instancetype)initWithSize:(CGSize)size
{
    self = [super initWithSize:size];
    {
        SKLabelNode *lblGameOver = [[SKLabelNode alloc] initWithFontNamed:@"Chalkduster"];
        lblGameOver.text = @"Game Over";
        lblGameOver.position = CGPointMake(self.size.width/2, self.size.height/2);
        lblGameOver.fontSize = 35;
        lblGameOver.fontColor = [UIColor whiteColor];
        lblGameOver.zPosition = 2;
        [self addChild:lblGameOver];
    }

    return self;
}

- (void)didMoveToView:(SKView *)view
{
    [super didMoveToView:self.view];

    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(newGame)];
    [view addGestureRecognizer:tapper];
}

- (void)newGame
{
    GameScene * scene = [[GameScene alloc] initWithSize:self.size];
    SKTransition *trans = [SKTransition flipHorizontalWithDuration:0.5];
    [self.view presentScene:scene transition:trans];
}

当我在新游戏场景中双击时,这会让我的应用程序崩溃。 错误信息是

Thread 1 : EXC_BAD_ACCESS (code=1,address=0x0)

我怎么能解决它?

1 个答案:

答案 0 :(得分:0)

在didMoveToView方法中尝试:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newGame)];
[self.view addGestureRecognizer:tapRecognizer];
tapRecognizer.numberOfTapsRequired = 2; // 1 for a single tap or 2 for a double tap