Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97076 Viewers
61 Video lessons · 84395 Viewers
71 Video lessons · 68607 Viewers
56 Video lessons · 101093 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.