在多个类中添加touchesBegan

时间:2014-11-30 21:46:57

标签: ios xcode swift

是否可以在项目的多个位置或对象中使用touchesBegan方法?

例如,如果我有一个带有两个按钮的主类:

class GameScene: SKScene, SKPhysicsContactDelegate {
 let moveButton = MoveButton()
 let fireButton = FireButton()
 //add buttons to scene etc...
}

class MoveButton: SKSpriteNode {
 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  NSLog("moved!")
 }
}

class FireButton: SKSpriteNode {
 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  NSLog("fired!")
 }
}

目前我有类似的东西,但触摸开始没被击中,这是不正确的吗?

1 个答案:

答案 0 :(得分:1)

是的,在几个地方实施touchesBegan应该完全没问题。关于他们为什么不被召唤,或许这可以帮助? Custom SKSpriteNode not detected during touch