【发布时间】:2014-11-25 10:33:55
【问题描述】:
我正在尝试实现一个手势识别器,一切正常,直到加载下一个级别,然后在下一个级别阶段,可以点击进入下一个级别,如果我使用手势,程序崩溃。
需要明确的是,当一个级别完成时,会出现一个标题,上面写着“下一个级别 - 触摸以进入下一个级别”。在我添加手势识别器之前,轻按会导致级别增加并使用 [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");
}
}
问题可能出在我增加关卡或实现触摸的方式上,所以这里是 touches started 方法和关卡增量代码
-(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;
}
}
任何帮助将不胜感激。
【问题讨论】:
-
来自documentation
If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations. -
感谢@Mateusz 的回复,您能否详细说明您认为这意味着什么。我不太明白。当呈现下一级视图时发生崩溃,这涉及调用超级。
-
对不起,我不清楚。我的建议可能无法解决您的问题。如果您使用其他触摸事件,我只想注意它们可能不起作用。
标签: objective-c uigesturerecognizer levels