How to move node in the direction of another node

泪湿孤枕 提交于 2020-01-03 03:29:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!