首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >雪碧-试剂盒:检测左右触摸

雪碧-试剂盒:检测左右触摸
EN

Stack Overflow用户
提问于 2015-08-22 10:06:15
回答 1查看 822关注 0票数 4

在我试图构建的新游戏中,我想知道用户在什么时候触摸了()--屏幕的右边和左边--来做一些逻辑工作。

我的代码:

代码语言:javascript
复制
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..
        }
    }
}

控制台:

代码语言:javascript
复制
Right
Left

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-22 10:45:47

这样做;

代码语言:javascript
复制
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..
   }

}

更新:SWIFT2.2

对于SKView:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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..
        }
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32154859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档