Skip to content

Instantly share code, notes, and snippets.

@webcaetano
Created November 27, 2015 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webcaetano/d6a085f8166ef801713e to your computer and use it in GitHub Desktop.
Save webcaetano/d6a085f8166ef801713e to your computer and use it in GitHub Desktop.
Phaser Tweened Particle.
function create() {
// console.log(game.add)
var rand = function(min, max){
if(!min) min = 0;
if(!max) max = 100;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
game.input.onUp.add(function(){
var swingOffset = 8;
var swingTimes = 6;
var lifetime = 1000;
var emitter = game.add.emitter(0,0,100);
emitter.makeParticles('particle');
emitter.setYSpeed(-150, -200);
emitter.setXSpeed(0, 0);
emitter.setRotation(0, 0);
emitter.setAlpha(0.9, 0,1000);
emitter.setScale(0.2, 0.1, 0.2, 0.1, 1000, Phaser.Easing.Quintic.Out);
emitter.x = game.input.x;
emitter.y = game.input.y;
emitter.start(true,1000,null,1)
emitter.forEachExists(function(particle){
var tw = game.add.tween(particle);
var left = rand(0,1);
for(var i=0;i<swingTimes;i++) {
// tw.to({ x: rand(particle.x-swingOffset,particle.x+swingOffset)}, i===0 ? (lifetime/swingTimes)/2 : lifetime/swingTimes, Phaser.Easing.Linear.None);
tw.to({ x: particle.x+swingOffset*(left ? -1 : 1)}, i===0 ? (lifetime/swingTimes)/2 : lifetime/swingTimes, Phaser.Easing.Linear.None);
left = !left;
}
tw.start()
.onComplete.addOnce(function(){
if(particle && particle.remove) {
particle.remove();
emitter.destroy();
}
});
})
})
// var sprite = game.add.sprite(0, 0, 'phaser');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment