Start learning with our library of video tutorials taught by experts. Get started

Building Flash Games with Starling

Creating the ExplosionManager class


From:

Building Flash Games with Starling

with Lee Brimelow
Expand all | Collapse all
  1. 2m 9s
    1. Welcome
      45s
    2. Using the exercise files
      36s
    3. What you should know
      48s
  2. 10m 21s
    1. Checking out the finished product
      1m 10s
    2. Downloading the Starling framework
      4m 13s
    3. Installing Flash Builder
      1m 44s
    4. Installing additional tools
      1m 30s
    5. Overview of the Starling framework
      1m 44s
  3. 13m 0s
    1. Creating the Flash Builder project
      2m 40s
    2. Setting up your Starling scene
      6m 7s
    3. Creating the main game class
      4m 13s
  4. 13m 52s
    1. Creating the IState interface
      2m 14s
    2. Creating the three game states
      3m 50s
    3. Building the state machine
      7m 48s
  5. 22m 14s
    1. Creating a static assets class
      2m 3s
    2. Adding images and creating textures
      3m 24s
    3. Creating sprite sheets with TexturePacker
      6m 26s
    4. Creating the main texture atlas
      3m 29s
    5. Creating and importing bitmap fonts
      6m 52s
  6. 14m 16s
    1. Creating the Background class
      5m 55s
    2. Adding the logo
      2m 58s
    3. Adding the play button
      3m 4s
    4. Implementing the destroy method
      2m 19s
  7. 56m 47s
    1. Adding the background
      1m 3s
    2. Creating the Hero class
      6m 14s
    3. Creating the Bullet class
      1m 44s
    4. Creating the BulletManager class
      4m 23s
    5. Using the StarlingPool class
      9m 24s
    6. Firing bullets with the mouse
      7m 24s
    7. Creating the Alien movie clip
      1m 58s
    8. Creating the AlienManager class
      8m 12s
    9. Overview of collision-detection options
      3m 10s
    10. Creating the CollisionManager class
      10m 51s
    11. Implementing the destroy method
      2m 24s
  8. 7m 59s
    1. Adding the background
      1m 10s
    2. Creating the "Game Over" text
      2m 43s
    3. Adding the Try Again button
      4m 6s
  9. 20m 5s
    1. Exploring particle-creation tools
      4m 52s
    2. Adding the smoke texture
      5m 3s
    3. Creating the Explosion class
      2m 6s
    4. Creating the ExplosionManager class
      8m 4s
  10. 11m 5s
    1. Creating sound effects with cfxr
      3m 5s
    2. Adding the sound effects
      3m 42s
    3. Creating the Score class
      4m 18s
  11. 6m 1s
    1. Starling optimization tips
      4m 8s
    2. Helpful Starling resources
      1m 53s

Video: Creating the ExplosionManager class

So now that we've created our explosion particle class here, we're going to create an ExplosionManager to help track these in our game. So I'm going to right-click on the managers package, New > ActionScript Class, and this is going to be ExplosionManager, and click Finish. Now much like the other managers, we're going to send in our play state to the constructor, and I'm going to set that as an instance variable, this.play = play.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Building Flash Games with Starling
2h 57m Intermediate Jul 05, 2012

Viewers: in countries Watching now:

Join Lee Brimelow in this project-style course that teaches how to build a Flash-based game with Flash Builder 4.6, Flash Player 11, and the Starling framework. Starling, a pure ActionScript 3.0 library for advanced graphics, extends Flash Player 11's support for the modern GPU (graphic processing unit) to enable visual presentations and games in the browser or as a mobile app.

Starting with installation and configuration of Starling and creation of a Flash Builder project, the course shows how to prepare and import graphical assets, create MovieClip classes from sprite sheets, manage various gaming objects, detect collisions, and add both particle and sound effects. The finished game can be deployed to any browser or mobile device that integrates Flash Player 11, which was released in September 2011.

Topics include:
  • Downloading and installing Starling and Flash Builder
  • Creating the project
  • Creating the main game class, static assets class, and other classes
  • Building the state machine
  • Creating a static assets class
  • Adding images
  • Creating sprite sheets with Texture Packer
  • Creating and importing bitmap fonts
  • Detecting collisions
  • Adding particle effects
  • Adding sound effects
Subjects:
Developer Web Games
Software:
ActionScript Flash Builder Flash Player Starling
Author:
Lee Brimelow

Creating the ExplosionManager class

So now that we've created our explosion particle class here, we're going to create an ExplosionManager to help track these in our game. So I'm going to right-click on the managers package, New > ActionScript Class, and this is going to be ExplosionManager, and click Finish. Now much like the other managers, we're going to send in our play state to the constructor, and I'm going to set that as an instance variable, this.play = play.

Okay, so much like the AlienManager and the BulletManager, we're going to use an object pool here to hold our particle effects. These are the things that you don't want to be instantiating in the middle of your gameplay. So I'm going to create a variable called pool, and that's going to be equal to new StarlingPool. and for the type it's going to be a type of Explosion, make sure we import that. And we're going to create 15 of them, and again that's a number that you're going to have to trial and error depending on your game for the least number that you can get away with.

And what we're going to do is to create a public function called Spawn, which is going to spawn a new explosion. So public function spawn, and we're going to pass in an X and a Y value for where in the game we want it to spawn, and this is something we're going to be calling from the CollisionManager class. So I'm going to say our X property of type int, Y property of type int, we're not going to be returning anything, and now let's get a new explosion from the pool, so I'll create a local reference variable, I'll just call it ex, and we'll say pool.getSprite() as Explosion.

And now we want to set the emitterX and the emitterY property of our explosion, and we're going to set it to that X property that was sent in to this function. We'll set emitterY = y, and now we want to start our particle effect, so we're going to call that start method. But for this we do want to pass in a duration, because we don't want this to be like a sparkler that just keeps going. So we're going to put 0.1 seconds in here for the duration of the particle effect.

Now we want to add this particle effect to our play state, so we're going to say play.addChild(ex), and much like we did for the smoke particle, we need to add it to the juggler, so Starling.juggler. add(ex), and what we need now is to know when this particle effect is finished, essentially it's going to blow up and it's going to make all these stars come out.

We want to know when it's actually finished, so that we can clean up after ourselves and return it to the pool. Well there is a complete event on the PD particle designer class, so we'll listen for that. So ex.addEventListener and we want Starling.events and we want the COMPLETE event. And we'll just have it call a function called onComplete, and go ahead and create that method, and make sure we are catching our event object as well.

Okay, so in the onComplete we essentially want to clean up that particle effect and return it to the pool. So first I'm going to get a local reference to it and so var ex of type Explosion = event.currentTarget as Explosion, and that gives us a reference to the particle effect that is finished here. And what we want to do most importantly is to remove this from the juggler, so Starling.juggler.remove(ex).

And now we want to return it to the pool, and just to be on the safe side here, we're going to do a check to make sure if pool is not equal to null then we're going to say pool.returnSprite(ex). And the reason is once we get down to our destroy method for this, it's possible that this might actually fire after we've killed the pool, so just to be safe we're going to put that line in there. So now we need a public function which is going to be our destroy method.

So we're going to say public function destroy. Now these particle effects are one of the more trickier things to remove from memory, so I'm going to show you how we're going to go about doing that. We're going to go do a for loop, so we can loop through all the items in our pool. So we're going to say for(var i:int=0; i

And let's get a reference to that item, so let's say var ex:Explosion = pool.items i, and we're going to say ex.dispose(). So that's an important thing that we need to do is to call the dispose method of that particle, and then we're going to set ex = null. So after we do that, then we're going to actually destroy the pool, so pool.destroy(); and then we'll set pool also equal to null.

And that will successfully remove our particle effects from memory. So now we have our ExplosionManager, now we actually need to create that ExplosionManager in our play state, where we created the other ones. So I'm going to come under CollisionManager and create it here is equal to new ExplosionManager. Now we could create this in the CollisionManager class, but I prefer to create it here in our play state, create that as an instance variable, and we're going to need to create this public, because we're going to need to reference it from our CollisionManager.

So now what I want to do is to go into my CollisionManager class and I want to make one of these explosions happen. So here while we're testing between bullets and aliens, if we determine that a collision has happened, what we want to do is to go play.ExplosionManager.spawn and give it the position where we want this to spawn, while we want to actually do it from a.x and a.y.

And actually, let's move this before we actually call those destroy methods, like that. Okay let's go ahead and test it now. We'll run, and now you can see we have those cool particle effects happening whenever a bullet touches one of the aliens, and you can imagine easily creating multiple of these types of particle effects for different things that happen in your game. Now it's important to note that you can also adjust properties like the color of these particles at run time.

So let's say we also had red aliens, and we wouldn't have to create a whole new particle effect, we can just change the particular color of these existing particle effects. So really cool that we can easily add these kind of eye candy effects to our game using the particle extension in Starling.

There are currently no FAQs about Building Flash Games with Starling.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Building Flash Games with Starling.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked