2016-12-16 53 views
1

我已经使用touchesBegan为我的UIButtons提供功能,并已使用轻击手势为我的主玩家SKSpriteNode提供功能,使其在触发时跳转。点击手势不会发生时触摸特定的位置 - SpriteKit

//代码有关的UIButton触摸

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    //touches began is only used for GUI buttons -> not to affect player 

    for touch: AnyObject in touches { 
     //We get location of the touch 
     let locationOfTouch = touch.location(in: self) 

     if muteButton.contains(locationOfTouch) { //mute the game 
      timer.invalidate() 
      audioPlayer.volume = 0 
     } 

//守则关于自来水

let tap = UITapGestureRecognizer(target: self, action: #selector(GameScene.tapped(gesture:))) 
    tap.cancelsTouchesInView = false 

    self.view!.addGestureRecognizer(tap) 

...... 

func tapped(gesture: UIGestureRecognizer) { //used to make the player jump  
      player.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 60)) 
      player.physicsBody!.affectedByGravity = true */ 

      } 

我的问题是,当我按在restartButton点击手势也将在后面启动的时候,触摸结束。有什么我可以做的吗?

+0

您是否有使用单独的水龙头手势识别器的具体原因?如果用户不触摸按钮,为什么不在“touchesBegan”中包含播放器跳转代码? – nathan

+1

我正在使用不同的水龙头手势,因为我也使用了滑动功能,在这种情况下,如果我使用touchesBegan,则无法区分并识别滑动。 –

+0

当您重新启动场景时,您是否创建并呈现新场景? – Knight0fDragon

回答

2

主要问题是用于检测触摸的两个单独系统(使用手势识别器和使用方法)存在冲突。

一种解决方案是,如果触摸位于其中一个按钮内,则启用和禁用手势识别器。

touchesBegan方法中,如果触摸是按钮内,禁用敲击手势识别器:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let locationOfTouch = touch.location(in: self) 
     if muteButton.contains(locationOfTouch) { 
      // mute action 
      tap.isEnabled = false 
     } 
    } 
} 

然后在touchesEndedtouchesCancelled,重新启用所述手势识别:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    tap.isEnabled = true 
} 

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
    tap.isEnabled = true 
} 

这样,如果触摸位于按钮内部,轻击手势识别器将不会触发。每当任何触摸完成时,我们总是重新启用手势识别器,以防下一次触摸让玩家跳跃。

我已经在一个空的项目中测试过了,它工作。

希望有帮助!祝你好运。

+1

谢谢!它完美的作品 –