fritzvd

Particle Emitter in HaxePunk

· fritzvd

For some this will be a trivial example. For me this took a bit longer than I suspected.

This small example will produce this:

If you’re planning on making an awesome game you probably want a particle emitter. because.. well look at it.

What I did is I started a haxepunk project:

`For some this will be a trivial example. For me this took a bit longer than I suspected.

This small example will produce this:

If you’re planning on making an awesome game you probably want a particle emitter. because.. well look at it.

What I did is I started a haxepunk project:

`

And added a file: Explosion.hx

The Emitter is also a type of Entity, and also needs a graphic. It looks like this:

import com.haxepunk.graphics.Emitter;
import com.haxepunk.utils.Ease;
import com.haxepunk.Entity;

class Explosion extends Entity
{
	private var _emitter:Emitter;

	public function new()
	{
		super(x, y);
        _emitter = new Emitter("graphics/particle.png", 4, 4);
        _emitter.newType("explode", [0]);
        _emitter.setMotion("explode",  		// name
		        	0, 				// angle
		        	100, 			// distance
		        	2, 				// duration
		        	360, 			// ? angle range
		        	-40, 			// ? distance range
		        	1, 				// ? Duration range
		        	Ease.quadOut	// ? Easing	
		        	);
        _emitter.setAlpha("explode", 20, 0.1);
        _emitter.setGravity("explode", 5, 1);
        graphic = _emitter;
        layer = -1;
	}

	public function explode(x:Float, y:Float)
	{
		for (i in 0...20)
		{
			_emitter.emit("explode", x, y);
		}
	}


}

Then you can add the Emitter to your current scene like in my MainScene.hx

It looks like this:

import com.haxepunk.Scene;
import Explosion;

class MainScene extends Scene
{
	private var explosion:Explosion;

	public override function begin()
	{
		explosion = new Explosion();
		add(explosion);
	}

	public override function update()
	{
        super.update();
        explosion.explode(230, 240);
	}
}

Now run

lime test neko

And you should see something similar to the emitter above.

You can checkout my repo and run it to play around with it.

I ran into this error below. It meant my sprite was too small. It took me a long time to realize that.

``For some this will be a trivial example. For me this took a bit longer than I suspected.

This small example will produce this:

If you’re planning on making an awesome game you probably want a particle emitter. because.. well look at it.

What I did is I started a haxepunk project:

`For some this will be a trivial example. For me this took a bit longer than I suspected.

This small example will produce this:

If you’re planning on making an awesome game you probably want a particle emitter. because.. well look at it.

What I did is I started a haxepunk project:

`

And added a file: Explosion.hx

The Emitter is also a type of Entity, and also needs a graphic. It looks like this:

import com.haxepunk.graphics.Emitter;
import com.haxepunk.utils.Ease;
import com.haxepunk.Entity;

class Explosion extends Entity
{
	private var _emitter:Emitter;

	public function new()
	{
		super(x, y);
        _emitter = new Emitter("graphics/particle.png", 4, 4);
        _emitter.newType("explode", [0]);
        _emitter.setMotion("explode",  		// name
		        	0, 				// angle
		        	100, 			// distance
		        	2, 				// duration
		        	360, 			// ? angle range
		        	-40, 			// ? distance range
		        	1, 				// ? Duration range
		        	Ease.quadOut	// ? Easing	
		        	);
        _emitter.setAlpha("explode", 20, 0.1);
        _emitter.setGravity("explode", 5, 1);
        graphic = _emitter;
        layer = -1;
	}

	public function explode(x:Float, y:Float)
	{
		for (i in 0...20)
		{
			_emitter.emit("explode", x, y);
		}
	}


}

Then you can add the Emitter to your current scene like in my MainScene.hx

It looks like this:

import com.haxepunk.Scene;
import Explosion;

class MainScene extends Scene
{
	private var explosion:Explosion;

	public override function begin()
	{
		explosion = new Explosion();
		add(explosion);
	}

	public override function update()
	{
        super.update();
        explosion.explode(230, 240);
	}
}

Now run

lime test neko

And you should see something similar to the emitter above.

You can checkout my repo and run it to play around with it.

I ran into this error below. It meant my sprite was too small. It took me a long time to realize that.

``