Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So in the last movie we started to fill out our BulletManager functions here for updating and for firing, but we only want to fire bullets when the user has their mouse down, so when they actually have the mouse button down. So let's set that up in the play state. So the first thing I'm going to want to create is a new instance of the BulletManager class. I'm going to come under where we created our hero and I'm going to create a variable called BulletManager. It's going to be of type new BulletManager.
Now remember to that we need to pass in a reference to our play state here. So I'm just going to pass in this. Again, let's create bulletManager as an instance variable. So now what we want to do is to create a Boolean value. We're going to create a public Boolean property here, public var, and we're going to call it fire. This is going to be able to tell the BulletManager whether it should fire a bullet or not. Now we're going to set it at the default to false up here, because when we first come into the movie the user is not going to have their mouse down.
So we're going to keep it at false. Now what we need to do is to listen for the mouse down and mouse up events, and I told you before how Starling doesn't have mouse events, but we can actually reach out to the nativeStage and listen for the mouse events there. So I'm going to come to the top of my init function here and I'm going create a property called ns, which is short for native stage and I'm going to set that equal to Starling and again you might have to import that Starling.core.current.nativeStage.
That way we just have a nice short reference to our nativeStage and now I'm going to listen for the mouse down and mouse up events on the nativeStage. So ns.addEventListener and these are going to be MouseEvent.MOUSE_DOWN. We'll have that function called onDown, and then I'm going to copy this, we're going to create also one called onUp, and that we're going to be listening for the MOUSE_UP event. So let's go ahead and create those.
Private function onDown. Remember, the event object is going to be MouseEvent and we can copy this and duplicate it for the onUp event. So all we're going to doing in the onDown is we're going to be setting that fire Boolean property to true, and again, the BulletManager is going to be reading that Boolean value determining whether to fire bullets or not.
Then in the onUp what I want to do is to set that fire Boolean to false. Now another thing we're going to do here is to reset the count property on our BulletManager. So let's go ahead and change that to public and we're going to reset the count = 0; and this is actually bulletManager.count. So again these methods are very simple. Now the last thing we need to do is we need to call the update method of our BulletManager.
So bulletManager.update. So now let's go back to the BulletManager. In our update method remember we came down here and we say if count is evenly divisible ten, so on every ten frames it's going to fire a bullet. But what we want to add is a condition, because we want to say if(play.fire && count%10. So that will mean it will check essentially if the mouse is down.
If so, then it's going to actually fire bullets. So I'm going to save that now and if we look we have our fire method and now we need to fill in the destroyBullet method. So let's go ahead and do that. Because remember up here in the update method if the bullet is off the screen we need to destroy it. So what I'm going to do is to copy this for loop here. We are going to use something similar up in the destroyBullet method. One of the things that we should do, best practices, is if you're looping through an array, you actually don't want to be continuously checking the length.
So what we're going to do is to save that as a variable. So I am going to come up to the top here. I'm going to create a variable called len for length. It's going to be an integer and it's equal to bullets.length. So we can use that instead of constantly hitting that bullets.length property. For consistency let's also go up to our update method and we'll do the same thing there.
So in our destroyBullet method, in this case we don't have to go
backwards through it.
So we can actually set i to 0 and we can say if i
So first let's splice it from the bullets array. We are going to say bullets.splice and we're going to start at the i position and remove one element. Let's remove the bullet from the display list. So we're going to say b.removeFromParent. We'll pass in true there and now we want to return that bullet to our pool so that it's available for the next time we want to use it. To do that we call the returnSprite method of our pool and we pass in b.
So let's go ahead and test it and see where we're at now, and I click in here and now when I hold the mouse button down you can see we're firing bullets continuously. If I lift the mouse button up, we stop. Again, hold it down and we fire. Now if you want to see what it looks like to fire on every frame, you can actually remove this, which is a little excessive. It's almost kind of like a laser. It's not even individual bullets, but you could do something like this. See the frame rate at the top is not dipping.
When you're dealing with Starling and Stage 3D you can use like thousands of things on the screen at the same time, but my personal preference again is to do a little slower. Something like this could be an upgrade, like you have a little thing that comes down that when you get it allows you to rapid fire and do all kinds of upgrades and things like that. But we now have our BulletManager class and we're able to fire bullets and again this concept of having of a manager is something that you should use often in your games, because again, you don't want your main play state class to be littered with all of this code.
It would just become too big. So it's better to offload certain things like that to some manager classes like the one we just created.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98508 Viewers
61 Video lessons · 85800 Viewers
71 Video lessons · 69704 Viewers
56 Video lessons · 102010 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.