Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now that we have our main entry to our game created in the Spacer class, we now need to create our Game class, which is going to be our root Starling instance. So what I'm going to do is go into the default package here, right-click, and say New > ActionScript Class. Now we're actually going to be organizing the classes in our games into different packages, and we're going to hold this Game class in a package called core, which is going to contain our core files for our project.
And for Name we're going to give this a name of Game. For the Superclass, this is going to need to sub-class sprite, but again, we're not going to be using flash.display. Sprite, we're using starling.display.Sprite, so that's a very important distinction. It can be a little difficult at first when you first begin with Starling, because again, the class names are often times exactly the same as the traditional display list, so you need to make sure you are using the correct one. I'm going to click Finish now.
And now we have our core package with that Game class inside of it. Now you'll notice the Spacer class still has an error and that's because the Game class is not in the same package. So what we're going to do is to move that Spacer class into the core package, we're just going to drag it like that, and then we can click OK, and it's going to tell you that there is errors but don't worry about it, just click Continue, because the error will be corrected as soon as it gets into the core package. So now both our Spacer class and our Game class are in that core package.
So the first thing I am going to do in the constructor, and we don't have to call this super constructor here, is we need to listen for the ADDED_TO_STAGE event, because we don't want to do anything with our main Starling instance here, until we know it's been added to the stage. So we're going to say addEventListener. Now here again, we're going to be using the ADDED_TO_STAGE event, but this is the one that's actually in Starling, so when you import it, you want to import the starling.events.Eventclass. And you'll notice it has a lot of very similar methods or events rather than the traditional event class. Now there is also some other ones in here that are specific to Starling.
But we're going to use the ADDED_TO_ STAGE event, and we're going to have it call a function called init. So let's go ahead and create that function. So private function init, the event object again is going to be of type starling events.Event. We're not going to be returning anything here. So now inside of this main class, we're going to have an enter frame loop, and this is going to serve as our main game loop. In the init event, register and listen for the ENTER_FRAME event. So again, addEventListener, this is going to be event.ENTER_FRAME, and we're going to have it call a method called update.
Quick tip in Flash Builder, if you hit Command+1 or Ctrl+1, it will give you this quick fix menu, it will give you the option to create that method called update. And all we have to do is to add in the event object to that. So I'm going to say event, and again this is going to be of type Event. And that's our basic Game class created. Again, the main thing to understand is that once we're in our main Starling instance here, we're now going to be working with the Starling version of the display objects, so you're not going to using regular movie clips and sprites; we're now essentially in Starling world.
Here is the main entrance to our game, which is our Spacer class, this is what extends flash.display.Sprite. But again, once we're in our Starling instance, we have now switched to using the Starling versions of all of those classes. Now it is possible to mix regular Flash display list with Starling, but it's not recommended. If you're building a game, you should try to do everything inside of Starling, even things like menus and high scores and things like that, and we'll get into how you can accomplish that later in the course, but this is the main structure for our Game class.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101606 Viewers
61 Video lessons · 88367 Viewers
71 Video lessons · 72209 Viewers
56 Video lessons · 103958 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.