Building Flash Games with Starling
Illustration by John Hersey

Using the StarlingPool class


From:

Building Flash Games with Starling

with Lee Brimelow

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Using the StarlingPool class

So now in our BulletManager class we need to actually handle the creation and destruction of bullets and this is a time to talk about a very important concept for games and that's object pooling. So one of the things that's most expensive performance-wise in games or any application is when you instantiate a new object, because there is a lot of overhead. So let's say in a game where I am constantly firing bullets like this one, every time I fire a bullet I don't want to create a new bullet using the new keyword and then when it's off the screen I destroy it.
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 1s
    1. Creating the Flash Builder project
      2m 40s
    2. Setting up your Starling scene
      6m 8s
    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. 8m 0s
    1. Adding the background
      1m 10s
    2. Creating the "Game Over" text
      2m 44s
    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 6s
    1. Creating sound effects with cfxr
      3m 5s
    2. Adding the sound effects
      3m 43s
    3. Creating the Score class
      4m 18s
  11. 6m 1s
    1. Starling optimization tips
      4m 8s
    2. Helpful Starling resources
      1m 53s

please wait ...
Watch the Online Video Course 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
Software:
ActionScript Flash Builder Flash Player Starling
Author:
Lee Brimelow

Using the StarlingPool class

So now in our BulletManager class we need to actually handle the creation and destruction of bullets and this is a time to talk about a very important concept for games and that's object pooling. So one of the things that's most expensive performance-wise in games or any application is when you instantiate a new object, because there is a lot of overhead. So let's say in a game where I am constantly firing bullets like this one, every time I fire a bullet I don't want to create a new bullet using the new keyword and then when it's off the screen I destroy it.

That repetitive process causes a lot of stuttering in your game and it's just a bad practice. So this is where you want to use an object pool where at the beginning of your game you create a pool of, in our case, we are going to create 100 bullets in memory right at the beginning of our game and then we are just going to use those bullets that are already in memory and just reuse them and that's definitely the approach to take. So back earlier in the course we actually linked to a class that I created called StarlingPool and let's look at that really quickly.

This is a simple class that essentially allows you to, when you instantiate it, send in a type and this is a class of what type of object you want to create for the pool and then a length of how many objects you want to create for the pool. It has three public methods available. First is something called getSprite and this when you want a new object from the pool. So let's say when it comes time to fire a bullet I am going to call getSprite. Now if you go beyond, let's say, if I create 100 bullets and there is more than 100 bullets being requested it's going to throw an error, so that's where you need to make sure that you are instantiating enough bullets for your game.

Now when you are done with the bullet, let's say, when it goes off the top of the screen, I return it to the pool using this returnSprite method. And lastly, we can also destroy all of the items by calling the destroy method. So that's what we are going to use in our BulletManager. So what I am going to do is under where we create a new array, we are actually going to instantiate a new variable. I am going to call it pool and that's going to be equal to new StarlingPool.

Now the constructor for the class type, we want to send in bullet, because remember we want to actually create a pool of bullet objects and I am going to pass in 100. So we want to instantiate 100 bullet objects for us to use, because the ship is going to be firing pretty rapidly. You aren't going to want to trial and error with those numbers. Obviously, you want to use the least amount as you can, because it's going to take up less memory. So I am going to create that pool variable as an instance variable at the top and now what I am going to do is go in and fill in this fire method.

Again, this is going to be called when we want to fire a new bullet. But in our game we actually have guns on either side, on either wing. So whenever we fire, we are actually going to be firing two bullets. So let's go ahead and implement this method now. So I am going to create a new reference here. I am going to call it b, and this is going to be of type Bullet. Now if I wasn't using an object pool this is where I would say new bullet, but this is again is what you want to do. What we want to do is to pull one of those bullets out of the pool.

So I am going to say pool.getSprite. Now the type for inside of the StarlingPool class is actually DisplayObject. So I am just going to cast it to a bullet. So I am going to say as Bullet. What we want to do now is to add this bullet to the display list of our play state and that's why we got a reference to our play state here. So I am going to say play.addChild(b). Now we want to position the bullet in the right location.

So we are going to say b.x = play.hero. Now you'll notice by default when we created it, it created hero as a private property. So what we are going to do is to go back to our play state and make sure that that hero property is actually set to public so we can see it from our BulletManager. And now we can say play.hero.x. But we want to move it a little bit, because again, we don't want to do it in the center of the ship for this one, we want to do it on to the left of the wing.

So we'll actually say -10 here. And now we'll set the Y position of the bullet. So b.y = play.hero.y and we actually want to push it back a little bit on the Y axis. So we are going to say -15 just so it's positioned where it should be. So now let's go ahead and copy this block, because again, we are going to be creating two bullets every time we fire. So I'll paste this in and I am just going to reuse this b reference here.

So we are going to get another bullet from the pool. We are going to add it to the display list of our play state and we are going to set it X and Y. Now for this one obviously we are going to want to put the X position at +10, so it's on the other wing. Now the one last thing that we need to do is we need to add these bullets into our bullets array, because again, as soon as we fire, these things are active bullets in our game. So I am going to come under each of these and push it into that bullets array.

So I am going to say bullets.push(b) and then we'll do the same thing down below bullets.push(b). So now when our fire method is called, again we are going to be creating two individual bullets, adding them to the display list of our play state, positioning them, and also adding them into our bullets array so that we can update them on every frame. So now we want to turn our attention to our update method.

Again, we are going to be calling this BulletManager update method on every frame from our play state. So what we are going to want to do is to loop through our bullets array and update all the positions of the active bullets. So I am first going to come in here and I am just going to create a local reference variable and I am going to call it b and it's going to be of type bullet. Now we want to loop through that bullets array. So we are going to use the for loop. Now an important thing when you are creating a for loop and looping through an array that you might splice something out of, is you want to go from the top down.

You don't want to start at zero and increment up through all the items. You want to go backwards through the array. So we are going to create an integer counter here and we are going to set it equal to bullets.length -1 and then we are going to test for if i > =0 then go ahead and continue and then we are going to subtract from i everytime we go through. So what we are first going to do in here is to get a reference so we are going to use that b variable and we are going to say bullets i, so that way we have a nice reference variable to work with.

Now on every frame we are going to increment the Y position of the bullet, we are actually going to decrement it by 25 so it moves up the screen. So we are going to say b.y - = 25. Now if the bullet is off the top of the screen then let's go ahead and destroy that bullet. So we are going to say if b.y < 0, call the destroyBullet method and pass in this bullet.

And we'll go ahead and set up the destroyBullet in a minute. So again that's what we are going to do in our for loop. Now another thing we need to do is we are not going to call the fire function on every frame, because remember this is running at 60 frames per second. So it's going to be firing way too many bullets. So what we are going to do is we are going to only do it on every sixth frame. So if I actually come--I am going to create a property at the top here, private var count, and this is going to be an integer and it's simply going to count the number of times that the update function has been called.

So we are going to come down to the bottom of our update function and increment that count variable. So now we are going to use the Modules operator to test whether we should fire or not. So we are going to say if count%10 = 0 then we are going to call the fire method and that's just a way for us to slow down the amount of times that it's actually firing, because doing it 60 frames per second is just going to fire way too many bullets.

So that's the basic setup for our update function. Now in the next movie I am going to show you how to implement the actual fire behavior in our play state and get the bullets firing the way they should.

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

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

Join now Already a member? Log in

* Estimated file size

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 ?

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 preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.