如何在不停止的情况下将物体移向一个方向 [英] How to move an object towards a direction without stopping

查看:28
本文介绍了如何在不停止的情况下将物体移向一个方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SpriteKit 在 Swift 3 中开发一个小游戏,我想将敌人移动到角色的方向,但在任何时候敌人到达角色的初始位置时都会停止.

I'm developing a small game in Swift 3 with SpriteKit and I want to move the enemies to the direction of the character, but at all times the enemies stop when they arrive at the character's initial position.

我正在这样做:

enemigo.name = "enemigo"
enemigo.position = CGPoint(x: 280, y: 100)
enemigo.xScale = 1/1
enemigo.yScale = 1/15
addChild(enemigo)


let movement1 = CGVector(
    dx: (enemigo.position.x - personaje.position.x)*10,
    dy: (enemigo.position.y - personaje.position.y)*10
)

let actionTransaction = SKAction.move(by: movement1, duration: 20)
enemigo.run(actionTransaction)

如何在不停留在角色初始位置的情况下将敌人移动到特定方向?

How should I move the enemies to a specific direction without stopping at the initial position of the character?

推荐答案

您已经完成了确定方向向量的所有艰苦工作.

You've done all the hard work figuring out the vector of direction.

现在您可以重复此moveBy action 随心所欲,敌人会不断朝同一个方向移动,越走越远,因为 move(by: CGVector) 是相对的,而不是绝对的:

Now you can repeat this moveBy action as often as you like, and the enemy will keep moving in the same direction, further and further, because move(by: CGVector) is relative, not absolute:

let actionTransaction = SKAction.move(by: movement1, duration: 20)

所以你需要做的就是永远运行这个用一个键,这样您就可以从任何地方找到它,并随时停止.

So all you need do is run this forever with a key, so you can then seek it out from anywhere, and stop it whenever you like.

run(SKAction.repeatForever(actionTransaction), withKey: "movingEnemy")

这篇关于如何在不停止的情况下将物体移向一个方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆