【发布时间】:2016-07-03 20:22:28
【问题描述】:
我有几个按钮可以移动主角。 (左,右,跳跃等) 但是,当我一次触摸多个按钮时,前一个按钮就结束了。我的问题是,我如何在触摸期间保持两个触摸都保持活力?例如,这将允许角色同时向前移动和跳跃。我已将 multipleTouchEnabled 设置为 true。我读过使用字典来跟踪触摸会有所帮助,但我似乎无法完全理解实现。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInView(nil)
if location.x < self.size.width / 2 && location.y > self.size.height / 2 {
movingLeft = true
}
if location.x > self.size.width / 2 && location.y > self.size.height / 2 {
movingRight = true
}
if location.x < self.size.width / 2 && location.y < self.size.height / 2 {
jump()
}
if location.x > self.size.width / 2 && location.y < self.size.height / 2 {
jump()
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
movingLeft = false
movingRight = false
}
func jump() {
mainCharacter.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
mainCharacter.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 400))
}
【问题讨论】:
标签: swift sprite-kit touchesbegan