Particle Emitters!


(Nate ) #1

Hey guys I have a question! I am messing around with particle emitters for the first time so I am basically just trying to hook in someone’s code, that may or may not be dated. So basically I want to have a nice particle effect when the player jumps. Here is all of the respective code:

theParticle.as

package entities {
import net.flashpunk.Entity;
import net.flashpunk.graphics.Emitter;
import net.flashpunk.graphics.ParticleType;
import net.flashpunk.utils.Ease;

public class theParticle extends Entity
{
	[Embed(source = '../assets/particle.png')] 
	private const PARTICLE:Class;
	public var emitter:Emitter = new Emitter(ParticleType, 11, 11);
	
	public function theParticle() 
	{
		graphic = emitter;
		emitter.x = emitter.y = -5;
		//layer = 1;
		
		//create dust particles
		// Create a dust particle
		var p:ParticleType = emitter.newType("dust", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
		p.setMotion(20, 5, .2, 140, 50, .3, Ease.cubeOut);
		p.setColor(0xFFFFFF, 0xFF3366, Ease.quadIn);
	}
	
}}

thePlayer.as related variable:

	public var emit:Emitter;

thePlayer.as class inside of the jump function

emit.emit("dust", x - 10 + FP.rand(20), y + 16);

thePlayer.as added function:

override public function added():void
	{
		emit = (FP.world.classFirst(theParticle) as theParticle).emitter;
	}

The error I get when I try to jump in the game is:

“[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.”

Let me know what you guys think!

Thanks!


(Jacob Albano) #2

It would help to have the line number that the error is raised at. Without it all we can do is guess.

Try tracing the value of emit after the line in the added() function.


(Nate ) #3

The game doesn’t get past the splash screen… and the line:

emit = (FP.world.classFirst(theParticle) as theParticle).emitter;

is what is throwing the error. which is basically the entire added function.


(Jacob Albano) #4

That’s what I thought. Do you ever add an instance of theParticle to the world? If you never do, classFirst(theParticle) will always return null.


(Nate ) #5

I do not have it in the world.as… how would I go about adding it?

import entities.theParticle;

private var part:theParticle;

add(part);

I just added those lines to the world.as and got the same error.


(Nate ) #6

I just added part = new theParticle;

now I am getting this as an error:

[Fault] exception, information=ArgumentError: Error #1063: Argument count mismatch on net.flashpunk.graphics::ParticleType(). Expected 5, got 0.


(Jacob Albano) #7

Well, see the thing is that classFirst() returns the first instance of the specified entity class that exists in the world. If you never add any, you’ll never get a valid instance back.

Again, tell us the line that the error is from, and copy the code on that line in here. We won’t be able to help you otherwise, especially when you’re having errors that aren’t specific to Flashpunk. Pay attention to the error messages! They’re designed to tell you what the problem is, and this is one of the simpler ones.

“Argument count mismatch”: You passed the wrong number of arguments to a function.

“on net.flashpunk.graphics::ParticleType()”: The function is the constructor of the ParticleType class.

“Expected 5, got 0”: You passed zero arguments where you should have passed 5.


(Nate ) #8

Okay thank you for the information!

After a painful comment related lines out and compile, this appears to be the line causing the issue, since I have never worked with particles I am not sure how to fix this… Let me know what you think please.

this line is located in theParticle.as

public var emitter:Emitter = new Emitter(ParticleType, 11, 11);

(Jacob Albano) #9

You’re passing the ParticleType class to the Emitter? Why? Did you mean to pass your PARTICLE embedded bitmap?


(Nate ) #10

Oh… that might explain it… so I just changed that line to this:

	public var emitter:Emitter = new Emitter(PARTICLE, 11, 11);

Now everything loads and runs, but there is nothing appearing at my players feet! D:


(Jacob Albano) #11

Either change your emit call to this:

emit.emit("dust", -10 + FP.rand(20), 16);

or set your emitter to non-relative:

emit.relative = false;

By default all graphics in Flashpunk are relative to the entity that they belong to. Your particles are just being created offscreen.


(Nate ) #12

Okay I changed the emit call, and I still do not see anything. I enabled the console and held shift and moved around off screen, I do not see them anywhere.


(Nate ) #13

I added this line:

emitter.emit("dust", x, y);

to my theParticle.as file and it is now appearing on the stage, I just have to tweak the x,y values now!


(Nate ) #14

Okay so I have it all working now! Just for my own edification can someone explain to me what the importance of the added function is? And why I had to use this function:

override public function added():void
	{
		emit = (FP.world.classFirst(theParticle) as theParticle).emitter;
		
	}

In the player.as class to have it work?


(Ultima2876) #15

The added function is called as soon as the entity is added to the world. You need a reference to the emitter to be able to emit particles from it, so when the player is added to the world you are storing a reference to the emitter in emit for later use (so you can emit particles in other functions later on).


Set the scale of particles like you would color or alpha?
(Zachary Lewis) #16

The Pink demo game is a good source of learning.


(Zouhair Elamrani Abou Elassad) #17

I’ve checked the Pink repository, is the project ready to be tested or do i have to make some changes, i would love to see a demo :smile:


(Zachary Lewis) #18

I’m not sure. Try it out and let us know!


(Zouhair Elamrani Abou Elassad) #19

Actually i did, the Main is not extending the Engine class, and there is something about XML, do you think you can check that out, that would be great


(Darrin) #20

Ok it is a great demo. I wasn’t clear on what Ease does exactly until I a couple made a difference. They seem to have an effect on the speed and sometimes the direction.

Setting up as an entity is important so you can control the layer. Unfortunately I had the exact same problems as above. The code that confused the heck out of me was this.

emitter = (FP.world.classFirst(Particles) as Particles).emitter;

For anyone else that is doing work on the demo, you’ll notice that you need to code organized like his. A world that adds particles (see level.as) and that the particles is being called by an entity so you get to override add(). In my case I forgot to add the particles and I had no way to override because I wasn’t calling from an entity thus giving I’d get a null error even after added.

Another way to call from a non-entity is as follows.

		private var particles:Particles = new Particles();
	
	public function SquareManager(myworld:World) 
	{
		this.myworld = myworld;			
		myworld.add(particles);
		emitter = particles.emitter as Emitter;					
	}

Anyway, great stuff. it looks cool.