Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Creating the Background class


From:

Building Flash Games with Starling

with Lee Brimelow

Video: Creating the Background class

So in this chapter we're going to be building our Menu state. Again, this is the first state that actually you get into when you first launch the game. It's a menu that has the star background, and it has the logo and the Play button. What we first need to do is to create a class called Background, and this is going to hold that stars image and it's actually going to scroll it, so we have an animated star background. So I'm going to go to my main source folder, right-click, create a new ActionScript Class.
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 1s
    1. Creating the Flash Builder project
      2m 40s
    2. Setting up your Starling scene
      6m 8s
    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. 8m 0s
    1. Adding the background
      1m 10s
    2. Creating the "Game Over" text
      2m 44s
    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 6s
    1. Creating sound effects with cfxr
      3m 5s
    2. Adding the sound effects
      3m 43s
    3. Creating the Score class
      4m 18s
  11. 6m 1s
    1. Starling optimization tips
      4m 8s
    2. Helpful Starling resources
      1m 53s

please wait ...
Watch the Online Video Course Building Flash Games with Starling
2h 57m Intermediate Jul 05, 2012

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.

Topics include:
  • Downloading and installing Starling and Flash Builder
  • Creating the project
  • Creating the main game class, static assets class, and other classes
  • Building the state machine
  • Creating a static assets class
  • Adding images
  • Creating sprite sheets with Texture Packer
  • Creating and importing bitmap fonts
  • Detecting collisions
  • Adding particle effects
  • Adding sound effects
Subjects:
Developer Web
Software:
ActionScript Flash Builder Flash Player Starling
Author:
Lee Brimelow

Creating the Background class

So in this chapter we're going to be building our Menu state. Again, this is the first state that actually you get into when you first launch the game. It's a menu that has the star background, and it has the logo and the Play button. What we first need to do is to create a class called Background, and this is going to hold that stars image and it's actually going to scroll it, so we have an animated star background. So I'm going to go to my main source folder, right-click, create a new ActionScript Class.

And for the different objects of our game, we're going to put it in a Package called objects. And the name of this Class is going to be Background and the Superclass is going to be starling.display.Sprite. So now in order to achieve the scrolling effect, we're going to need two copies of that sky texture that we're going to be scrolling and moving. So in the constructor here I'm going to create a couple of instance properties, and I'm going to call it sky1 and that's going to be equal to a new image object and this is starling.display.image.

And this is the display object that allows you to map a texture onto, essentially, two triangles, so we have a very light weight image. Now to the constructor of the image object we need to pass in what texture do we want to use for this image, but we already created that texture in our assets class. So I'm going to say Assets.skyTexture, and now I'm going to do a quick fix to create that as an instance variable at the top.

And as soon as I create it, I'm essentially going to add that to the display list. So I'm going to say addChild(sky1). So I'm going to copy those two lines, come down, paste in another one, because we're going to create another image, and let's also create that as an instance variable. And I want to add sky2 to the display list, and now I'm going to want to position this one off the screen to the top, so that it is lined up right behind sky1.

So to do that, I'm going to say sky2.y is equal to -800. So it's going to position it above sky1. Okay, so now what we need is a method which is going to update the position of these images. And we're going to call this function from our Menu state. So we're going to create a public function, we're going to call it update, I'm not going to be returning anything, and this is going to handle moving that background.

So what we're going to do is for each of the images, we're going to increment its y position by 4 pixels, so I'm going to say sky1.y += 4. Now what I need to do is to test whether this image is actually off of the screen, like down below the screen, and if it is, I need to move it back to the top into position to animate again. So I'm going to do an if statement, if( sky1.y = 800), then I'm going to set its y position sky1.y = to -800 and that way it's ready to start animating again.

And now we're also going to increment the sky2.y position by the same amount, so += 4; and now we need to do that same check to see if its actually off the screen. So we're going to say if(sky2.y == 800); if this is not a perfectly divisible number you could actually do, if(sky2.y > 800); but for our cases we can just check for whether it's equal to 800 and if it is, again, we're going to say sky2.y = -800.

So again, if we call this update method repetitively, it's essentially now going to animate our sky. So let's go back to our Menu state and what I'm going to do is in the init function of our Menu state, I'm going to create a new instance of that Background class. And I'm going to call it background = new Background(); and again I want to create this as an instance variable at the top, and we want to add that background to the display list of our menu.

So addChild(background); Now remember our update function for our menu is going to be called on every frame, because that's being funneled down from our main Game class. So what I want to do in this update method is to call the update method of our background. So we're going to say background.update(); Okay, so now we're going to run our project for the first time. Now if you get an error when you first try to run your project, just go ahead and open up that Spacer file, because remember we changed the package that it was in, so just go ahead now and debug your project and you can see when it launches, we have our nice scrolling background here.

Now one of the things I wanted to point out when you're using Starling is when you test your movie, or your game and debug it, the performance can often be pretty poor compared to what it's going to be when you export a release build. So if you're seeing stuttering here, this is a debug version. If you want to see the real performance of your game, you are going to want to actually export a release build of it and we'll do that towards the end of this course. But now you should see you Menu state up and we have that nice seamless scrolling stars background.

There are currently no FAQs about Building Flash Games with Starling.

 
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

* Estimated file size

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

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.