问题描述
我正在尝试实现一个手势识别器并且一切正常,直到加载下一个级别,然后在下一个级别阶段,如果我使用手势,程序可以点击进入下一个级别崩溃.
I am trying to implement a gesture recogniser and everything is working well until it comes to loading the next level, then at the next level stage, where can tap to go to the next level, if I use a gesture, the program crashes.
明确地说,当一个级别完成时,会出现一个标题,上面写着下一个级别 - 触摸以进入下一个级别".在我添加手势识别器之前,点击将导致级别增加并使用 [super initWithSize:size] 和基于级别编号的新变量呈现相同的场景.当我添加手势识别器时,当屏幕上显示下一级 - 触摸进入下一级"时,轻按仍会将我带到下一级,但平底锅会使应用程序崩溃.
To be clear, when a level is complete a title appears saying "Next Level - touch to go to next level". Until I added the gesture recogniser a tap would result in incrementing the level and presenting the same scene using [super initWithSize:size] with new variables based on the level number. When I added the gesture recogniser, when "Next Level - touch to go to next level" is on screen, a tap will still take me to the next level but a pan crashes the app.
我的手势识别器在下面.
My gesture recogniser is below.
- (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");
}
}
问题可能出在我增加级别或实现触摸的方式上,所以这里是触摸开始方法和级别增加代码
The problem may be in the way I am incrementing levels or implementing touch so here is the touches began method and the level increment code
-(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;
}
}
任何帮助将不胜感激.
推荐答案
当您过渡到新场景时,手势识别器不会自动从 SKView
中删除.您将需要手动删除它们.您的平移手势识别器配置为调用 [self handlePanGesture];
.然而,当你过渡到新场景时,self
被释放了.当您尝试在新场景中平移时,会调用旧的、已发布的识别器处理程序,从而导致崩溃.要解决此问题,您需要从视图中删除识别器.以下是如何执行此操作的示例:
Gesture recognizers aren't automatically removed from the SKView
when you transition to a new scene. You will need to remove them manually. Your pan gesture recognizer is configured to call [self handlePanGesture];
. However, self
was released when you transitioned to the new scene. When you attempt to pan in the new scene, the old, released recognizer handler is called, causing the crash. To remedy this, you will need to remove the recognizers from the view. Here's an example of how to do that:
// 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];
}
}
这篇关于手势识别器在下一级崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!