How can I move a circular body to another position with an animation? When I use translate(), the object disappears and reappears instantly at the new position.
I'm developing simple game. The player has to avoid incoming obstacles from the top. They can only move to left or right at three fields. I want to animate the motion on these fields. Currently, I have something like this:
public move = (entities, {touches, time}) => {
touches.filter(t => t.type === 'press').forEach(t => {
const player = entities['player']
const direction = this.getPlayerMoveDirection(t.event.pageX)
const nextFieldId = this.getNextFieldIdByDirection(direction)
if (nextFieldId !== this.fieldId) {
this.setFieldId(nextFieldId)
const nextField = this.game.fields[nextFieldId]
const nextFieldXPosition = nextField.getCenter()
const newXPosition = direction === 'left' ? (player.body.position.x - nextFieldXPosition) *-1 : nextFieldXPosition - player.body.position.x
Matter.Body.translate( player.body, {x: newXPosition, y: 0});
this.playMoveSound()
}
})
return entities
}
This works very well but the sprite is not animated. It just appears at the new position. I want to animate its movement.
I'm using Matter.js in react-native with react-native-game-engine.