Swift: change speed of a moveTo SKAction

青春壹個敷衍的年華 提交于 2019-12-08 18:50:39

You can't change the speed of an action itself, because SKAction objects are "fire and forget" and reusable — once you pass one to a node's runAction method, what you do to that action instance no longer affects what the node is doing to run the action.

But there are a few other options here.

  1. Change the speed property of the node being animated. You can do this either directly or by running a speedBy or speedTo action. This property is a multiplier that changes how fast that node runs any and all actions attached to it, so you can set it below 1.0 to slow down whatever's going on, or above 1.0 to speed things up.

  2. Control the node more directly with a custom action that periodically runs a block; in that block, you can directly set the node's position, and you can check external state to determine whether to move the mode more or less per time interval.

  3. Control the node's position directly during your scene's (or scene delegate's) update loop, at which point you can control the amount of movement per frame however you like.

Using Physics instead of Actions:

Set a speed to a destination.

Create a joint at your destination, connecting your object to the destination, with damping and frequency of response. You can set this on arrival, or initially. Set it initially, with a strong enough frequency and you won't need to set the initial speed, it'll be "attracted" to the destination strongly enough to do all the animation for you.

At any point in the journey, you can adjust the frequency and impart other forces on the moving object to slow or speed it.

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