我必須按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的答案! –