I'm playing around with phaserJS to make a simple example of top to down car game. I thought it would be nice, add to the car some drift marks when accelerating. So I came up adding a two graphics elements and then update their position on each frame.
Something like this:
var game = new Phaser.Game(500, 500, Phaser.AUTO, 'game');
var mainState = {
preload:function(){
game.load.image('car', 'notfound');
},
create: function() {
game.stage.backgroundColor = '#ffffff'
game.physics.startSystem(Phaser.Physics.P2JS);
car = game.add.sprite(window.innerWidth/2, window.innerHeight/2, 'car');
game.physics.p2.enable(car, false);
car.anchor.setTo(0.5, 0.8);
this.graphics = game.add.graphics(0,0);
this.graphics2 = game.add.graphics(0,0);
this.graphics.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*5),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*5));
this.graphics2.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*(-5)),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*(-5)));
this.trololo;
},
update: function() {
car.body.damping = 0.9;
car.body.setZeroRotation();
if (game.input.keyboard.isDown(Phaser.Keyboard.UP)){
this.graphics.lineStyle(2, 0x111111, 1);
this.graphics2.lineStyle(2, 0x111111, 1);
this.graphics.lineTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*5),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*5));
this.graphics2.lineTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*(-5)),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*(-5)));
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
car.body.rotateLeft(100);
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
car.body.rotateRight(100);
}
car.body.thrust(400);
}
else{
this.graphics.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*5),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*5));
this.graphics2.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*(-5)),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*(-5)));
if(Math.abs(car.body.velocity.x) > 6 || Math.abs(car.body.velocity.y) > 6){
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
car.body.rotateLeft(10);
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
car.body.rotateRight(10);
}
}
}
}
};
game.state.add('main', mainState);
game.state.start('main');
<div id="game"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.8/phaser.min.js"></script>
Now, I would like this marks to disappear progressively... Any clue on how to achieve that?