Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now that I've showed you how you can go ahead and create particle effects, let's incorporate the first particle effect into our actual game. So I've included two particle effects in the assets; the first is this smoke particle effect and this is going to be the exhaust for our hero ship. So I'm in our assets class. Now remember that our particle PNG file is actually in our texture atlas, so we don't need to actually go ahead, and embed that here. All we need to embed is the PEX file.
So I'm going to say Embed(source=" assets/smoke.pex" which again is that XML file, and I'm just going to copy this, because again, since it's an XML file, we need to set its MIME type. And now I'm going to create this as a public static variable because we are going to reference this directly from our Hero class. Public static var and I'm just going to call it smokeXML, it's going to be of Type class.
Okay, so now let's go to our Hero class, and what I want to do is to set up this particle system. So I'm going to come under where we import our image. I'm going to create a new property called smoke, I'm going to set it equal to new, and then the class is PDParticleSystem. That's the class which actually handles our particle system. So to the constructor, you can see it's expecting that particle XML file which is our PEX file, and then our texture that we are using for the particle effect.
So I'm going to first have to cast that to an XML file and then create a new instance of Assets.smokeXML. Now for the texture, remember, that's in our texture atlas, so I'm going to say Assets.ta.getTexture and it's called smoke. Now we need to actually create that as an instance variable, and this has now instantiated our new particle system.
So now we need to set up its properties. So much like a movie clip where the movie clip needs to animate itself, we need to add it to the juggler object. We need to also do that for our particle effects. So we need to stay Starling and make sure we import that class, .juggler.add(smoke). Now we need to add it to the display list of our game. Now the reason we are adding it to the display list of our game, or our play state, is we are going to be moving these things kind of independently of each other, because again, if we just move this hero object, the particle system inside of it, if we add it to the display list here, is not going to be moving.
So we are not going to get nice trails that go back and forth as we move our ship. So that's why we are going to add the particle effect to the display list of the play state. So we are going to say play.addChild(smoke); and that's a very important part. Now what we need to do is to actually start the particle system. So we are going to say smoke and we are going to call the start method. Now if we want this to only last a certain amount of time, we want to pass in a duration here, and we will be doing this for the explosion particles.
But for this, we want it to be constant, because the ship is always going, so we want the smoke particle to always be running, and in that case, we can just leave out the duration, and it will automatically loop this thing. Okay, so now in our update method, we need to update the position of our smoke so that it sticks with the position of our ship. But the important thing to note is we are going to be changing the smoke.x because that's just essentially changing the coordinate system. What we need to change is the emitterX.
So it has an emitterX and emitterY property. Those are the values we want to change if we want to get nice side-to-side movement of our particle effect. So we are going to change that to the x position of the hero, and then smoke.emitterY, we are going to set that equal to the y position of our ship. But we are going to add 20 on it, so that it looks like it's coming out of the back of the ship. Okay, let's save that, and let's check it out. Now you can see we have our nice particle effect that moves side to side.
It looks really nice. I mean, one of the reasons I love Starling is that it's so easy to do these kinds of really rich particle effects that look really nice and really realistic. So that's our smoke particle effect. Now we are going to focus on the explosions which happen when a bullet actually hits one of these aliens.
Get unlimited access to all courses for just $25/month.Become a member