我必须按leftbutton, rightbutton
他们都是SKSpriteNode()
。 当用户触摸其中一个按钮时,只要用户持续触摸,就会有一小船左右移动。如何不断快速获取移动物体的x位置?
现在我需要给我的ship.position.x
整个时间的函数或其他任何东西。我坚持要让它不断打印位置。每次触摸按钮时我都可以打印,但只打印一次。
在我didMove
我只创建了按钮和船舶。所以它应该是无关紧要的。
func moveShip (moveBy: CGFloat, forTheKey: String) {
let moveAction = SKAction.moveBy(x: moveBy, y: 0, duration: 0.09)
let repeatForEver = SKAction.repeatForever(moveAction)
let movingSequence = SKAction.sequence([moveAction, repeatForEver])
ship.run(movingSequence, withKey: forTheKey)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\(ship.position.x)")
for touch: AnyObject in touches {
let pointTouched = touch.location(in: self)
if leftButton.contains(pointTouched) {
// !! I MAKE IT PRINT THE POSITION HERE !!
moveShip(moveBy: -30, forTheKey: "leftButton")
}
else if rightButton.contains(pointTouched) {
moveShip(moveBy: 30, forTheKey: "rightButton")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first{
let pos = touch.location(in: self)
let node = self.atPoint(pos)
if node == aButton {
} else {
ship.removeAction(forKey: "leftButton")
ship.removeAction(forKey: "rightButton")
}
}
}
在我的代码,该位置仅在触摸的开始打印一次,但不打印,直到你再次释放触摸触摸它。有没有可能的方式来做到这一点与我的代码?
您可以覆盖更新方法。在这种方法中,您应该能够获得您的对象并获得位置! –
我读了很多关于这种更新方法。我如何覆盖它?当我尝试,没有更新方法建议xCode –
看看Bharath的答案! –