我有以下代码将"敌人"移动到我的"玩家"位置。
let follow = SKAction.moveTo(player.position, duration: 2)
enemy.runAction(SKAction.repeatActionForever(action), withKey: "moving")
此代码运行良好。然而,当我移动玩家时,我希望SKAction"再次运行",这样敌人总是朝着玩家所在的最后一个位置移动。因此,如果玩家不继续移动,他们最终会被抓住。
为什么"repeatActionForever"不起作用?敌人移动到玩家的初始位置,但当你将玩家移动到新位置时,敌人不会移动到该新位置。
感谢:D
repeatActionForever
正在工作,它不是动态的,它只会在创建动作时移动到玩家,它不知道有什么变化。
你需要为此做runBlock
:
let follow =
SKAction.sequence(
[
SKAction.runBlock(
{
//this will allow the enemy to constantly follow the player
enemy.runAction(SKAction.moveTo(player.position, duration: 2), withKey:"move")
}),
SKAction.waitForDuration(0.1)
]
)
enemy.runAction(SKAction.repeatActionForever(follow), withKey: "moving")
现在这种方法的问题是你的敌人速度会根据玩家的距离而变化。相反,你想这样做:
//duration is expected time to reach the player
let follow = SKAction.customActionWithDuration(duration,
actionBlock:
{
node,elapsedTime in
//change the nodes velocity
//this formula is dependent on gameplay
})
enemy.runAction(SKAction.repeatActionForever(follow), withKey: "moving")