禁用除按钮内部以外的任何地方的触摸? - 斯威夫特

时间:2016-06-26 06:00:21

标签: ios swift sprite-kit touch skspritenode

当我在游戏中死亡时,如果用户点击重置游戏按钮内部或上方,我想忽略用户的所有触摸事件。这是我的代码。

  for touch in touches{
        let location = touch.locationInNode(self)

        if died == true{
            Ball.physicsBody?.velocity = CGVectorMake(0, 0)
            if resetGame.containsPoint(location){

                restartScene()
                runAction(SKAction.playSoundFileNamed("Woosh.wav", waitForCompletion: false))

            }

            else  {


                self.userInteractionEnabled = false



            }

这完全在我的触摸开始之内。这是我试图忽略用户的触摸,除非触摸的位置在按钮的大小范围内。除按钮外,如何忽略屏幕上任何位置的用户触摸? resetGame是一个SKSpriteNode图像。

1 个答案:

答案 0 :(得分:1)

您的问题有两种解决方案。

我想向你提出的第一个案例是基于手势识别器。 您可以将按钮与其他触摸事件分开,并通过布尔值var打开/关闭触摸事件,如下所示:

手势识别器:

在班级的全局变量声明部分中:

var tapGesture :UITapGestureRecognizer!
var enableTouches:Bool = true

override func didMoveToView(view: SKView) {
        super.didMoveToView(view)
        self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(GameClass.simpleTap(_:)))
        self.tapGesture.cancelsTouchesInView = false
        view.addGestureRecognizer(tapGesture)
        myBtn.name = "myBtn"
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        return self.enableTouches
}

func simpleTap(sender: UITapGestureRecognizer) {
        print("simple tap")
        if sender.state == .Ended {
           var touchLocation: CGPoint = sender.locationInView(sender.view)
           touchLocation = self.convertPointFromView(touchLocation)
           let touchedNode = self.nodeAtPoint(touchLocation)
           // do your stuff
           if touchedNode.name == "myBtn" { 
              // button pressed, do your stuff
           }
        }
}

override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
       if died == true { 
            self.enableTouches = false // disable all touches but leave gestures enabled
            //do whatever you want when hero is died
       }
}

仅限布尔值

我想提出的第二个解决方案就是通过使用一个简单的布尔值来停止触摸流(它不是很优雅,但它有效)。 此方法在点击按钮时查看,更新方法检查您的英雄是否已经死亡,因此所有触摸都将被禁用:

在班级的全局变量声明部分中:

var enableTouches:Bool = true

override func didMoveToView(view: SKView) {
            super.didMoveToView(view)
            myBtn.name = "myBtn"
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (!enableTouches) {
            return
        }

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)
        let touchedNode = self.nodeAtPoint(positionInScene)
        // do your stuff
        if touchedNode.name == "myBtn" { 
           // button pressed, do your stuff
           if died == true { 
                self.enableTouches = false // disable all touches
                //do whatever you want when hero is died
           }
        }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (!enableTouches) {
            return
        }

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)
        let touchedNode = self.nodeAtPoint(positionInScene)
        // do your stuff

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (!enableTouches) {
            return
        }
}

第一种情况允许您始终启用手势,这样当您的英雄将会死亡时,您还可以使用手势执行其他操作。第二种情况是当你按下按钮并进行“模具流动”时停止触摸。选择哪一种最适合你。