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

Creating the Hero class

From: Building Flash Games with Starling

Video: Creating the Hero class

So now that we have our scrolling background in our play state, the next thing we're going to create is our actual hero, and this is the fighter that's actually going to be firing bullets at all of the aliens. So what we're going to do is in our objects package, we're going to create a new ActionScript Class, and we're going to call it Hero, and this for a Superclass is going to be starling.display.Sprite. And what we're going to need to do is when we create this hero, we're going to pass in a reference to our play state here, so that we can actually call back to it.

Creating the Hero class

So now that we have our scrolling background in our play state, the next thing we're going to create is our actual hero, and this is the fighter that's actually going to be firing bullets at all of the aliens. So what we're going to do is in our objects package, we're going to create a new ActionScript Class, and we're going to call it Hero, and this for a Superclass is going to be starling.display.Sprite. And what we're going to need to do is when we create this hero, we're going to pass in a reference to our play state here, so that we can actually call back to it.

So I'm going to put in a argument here called play, and this is going to be of type Play, so it's in from our states. And now I'm going to create an instance variable to hold that, so I'm going to say this.play = play; and we'll create that as an instance variable. Okay, so now for the visual representation of our hero, we're going to be using a texture which is actually in our texture atlas.

So I'm going to come down here, I'm going to create a new variable. I'm just going to call it img for image, and it's going to be of type starling.display.image = new Image, and now we're going to grab a texture from our texture atlas, so it's going to be Assets.ta.getTexture, and the texture name is hero. Now we're going to set the pivotX and the pivotY. For all of the objects that are in our game, we're going to set its pivot point directly in the center of the visual representation, and that's going to make it a lot easier for us as we're doing this like collision detection.

So I'm going to say img.pivotX = img.width * 0.5, and now we'll do the same thing for the pivotY = img.height * 0.5. And lastly let's go ahead and add that to the display list here. So now when we're actually moving our hero around, we're going to be doing it with the mouse, based on the mouse position.

Now the Starling framework does not have mouse events built in, it was actually first designed with mobile in mind, so it actually uses touch events. Now that's good if your game or your application is taking advantage of things like multitouch, but if you're not taking advantage of multitouch, it's better to reach out to your main Flash stage and get the mouse position that way. So I prefer to use mouse events, unless I have to use the touch events which are in Starling, because there is more overhead associated with those.

So for updating my hero's position, I'm going to create a public function called update, and you'll notice the theme here that all of the game objects are going to have an update function with which can be called to obviously update its position or any other characteristics of the object. So inside of this update method, what I'm going to do is I'm going to ease the position of my hero to the current mouse position, and I'm going to do it using typical Flash easing code.

So I'm going to increment a certain amount to the X position of my hero, and what I need to do is to first get the mouse position. So here's where I'm going to need to reach out to the native Flash stage, and to do that I'm going to go the Starling method, you'll need to import that, it's in starling.core. Then you want to go to current.nativeStage, and now that gives you a reference to the native Flash stage, so you've now reached out back into the regular Flash display list world, and from there, I can retrieve the mouseX property.

In order to ease to that, I'm going to subtract the current X position from that, and that's going to give me the distance that needs to be traveled towards the target, but I'm going to multiply that by 0.3, so then it's essentially going to ease to that position. Now I'm going to duplicate that line, because this is going to be same thing for the Y position. I'm obviously going to be using the mouseY property, and I want to ease to the Y property there. So this is the update function again, and we're going to be calling that on every frame from the play state.

So let's go back to our play state, and let's create our hero now. So I'm going to come under here under the background, create a new variable called hero, and I'm going to instantiate a new hero object. And remember, we're passing in a reference to this play state, so I'm going to pass in this, create this as an instance variable, and now we want to add that hero to our display list. And now lastly in the update method of our play state, after we call an update on the background, let's call an update on our hero, because again we created that public update method.

Let me save that now and let's go ahead and test it. So we have our main menu state, I'm going to click on the PLAY GAME button. We now have our ship, and it's easing towards the mouse position. So you can see if I move my mouse really quickly it's not just sticking to the mouse cursor, it actually has a nice subtle easing effect, which just makes everything feel a little bit smoother. When we get later on in this course, we're actually going to add a smoke particle effect so it looks like there is exhaust coming out of this plane, but we're going to do that later on.

So that's all it took to create our hero. Again, the main theme is that for any game objects we're going to have a public update function that you're going to be able to call from your state to update its position, to check for collisions, and different types of things that happened in our game.

Show transcript

This video is part of

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

46 video lessons · 9515 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.