I am trying to effect the movement speed of a sprite in my app. When i apply velocity to the sprite it slowly increases in speed until it reaches a maximum speed. I am looking for a method to remove the smooth increase in speed and just have the exact same speed all the time (Until i change the velocity)
Feel free to add a comment if you have any questions.
enemy1 = SKSpriteNode(texture: enemyTexture) enemy1.position = CGPoint(x: CGRectGetMidX(self.frame) - 300, y: CGRectGetMidY(self.frame) - 300) enemy1.physicsBody = SKPhysicsBody(texture: enemyTexture, size: enemyTexture.size()) enemy1.physicsBody!.affectedByGravity = false enemy1.physicsBody!.allowsRotation = false self.addChild(enemy1)
This is the velocity i apply to the sprite. I am doing it in the update function for a continuing movement speed:
override func update(currentTime: NSTimeInterval) { enemy1.physicsBody!.velocity = CGVectorMake(70, 0) }
2 Answers
Answers 1
Instead of changing the velocity, have you tried to apply force vector for each update like this:
override func update(currentTime: NSTimeInterval) { enemy1.physicsBody!.applyForce = CGVectorMake(70, 0) }
Answers 2
I tried your code and it works correctly for me (sprite has a constant speed from the very beginning). It always worked like that. You can check this by pasting your code in an empty SpriteKit project. Currently, what you have described looks like you are actually using applyForce
method rather than changing velocity vector directly.
The solution certainly is to change velocity property directly, like you are saying that you are doing right now. Probably you are overwriting this behaviour somewhere in your code accidentally, because what you have described isn't reproducible (with the code you've provided).
0 comments:
Post a Comment