手势识别器在下一级崩溃

时间:2014-11-25 10:33:55

标签: objective-c uigesturerecognizer levels

我正在尝试实现一个手势识别器,一切都运行良好,直到它加载到下一个级别,然后在下一个级别,可以点击进入下一级别,如果我使用手势,该程序崩溃。

要清楚,当一个关卡完成时,会出现一个标题" Next Level - 触摸以进入下一个级别"。在我添加手势识别器之前,点击将导致增加级别并使用[super initWithSize:size]使用基于级别编号的新变量呈现相同的场景。当我添加手势识别器时,当"下一级 - 触摸进入下一级"在屏幕上,一个水龙头仍然会把我带到一个新的水平,但一个平底锅崩溃的应用程序。

我的手势识别器位于下方。

- (void)didMoveToView:(SKView*)view {

    UIGestureRecognizer *spinner = [[UIPanGestureRecognizer alloc] initWithTarget:self     action:@selector(handlePanGesture:)];
    [self.view addGestureRecognizer:spinner];

}



- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {

    CGPoint velocity = [gestureRecognizer velocityInView:self.view];

    if (velocity.y > 0) {

            NSLog(@"gesture went down");

    } else {

            NSLog(@"gesture went up");

    }
}

问题可能在于我增加级别或实现触摸的方式,所以这里是触摸开始方法和级别增量代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    BOMPlayerNode *playerNode = (BOMPlayerNode*)[self childNodeWithName:@"Player"];

    if (!self.restart) {

        for (UITouch *touch in touches) {
    CGPoint positionInScene = [touch locationInNode:self];

    int duration = 1.0;

    SKAction * actionMove = [SKAction moveTo:CGPointMake(positionInScene.x, positionInScene.y) duration:duration];
    [playerNode runAction:actionMove];

        }
    } else if ( self.restart && self.nextLevel ) {
        levelCount++;
        for (SKNode *node in [self children]) {
            [node removeFromParent];
        }


        BOMGamePlayScene *scene = [BOMGamePlayScene sceneWithSize:self.view.bounds.size];
        [self.view presentScene:scene];

    } else if ( self.restart && self.tryAgain ) {
        for (SKNode *node in [self children]) {
            [node removeFromParent];
        }


        BOMGamePlayScene *scene = [BOMGamePlayScene sceneWithSize:self.view.bounds.size];
        [self.view presentScene:scene];
    }

}

- (void) update:(NSTimeInterval)currentTime {

    if (GameOverConditions) {

        self.tryAgain = YES;
        self.restart = YES;
        [self performGameOver];

    } else if (NextLevelConditions) {

        self.restart = YES;
        self.nextLevel = YES;
        [self performNextLevel];

    }

}


- (void) performGameOver {
    if (!self.gameOverDisplayed) {
        BOMGameOverNode *gameOver = [BOMGameOverNode gameOverAtPosition:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
        self.gameOverDisplayed = YES;
    }

}

- (void) performNextLevel {
    if (!self.nextLevelDisplayed) {
        BOMNextLevelNode *nextLevel = [BOMNextLevelNode     nextLevelAtPosition:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
        self.nextLevelDisplayed = YES;
    }

}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

转换到新场景时,手势识别器不会自动从SKView中删除。您需要手动删除它们。您的平移手势识别器配置为呼叫[self handlePanGesture];。但是,当您转换到新场景时,self已被释放。当您尝试在新场景中进行平移时,将调用旧的释放的识别器处理程序,从而导致崩溃。要解决此问题,您需要从视图中删除识别器。以下是如何执行此操作的示例:

// Add this to your SKScene subclass. It will automatically be called when you
// transition to a new scene.
- (void) willMoveFromView:(SKView *)view {
    // Remove all gesture recognizers from the view
    for (UIGestureRecognizer *gesture in view.gestureRecognizers) {
        [view removeGestureRecognizer:gesture];
    }
}