【问题标题】:Sprite-kit: detecting left & right touchesSprite-kit:检测左右触摸
【发布时间】:2023-04-01 13:44:01
【问题描述】:

在我尝试构建的新游戏中,我想知道用户何时触摸了屏幕的BOTH左右两侧来执行一些逻辑。

我的代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        var isRight : Bool = false
        var isLeft : Bool = false

        if(location.x < self.size.width/2){
            isLeft = true
            println("Left")
        }

        if(location.x > self.size.width/2){
            //
            isRight = true
            println("Right")
        }
        if (isRight && isLeft){
            println("Both touched")
            // do something..
        }
    }
}

控制台:

Right
Left

我的代码似乎不起作用。我在这里做错了什么?有人有更好的解决方案吗?

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    这样做;

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */
    
       var isRight : Bool = false
       var isLeft : Bool = false
    
       for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)
    
            if(location.x < self.size.width/2){
                isLeft = true
                println("Left")
            }
    
            if(location.x > self.size.width/2){
                //
                isRight = true
                println("Right")
            }
        }
    
       if (isRight && isLeft){
          println("Both touched")
          // do something..
       }
    
    }
    

    更新:Swift 2.2

    对于 SKView:

    class skView:SKView {
    
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            /* Called when a touch begins */
    
            var isRight : Bool = false
            var isLeft : Bool = false
    
            for touch in touches {
                let location = touch.locationInView(self);
    
                if(location.x < self.frame.size.width/2){
                    isLeft = true
                    print("Left")
                }
    
                if(location.x > self.frame.size.width/2){
                    //
                    isRight = true
                    print("Right")
                }
            }
    
            if (isRight && isLeft){
                print("Both touched")
                // do something..
            }
    
        }
    }
    

    对于 SKNode:

    class skNode: SKNode {
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            var isRight : Bool = false
            var isLeft : Bool = false
    
            for touch in touches {
                let location = touch.locationInNode(self);
    
                if(location.x < self.frame.size.width/2){
                    isLeft = true
                    print("Left")
                }
    
                if(location.x > self.frame.size.width/2){
                    //
                    isRight = true
                    print("Right")
                }
            }
    
            if (isRight && isLeft){
                print("Both touched")
                // do something..
            }
        }
    }
    

    【讨论】:

    • 这段代码在哪里;即它所指的“自我”是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多