Skip to content

Instantly share code, notes, and snippets.

@smks
Last active March 1, 2023 02:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smks/e88600375c01a50a0056 to your computer and use it in GitHub Desktop.
Save smks/e88600375c01a50a0056 to your computer and use it in GitHub Desktop.
Haxeflixel Confetti Example
package com.particles;
import flixel.effects.particles.FlxEmitter;
import flixel.effects.particles.FlxParticle;
import flixel.FlxG;
import flixel.group.FlxTypedGroup;
import flixel.system.FlxCollisionType;
import flixel.util.FlxRandom;
/**
* @author Shaun Stone (SMKS) <http://www.smks.co.uk>
*/
class Confetti extends FlxTypedGroup<FlxEmitter>
{
#if mobile
private static inline var MAX_COUNT:Int = 30;
#else
private static inline var MAX_COUNT:Int = 100;
#end
var emitter:FlxEmitter;
public function new()
{
super(MAX_COUNT);
emitter = new FlxEmitter(0, 0, MAX_COUNT);
emitter.acceleration.y = 200;
emitter.gravity = 150;
for (i in 0...MAX_COUNT) {
var p = new ConfettiParticle();
emitter.add(p);
emitter.kill();
}
emitter.setSize(FlxG.width, 0);
add(emitter);
}
override public function update():Void
{
super.update();
}
public function trigger():Void
{
for (i in 0...MAX_COUNT) {
emitter.recycle(FlxParticle);
}
emitter.start(true, 0);
}
}
package com.particles;
import flixel.effects.particles.FlxParticle;
import flixel.FlxG;
import flixel.interfaces.IFlxParticle;
import flixel.util.FlxRandom;
/**
* @author Shaun Stone (SMKS) <http://www.smks.co.uk>
*/
class ConfettiParticle extends FlxParticle implements IFlxParticle
{
var spinRotation:Float;
public function new()
{
super();
this.makeGraphic(20, 20, FlxRandom.color());
this.x = FlxRandom.floatRanged(0, FlxG.width - this.width);
this.y = this.height * 1.5;
this.exists = false;
this.angularVelocity = 0.1;
this.friction = 0;
this.spinRotation = FlxRandom.floatRanged(0.01, 0.05);
}
override public function update():Void
{
super.update();
this.scale.x = 1;
this.scale.y = this.scale.y - spinRotation;
if (this.scale.y <= -1) {
this.scale.y = 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment