Jush KillaB
Jush KillaB

Reputation: 151

Moving Sprite properly based on the touch in Swift

I have a very simple app. I have squares falling from the sky, which are physics bodies. When the user taps an square, it jumps up. He needs to prevent the fruits from touching the ground.

Now I thought when touching the apple, just move its position.y += 10. But I would like the apple to jump up in different x-directions as well and I dont know how to implement that.

For example when I touch the bottom left edge of the square, it should not only jump up but an decent amount to the right as well.

Any ideas how to implement this moving into swift?

Upvotes: 0

Views: 212

Answers (1)

Ali Beadle
Ali Beadle

Reputation: 4516

As you already have PhysicsBodies, you can use the applyImpulse(_:,at:) to apply a 'bump' to the object when touched. Set the vector to be upwards and the position to be the position of the touch (in the object's coordinates).

Alternatively, for more control, use applyImpulse(_:), set the vector of the impulse to mostly upward with a bias left or right according to the X position of the touch to give it the push left or right you are looking for. Then for additional realism you could applyAngularImpulse(_:) at the same time to give it some spin.

Upvotes: 3

Related Questions