Swift在某些位置处理触摸 - GameScene类

时间:2015-01-30 20:24:37

标签: ios swift

我想检测在屏幕的某些区域中进行的触摸,并为已触摸的每个区域关联确定的动作。

类似的东西:

3 areas that are touchable, and each one has a different action

到目前为止,我的代码是:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        attackInTouch(heroAtlas) //function that gives me the animation of "attack"

    }
}

通常情况下,英雄正在运行,并且在每次触摸时他都会“攻击”(显示动画),但我想声明区域来处理触摸位置,然后激活相应的动作

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您必须创建一些代表您所在区域的SKSpriteNodes。之后,您可以检查用户是否按下了指定的节点。

首先,您必须设置SKSpritenodes的名称:

yourNode.name =“fire”

之后,如果您的节点被触摸,您可以检查touchesBegan方法:

let location = touch.locationInNode(self)
let node:SKNode = self.nodeAtPoint(location)

if(node.name == "specialNode"){
  //do something with your specialnode
}else if(node.name == "fire"){
  //do something with your fire-node
}