Stop SKSpriteNode from slowing down

…衆ロ難τιáo~ 提交于 2019-12-06 03:38:42

If you apply an impulse to a node's physics body it is only applied once. Think of kicking a ball. Applying a force on the other hand is like a continuos push, like an engine moving a car. Keep in mind that if you continue to apply force, your node will get faster and faster so you will have to assign a speed limit at some point. You can do that with something like this:

if(myNode.physicsBody.velocity.dx > 300)
    myNode.physicsBody.velocity = CGVectorMake(300, myNode.physicsBody.velocity.dy);

That will limit your "moving right" speed to 300.

Another option is to move your node manually by changing it's position. You can do that by using something like myNode.position = CGPointMake(myNode.position.x+1, myNode.position.y);. That will move your node to the right by 1 every time the code is run.

Set the restitution property of your nodes to 1.0. Restitution is how much of a node's energy is retained after a collision.

https://developer.apple.com/library/mac//documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/restitution

Example:

marble_node.physicsBody?.restitution = 1.0  

The default restitution is 0.2. Changing it to 1.0 will keep things very bouncy!

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