Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Firing bullets with the mouse

From: Building Flash Games with Starling

Video: Firing bullets with the mouse

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.

Firing bullets with the mouse

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 i++, and here we're going to loop through the array and check if the current array variable is equal to the bullet that we sent into the method. So we're going to do that and we're going to say if (bullets i is equal to b), because remember that's the bullet we're sending into this method, then we're going to remove it.

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.

Show transcript

This video is part of

Image for Building Flash Games with Starling
Building Flash Games with Starling

46 video lessons · 9459 viewers

Lee Brimelow
Author

 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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

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
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.