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.
So now that we've created our Alien class we're going to create our AlienManager class. So I'm going to go to my manager's package, right-click, create a new ActionScript class and call it AlienManager and I'm going to start with the code from the BulletManager class, because there's going to be a lot of similarities. So I'm just going to copy all of that class to the clipboard and just overwrite the AlienManager class here. Now obviously I need to go through and change the class name to AlienManager.
We're going to change our Array here from bullets to aliens. So I'm just going to refactor and rename that. And we're going to change our pool, because obviously we don't want to create bullets for our pool, we want to create alien objects. So we're going to make sure we import that Alien class and we're not going to need a hundred of them. At most we're going to need 20 aliens in any one time, and again, that's a number you can arrive at just by trial and error.
So we have our AlienManager. I'm going to remove the contents here of this for loop and remove this code right here. We're going to change this local variable from b to a, and then this is obviously going to be type Alien. We're going to change our fire method to spawn, because we're going to be spawning new aliens here, and I'll just remove the contents of that function. Our destroyedBullet class is actually going to be called destroyAlien and this is obviously going to be a, and the class name is going to be Alien.
Well, that's already aliens, and we can remove the contents of that for loop. So now we can start adding in our code here. Now I'm going to start with the spawn method and that's going to be called anytime we want a new alien to be spawned. What I'm going to first do is to get an alien out of our pool. So I'm going to create a local reference variable here called a, it's going to be of type Alien is equal to pool.getSprite() and we'll cast it as Alien.
Now the difference between a Sprite and a MovieClip is this movie clip needs to be animated on every frame and the way in which we do that in Starling is to add it to an object called the juggler and the juggler makes sure that all of the objects it contains are updated. We get to the main juggler instance, because it's a static property on the Starling class. So if we say Starling, and again make sure that that is imported, .juggler.add. Then we're going to add in that (a) alien here, and that's going to make sure that it's actually animated, because if we don't do that, it's just going to be stuck on the first frame.
The next thing I want to do is to push that into the aliens array. So I'm going to aliens.push(a); and now I need to set its X and Y position. First set its Y position. So we want it to be off the screen at the top. So I'm going to set it to negative 50. Now I'm going to set its X position and here we want a random X position, because obviously we don't want them all falling in the same spot. So I'm going to say Math.random() * 700 + 50; and that will give us a nice distribution across the X axis.
Now lastly I need to add this to the display list of our play state. So we are going to say play.addChild(a). So again this function will be called anytime we want to spawn a new alien. So then that brings up the question when do we want to spawn new aliens? Well, we want to do it kind of randomly. So in our update method up here I'm going to create an if statement and I'm going to create a random number. So I'm going to say Math.random(). If(Math.random() is less than 0.2, then we're going to call the spawn method.
So now remember Math.random returns a random number between 0 and 1. So by adjusting this number here, we can make it more or less likely that a new alien is going to be spawned. Again, you can just use trial and error and then also this value can change throughout your game to make the game more difficult. So now what I want to do is to go through and update all of the aliens that are currently in the game. So I'm going to use that local variable here and I'm going to get the item out of the aliens array, and I'm going to increment its Y position by 8 pixels on every frame.
Again, you can adjust that number to adjust the speed in which the aliens travel. Now I'm going to test if the alien is actually off of the screen. So I am going to say if(a.y > 800), then I need to actually destroy this alien. So I'm going to say destroyAlien and pass in (a); and now we can actually look how to destroyAlien method, and again, this can be called from within this function, but also outside of this function when we get to collision detection.
The first thing we're going to do is to loop through and make sure we have the correct alien. So we're going to say if(a is equal to aliens i) then we are going to dispose of this and we're going to do that first by splicing it out of the array, aliens.splice. I'm going to start with i and go to 1. Now we need to remove it from the juggler. So starling.juggler.remove(a); because again, we don't want this thing to be constantly updated if it's not even in the game anymore.
Then we want to remove it from the display list. So we're going to a.removeFromParent and we're going to dispose it and then lastly we want to return this to our pools so that it can be re-used later. So pool.returnSprite(a); and we're going to get to the destroy method for our Manager classes later in this chapter. So I'm going to save this now, and I'm going to go back to my play state, because now I need to create an instance of this AlienManager. So I'll do it right under my BulletManager.
I'm going to call it alienManager is equal to new AlienManager and remember we have to pass in (this) to it. I'll create that as an instance variable. And now I need to go to my update method and here I need to update the alienManager on every frame. So alienManager.update(); So I'm going to go ahead and run this. So here is our menu. I'm going to click Play Game.
And now you can see, we have actually exhausted the pool. So if I click on here, you'll see that custom error has been thrown and then this is where we need to adjust that value; the value which determines how often something is spawned. So we need to make it a lot less than this. So let's say if it's less than 0.05 and now we can try this. This number is of course going to be trial and error, because there are way too many aliens being spawned there. So that's a decent number right there, but again, this can be adjustable.
This could be a value that's actually changed over time once the game becomes more difficult. So definitely play around with those values to create different effects. So that's the basics of our AlienManager class. Now again, we're going to filling in the destroy methods for the AlienManager and the BullentManager later on. But in the next movie we're going to look at collision detection, because obviously it's no good firing bullets if it doesn't actually kill anything.
There are currently no FAQs about Building Flash Games with Starling.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.