Start learning with our library of video tutorials taught by experts. Get started
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.
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.
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.