2014-11-25 75 views
0

我試圖實現手勢識別器,一切都運行良好,直到它加載下一個級別,然後在下一個級別階段,在哪裏可以點擊去下一個級別,如果我使用手勢,程序崩潰。手勢識別器在下一個級別崩潰

需要說明的是,一個關卡完成後,標題會顯示「下一關 - 觸摸進入下一關」。在我添加手勢識別器之前,點擊會導致水平遞增,並使用[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; 
    } 

} 

任何幫助將非常感激。

+0

從[文件](https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIResponder_Class/index.html#//apple_ref/ occ/instm/UIResponder/touchesBegan:withEvent :)'如果您在不調用super(一種常見使用模式)的情況下重寫此方法,那麼您還必須重寫處理觸摸事件的其他方法(如果僅作爲存根(空)實現)。' – Mateusz 2014-11-25 11:42:02

+0

感謝@Mateusz回覆,你能否擴展一下你的想法。我不太明白。當下一層視圖出現時,崩潰正在發生,這涉及到調用超級視圖。 – 2014-11-25 20:06:43

+0

對不起,我不清楚。我的建議可能不能解決您的問題。如果您使用其他觸摸事件,他們可能無法正常工作,我只想提醒您。 – Mateusz 2014-11-26 08:04:04

回答

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]; 
    } 
} 
+0

謝謝!這解決了這個問題。我曾嘗試過在關卡末端移除GestureRecognizer的各種方法,謝謝你給我正確的方法去做。 – 2014-11-29 10:36:11