问题
I found the this answer on Stack overflow, but I want to make a modification to it that I just can't figure out. The answer to that question shows how to make the missile move in the direction of your finger. However, I want it to move in the direction of another node (in my case a ship) Meaning that instead of following my finger, it follows the ship. How can a achieve this?
Thanks very much
回答1:
It is very simple:
1) Instead of touchesMoved use update method.
2) Instead of touch location use player's position.
override func update(currentTime: NSTimeInterval) {
let location = player.position
//Aim
let dx = location.x - missile.position.x
let dy = location.y - missile.position.y
let angle = atan2(dy, dx)
missile.zRotation = angle
//Seek
let vx = cos(angle) * missileSpeed
let vy = sin(angle) * missileSpeed
missile.position.x += vx
missile.position.y += vy
}
来源:https://stackoverflow.com/questions/36684255/how-to-move-node-in-the-direction-of-another-node