Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97442 Viewers
61 Video lessons · 84668 Viewers
71 Video lessons · 68871 Viewers
56 Video lessons · 101326 Viewers
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.
Your file was successfully uploaded.