navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Flash Professional CS5: Creating a Simple Game for Android Devices

Flash Professional CS5: Creating a Simple Game for Android Devices

with Paul Trani

 


In Flash Professional CS5: Creating a Simple Game for Android Devices, author Paul Trani shows how to translate existing Flash skills from the web to mobile devices while designing a game in Flash and publishing it as an AIR for Android app. The finished application includes collision detection, random enemy creation and movement, shooting capabilities, multiple levels, and even a high score screen. This course also goes beyond game functionality and shows how to use mobile capabilities such as the accelerometer and gestures to control graphics, use the hardware keys to activate menus, and also how to optimize content so it plays well on mobile devices. Also included are instructions for distributing an app through the Android Market. Exercise files are included with the course.
Topics include:
  • Designing for mobile platforms
  • Creating and optimizing game graphics
  • Adding random movement and interactivity
  • Moving players based on the accelerometer
  • Using gestures
  • Detecting collisions
  • Implementing audio
  • Adding scoring and levels
  • Implementing a high score screen
  • Publishing an app on both Mac and Windows
  • Uploading to the Android Market

show more

author
Paul Trani
subject
Developer, Web, Games, Projects
software
Device Central CS5, Flash Professional CS5, Android
level
Intermediate
duration
3h 35m
released
Feb 15, 2011

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi, I am Paul Trani and welcome to Flash Professional CS5: Building a Simple Game
00:09for Android Devices.
00:11In this course, we're going to be creating a cool mobile game that's interactive
00:15and fun and fairly easy to create.
00:18This game will have all the functionality of a basic game, including shooting
00:22functionality and collision detection.
00:25You'll be able to control the player by tilting the device.
00:28We'll keep track of the enemies you destroy and show the results on a High Score screen.
00:34Now, this is specifically for mobile devices, so I'm also going to show you how to
00:39use some of those mobile features, such as gestures and how to use the hardware
00:44keys to activate the submenu.
00:46Finally, I'll show you how easy it is to put the game on the Android market
00:50for the world to play.
00:52So let's start making a game with Flash Professional CS5: Building a Simple Game
00:57for Android Devices.
Collapse this transcript
Using the exercise files
00:01If you are a Premium member of the lynda.com Online Training Library, or if
00:06you're watching this tutorial on a disc, you have access to the exercise files
00:11to use throughout this title.
00:13Here are the exercise files.
00:15They are in this folder. And again, you can see that there is Begin and Final
00:20files as well, and then there's an Assets folder that includes Code Snippets and
00:26some other graphics.
00:27If you are a Monthly or Annual subscriber to lynda.com, you actually don't
00:32have access to these exercise files, so it might be a little more tricky if
00:36you're trying to follow along, but the code snippets will be available to you
00:40for free as a free download.
00:43Now, let's dive into creating an app for Android devices.
Collapse this transcript
1. Introduction to Mobile
Understanding the user
00:00Before you actually start creating your game, you need to understand mobile phone
00:04users, which is pretty much everybody these days. And as I go through each topic,
00:09I want you to think about how you use your mobile phone, within the context you
00:15use it--so what were you doing, say, before and what will you do after--
00:19the environment you're using the phone, how you're interacting with the phone--
00:23which would be the input method--
00:25how you're holding the device--which is the orientation--and then other common
00:30metaphors you're use to when dealing with mobile phones and devices.
00:34First off, the context. What are they doing?
00:37Are they waiting in line for a lunch?
00:40Are they, say for instance, kind of watching a commercial but also kind of
00:44playing with their phone?
00:46Think about how you use your phone and the context in which you use it, and
00:50you need to create your game with this in mind. So you need to be prepared for
00:56the game to get interrupted because the commercial is over with, or whatever the case may be,
01:01and thus you also need to design for short tasks, roughly about two minutes.
01:07So maybe each level to the game might be only two minutes long. Environment.
01:12Well, where is the user?
01:13Are the inside where they can see the screen nice and clear, or are they outside
01:18where there's lots of glare?
01:19You never can really, tell so you need to design with different environments in mind.
01:25You need to make sure the graphics have enough contrast, they're easy to see
01:30and, of course, the text is easy to read, so you need to make sure that font size
01:35is large enough and that font is pretty easy to read.
01:39How is the user interacting with the content?
01:42Oftentimes it's through a touchscreen, so they are actually touching the
01:45screen of the device.
01:46They could be using the hardware buttons to navigate, say, back or to hit the
01:51Home button or using their keyboard-- if there's a keyboard on the device--or
01:57are they using device-specific capabilities to play the game, such as tilting
02:02the device and making, say for instance, the character move that way?
02:07We need to keep these input methods in mind and determine which one is going to
02:10be best for us. But when it comes to touch, which is most often how you would
02:15interact with content, we need to make sure those buttons are at least 36 pixels
02:20wide and high, and that's at a minimum. So we need to design for finger sizes,
02:25and don't forget that the finger does come with a whole hand that might get in
02:30the way or cover up content. So you need to make sure, say for instance,
02:34important content is higher up on the screen.
02:38But keep these things in mind, how the user interacts, and that's really going
02:42to help how you determine how you develop the input method. The orientation,
02:47well, how is the mobile device being held?
02:50Are they holding it horizontally or vertically when they're interacting with the
02:55content, or is it both?
02:56You to think about this orientation and make sure you design your
03:00content appropriately.
03:03Also, you want to use common metaphors. Say for instance if you need the user to
03:08advance, you might have an arrow moving or pointing to the right. Or if they need
03:13to add something, you might have a Plus sign.
03:15Also, use physical metaphors as well. Use a physical look of a calendar if you
03:21need to add a calendar, for instance.
03:23I've also added this link, www.patternpat.com.
03:26It has a ton of user an interface pattern that you can check out, and that is a great resource.
03:32The last and probably most important thing you need to keep in mind is you need
03:36to make the game fun, and it sounds pretty straightforward, but you need to
03:40continually ask yourself, "Would you play your game?"
03:43And are you having fun making the game?
03:46Because if you're not having fun with it, that might come across and, ultimately
03:50if your game is fun it gives the user a reason to play it.
03:54So keep this in mind, as well as the user in general, and it will really help make
04:00your game a success.
Collapse this transcript
Flash content on Android devices
00:00Now, I'd like to give you an overview of what Android is and the basics of
00:04creating apps for it.
00:06Well, Android is a type of software that actually was created by Google for
00:11mobile devices that include an operating system, or OS as it's known, OS,
00:17middleware, as well as key applications such as Google Maps, Gmail, things like that.
00:23Android also has an SDK, a software development kit that allows you to create
00:31applications for the Android operating system.
00:35The Android operating system can be used on cell phones, netbooks and tablets,
00:40so it can be used in a variety of devices.
00:46And for a complete list, I'd like to point you to the Wikipedia link.
00:50There is a long list since, again, it is constantly being updated.
00:54It's kind of hard to keep up-- but know that your content really will have a wide reach.
01:00Air for Android allows you to basically package up your SWF files--if you've
01:07made them in Flash Professional you can package up your SWF files into Adobe
01:12AIR apps for Android devices, which is pretty great.
01:16You package them up.
01:17They become an APK file, which is basically an app.
01:20Know that you can also take that same SWF content and make an app for Blackberry
01:27tablet operating systems, as well as apps for Apple iOS devices.
01:32So, again, your Flash content can be pretty wide reaching.
01:37You can make plenty of different types of apps really from the same basic
01:42content in Flash Professional.
01:44Air for Android also has mobile-specific features. Some of those include
01:49multitouch, so, again, swiping with your fingers, pinching and zooming,
01:54an accelerometer, which means tilting your device and having content move, screen
01:58orientations--whether it's landscape or portrait--microphone, keyboard,
02:04geolocation is defining where you are, camera roll which is accessing all the
02:09photos on the phone for instance, and then the GPU, which is the graphics
02:15processor unit, which really makes content move really fast.
02:19Now, this is a majority of the items available for Android.
02:23There are more, but these are the basics, and you need to keep all of these
02:27features in mind as you start to develop your game.
02:30Once you've created your game, you can go ahead and put that game on the
02:34Android marketplace.
02:36So, again, it allows Android developers to freely distribute all of their mobile
02:42apps to various mobile phones.
02:45They can be free apps, or you could even charge for them if you want to as
02:49well and make some money, but it's really pretty easy to do.
02:52It takes minutes to set up and is really powerful, the number of people you can
02:58get your apps out to.
02:59So the Android operating system has a wide reach on many devices. That, along
03:06with the marketplace, allows you to get your content out there to many different devices.
03:10But now let's dive into actually making some of this content.
Collapse this transcript
2. Mobile Game Setup
Reviewing the game
00:00Let's start off by reviewing the final game.
00:03So, this is what we're going to make in this course.
00:06It's called Doodle Galaxy Invaders, and I am just going to go ahead and run
00:09this file in Flash.
00:14Here it is. We have a nice animated intro.
00:16I can have whatever I want here, if I need to any sort of rules or anything like
00:20that, it would obviously go on this screen.
00:22It is a space-invaders-type game.
00:25I'm going to be controlling this spaceship by moving my mouse if I'm viewing it
00:29on the Desktop. Or if it's on the device, as I tilt the device, this ship will
00:35move in the direction of the tilt.
00:37He will automatically fire at these various spaceships that are going to
00:41come down from the top.
00:43Each time I hit one, it's going to be added to my score, and I've got to make sure
00:47three of these guys don't hit the bottom or hit me because if three do, then
00:52the game is over. And if I did well, I get a chance to entering in my name as
00:58the High Score Leader.
00:59So that's what the game play is going to look like.
01:02I am juts going to click Play and then play the game.
01:03You can see, he follows my mouse,
01:08automatically fires.
01:11(gunshots)
01:20All right, so three of them either landed or hit me, and you can see that my
01:25score of 15 does not beat the previous score of 20 by Paul, okay.
01:30So that's how the game works.
01:32That's how it's set up.
01:33It seems pretty fun.
01:34It's easy to interact with. The automatic firing is easy, and in general, I think
01:39it's a pretty fun game, and it's set up and structured nice and neat.
01:43So let's go ahead and dive into how this game was made, and really, how to make
01:47a game like this.
Collapse this transcript
Creating a file in Device Central
00:00Let's start off by creating our very first Flash file.
00:04Now, I am going to be going to be using Adobe Flash CS5 throughout this course,
00:08but I'm actually not going to start there.
00:10I'm actually going to create a file using Adobe Device Central CS5.
00:17Okay, so with Device Central CS5, it allows me to sort of create my very first
00:22file, add content to it, and then I can test that content to see how it looks and
00:28interacts back in Device Central.
00:31But again, I am going to use this as my starting point, so I am just going to go
00:34ahead and launch Device Central.
00:37Now, the first thing I am going to do is I am going to browse the various devices.
00:42So, as you'll notice, this is a great place to kind of peruse all the
00:46various devices out there.
00:48You can see the different display sizes, the creator, Flash content.
00:53This is Flash in the browser. Or you can just view by name.
00:57But what I want to do is I want to search for all the Droid devices, so I am
01:01just going to go ahead and type in Droid and that brings up all of these Droid devices.
01:06So I am going to just select by holding down the Command key, or Ctrl key if you're on a PC.
01:15I'm selecting these three, because really these three devices--the Droid
01:19Incredible, the Droid 2 and the Droid X-- are the three devices that I want to test on.
01:26So I am going to take those three and just click and drag them over into the
01:30Test Devices folder off to the side.
01:33Okay, just like that.
01:35Notice I can check out any of the specifics of each device just by selecting it,
01:42and it gives me the color depth, all sorts of things, right in here, or I can
01:47select it on the side as well.
01:49So if I go to the Droid 2, I can see that its display size is 854 x 480, and I'm
01:56given all the other details.
01:57But as you'll notice, the display size is pretty similar across these devices.
02:04So I am just going to start by selecting the Droid 2 and from this profile, I am
02:10going to go ahead and create--that's in the upper right-hand corner--a Flash
02:16file based on the Droid 2 specs.
02:19Before I click Create, I need to make sure I set to full screen, okay, so that's
02:25going to make sure my document is the full screen of the device as well.
02:30It's the same size, which is going to be 854 x 480. I'll click Create.
02:36So that ensures that your document is the correct size. As I look in the
02:42Properties panel, 854 x 480, I can see the stage color as well.
02:48Okay, so I am just going to ahead and draw a quick rectangle with the Rectangle
02:52tool, and this is basically just a throwaway file, so don't worry about the color
02:57or anything else, because all I want to do is just draw a quick graphic, go to
03:01Control, and then test it back in Device Central.
03:06So, again, the creation of your graphics, everything you need to do, and then
03:09you can test it back in Device Central, just like that.
03:14You will get this gray bar sometimes, and in order to get rid of that, go to
03:19the Display panel and make sure this is set to Fullscreen, because what's going
03:25on here is Device Central thinks you want to see this in a browser, so it's
03:30accounting for the browser bar.
03:32So it's set to Fullscreen.
03:33There is my content. I can see how that looks, and I actually can do more
03:38interaction, which we'll cover later.
03:40So that's a quick preview of how to make a profile, as well as--based on that
03:46profile--make a quick Flash file and then test out your graphics.
03:50Now if I go back into Flash, here's a file that I already have created. So if you
03:55could imagine I've created all of these graphics, I want to test these actual
03:59graphics out on the device. I can go to Control > Test Movie, again in Device
04:05Central. That's what I've been using. I can go to Test, and there's the graphics.
04:11So again, this one has some animation.
04:14I can see the graphics. Everything looks pretty good.
04:17I can make sure this is set to Fullscreen, but let's take this a step farther
04:22because using the Display panel, I want to change the Reflections to, say for
04:26instance, Indoor to see how the graphics hold up. Okay.
04:31So, so far so good. We get a little bit of a reflection. Outdoor. You can start
04:36to stimulate these different environments.
04:39And then what you want to do is adjust your graphics accordingly, okay.
04:44Same thing for, say for instance, Screen mode;
04:47I can change it from landscape to portrait and see what that looks like there.
04:52Notice how the content shifts, but this simulates how the content would look if
04:57they change the Screen mode, basically the orientation.
05:01Not only that, I did say I wanted to see how this looks on other devices.
05:04Well, I can always select this Motorola Droid X. I double-click on that.
05:09I can see that, and I can also change it to landscape and there's the Droid X
05:15with the graphics, and I can change the Reflections back to None.
05:19So again, Device Central gives you the ability to not only create your first
05:22file, but it enables you to view those graphics not only on the, say for
05:27instance, the profile that you've created the file on originally, but on
05:32different devices as well.
05:33So this is a great starting point for making your first Flash file.
Collapse this transcript
Reviewing the game structure
00:01Let's begin by reviewing the start file that already has basic game structure
00:06implemented, including graphics as well as ActionScript code.
00:11Let's start with the graphics, because on this introScreen layer is this intro
00:16screen, and I'll just double-click on it.
00:18You can see it contains animation, as well as a Play button right here.
00:24So you click the Play button and you'll get into the game.
00:28Let's go back to Scene 1. I'll turn off the introScreen.
00:32There is an options menu, with an Exit button that will be used--
00:35I'll turn that off-- and an endScreen that will populate the results.
00:40I'll turn that off. Then we have this text layer which contains some various
00:46text items. These happened to be dynamic text fields that will show the
00:50score, the lives, as well as the Level the person is on, and lastly we just
00:55have that background.
00:57So those were all the game's screens set up and various graphics, most of the
01:02graphics we'll need.
01:03And notice also in the Library panel there are some various graphics that will be used.
01:09So I am going to click back on the Properties panel.
01:11Now, let's take a look at the Actions layer. It's right here, in that first
01:16frame, is some ActionScript code.
01:19So let's go to Window, open up the Actions panel, and let's review some of this
01:25code, this basic game structure that's currently set up.
01:28So right up here at the top are going to be all of the variables.
01:32Okay, so there are four variables up here at the top, this INIT_GAME.
01:38There's this START_PLAYER State. There's the PLAY_GAME State and the END_GAME
01:45State. So at any point in time, the gameState is going to be equal to one of
01:52these items up here. Okay.
01:55We want to be aware of what gameState we're in at all times.
01:59We need to set up those variables first.
02:01Next up is the setup, so right in here, this is where we'd sort of turn on and
02:06off any graphics that we'd want to deal with, maybe adjust the screen size.
02:10That's all done in the setup, so in this case we're turning off the endScreen
02:15and we're turning off the optionsmenu.
02:16Okay let's scroll down, because next up we have this introscreen, and for this
02:22introScreen we have a Play button, and when it gets clicked on it fires off this
02:29clickAway function, which in turn moves the introScreen off. That's when it
02:35calls to this function right in here.
02:37function moveScreenOff this first line.
02:40I'll just do two forward slashes to add a comment.
02:43So basically "Move the screen off."
02:47That's what this line does.
02:50Right in here, this is where we establish the gameState, STAY_INIT_GAME. Okay.
02:59Not only that, but this trace statement is going to give me, in the Output
03:05panel, this word STAY_INIT_GAME.
03:08Trace statement will appear in the Output panel.
03:11And then, lastly for this, we are to go ahead and fire off the gameLoop function
03:21at the frame rate of the movie.
03:25This is important because this is going to keep track of what gameState we are in, okay.
03:31At the frame rate of the movie, that might be something interesting to check out,
03:34so let's go ahead and take a look at it.
03:36I'll minimize the Actions panel, and I'll just click on the background, right
03:40here for my stage, so my document is selected.
03:44And notice in my Properties panel frames per second, 24. So the gameLoop would
03:49fire off 24 frames per second, okay.
03:53That's quite a bit, and I am going to seeing a lot of these trace statements,
03:55so I really want to take that down to 1.
03:58Okay, so just for testing purposes, I want to change the frames per second to
04:031 frame per second.
04:05So at that rate, this function is going to get called 1 frame per second.
04:10So let's go ahead and scroll down a little further and take a look at gameLoop.
04:15Okay, for this gameLoop function, it's basically going to keep track of all of
04:21the games state the user is in, okay. So if they're in the STATE_INIT_GAME, if
04:28gameState is set to STATE_INIT_GAME, fire off this function. If it's set to
04:35START_PLAYER, fire off the START_PLAYER player function.
04:37PLAY_GAME, fire off that function. STATE_ END_GAME, then fire off the endGame function.
04:44So it's constantly checking to see what state you're in.
04:48If I scroll down a little further, I should see all of these functions down
04:53below, so let's take a look at initGame.
04:56So when they click the Start button, it fires off this initGame, and right in here, this
05:01is where we set up all the graphics and everything.
05:08Not only that, once they're all set up, we can change the gameState to START_PLAYER.
05:15Okay. Trace gameState, its going to just show me these words in the Output panel.
05:21But then next up, with that set to START_PLAYER, it's going to fire off this
05:26function startPlayer, so this is where we basically create the player that
05:31you're going to control.
05:32Okay, that's going to go right in here.
05:34Once he is set up, STATE_PLAY_GAME will show up in the Output panel, and then it
05:41will fire off this function called the playGame.
05:45And right in here, this is basically where you set up the enemies,
05:53explosions, et cetera.
05:56So there is a quite a few thing that are going to go right in here, so there
05:59might be multiple function calls in here.
06:03As I scroll down, makeEnemies, we should see this appear.
06:07testForEnd right in here, this is where basically "Check to see how many lives
06:20the user has etc." some other things. Check their level. All of that.
06:26But again, you can check that and all we're doing here is we are going to set
06:30the gameState to STATE_INIT_GAME, which in turn is going to go ahead and
06:35initialize this function, which is going to remove the game, which is going to
06:39appear this trace statement.
06:40It's going to turn on the endScreen and then go ahead and show the results.
06:47So if I scroll up, testForEnd fires off of this and just basically puts
06:54everything in place at the closing screen, all of that good stuff.
06:57All right, so that's the basic structure for this game.
07:01Notice right here everything is really powered by this gameStates function,
07:05which controls all of these other functions that do various things.
07:10And since I've dropped my frames per second down to 1 frame per second, we
07:14should see various statements in our Output panel, so I am going to close
07:18that Actions panel.
07:20I am going to just go ahead and increase the size of this panel because right in
07:24here, in my Output panel, I am going to see all of these trace statements.
07:27So I am going to go to Control > Test Movie.
07:29I am going to test it in Flash, and we'll see this animation go slow because it's
07:36only going 1 frame per second.
07:38That's fine. But watch what happens if I click on the Play button, and we'll just
07:41keep an eye out for my Output panel.
07:44Click Play. There we are.
07:51So it initializes the game,
07:52it starts the player, play the game so it can set up all of the enemies, into
07:57the game, and then remove the various things and show the results.
08:02So that's the basic game structure that's set up.
08:05I'd say it's looking pretty good.
08:07It's really just a matter of populating all of these functions with various
08:11things, and one of the first things is going to be to make sure we set up
08:15our screen appropriately so our content fits appropriate regardless of what
08:21size your screen is.
Collapse this transcript
Adding code snippets
00:01One thing that will really help in development of any project in Flash will be
00:05to use the Code Snippets panel.
00:07If you go to Window, open up Code Snippets,
00:10notice that there are already these folders filled with all these great Action-
00:15Script code snippets that you can use in your project. And for this title and
00:20this game, what I'm going to do is I am going to go ahead and load in some code
00:25snippets that I already have prepared.
00:28And this Code Snippet file is available for free on the lynda.com web site,
00:34associated with this course.
00:35So if you select the flyout menu, go to Import Code Snippets XML.
00:42And inside of the Assets folder, there's a Code Snippets folder, and just
00:47select MobileGame.xml.
00:50So with that file selected, click Open, and you can see that it loads in this new
00:56folder called Mobile Game, and it's filled with code snippets.
01:01Very powerful, and I can add any one of these easily, just by double-clicking on it.
01:07As I do that, it adds that code snippet to the Actions layer in my Timeline.
01:14Okay, so I have just added this ActionScript code snippet.
01:17I'll just delete that because, again, I don't need it right now.
01:20But really using the Code Snippets panel it's as simple as adding the code snippet
01:25and customizing it specifically for your use.
01:29But I'd say Flash Professional CS5 is all set up, and I am actually to further
01:34the development of this game.
Collapse this transcript
3. Basic Game Movement
Animating the intro screen
00:00Movement in animation and games is often actually done with ActionScript
00:05because ActionScript allows for flexibility over the Timeline alone.
00:11I'm going to show you exactly what I'm talking about, because what I want to do
00:14is I want to animate this intro screen out.
00:18In fact, I want it to move over to the left-hand side when you click on the Start button.
00:24I also want to do the same thing for this end screen.
00:28So again, I want to create some animation using ActionScript, and I want to
00:32animate both of these things here.
00:34So let's just go ahead and undo that move. All right. And again, I'm going to
00:40animate the intro screen, and I'm going to start off by using some code snippets.
00:46So in the previous movie, I talked about how to add to this Mobile Game folder,
00:52and I'm going to select Animate with ActionScript. And when I double-click on it,
00:57it will add that code to my Actions panel on that first frame right there, so
01:03I'll just double-click on it and it adds that code.
01:07Okay, so the first thing I have here are the import statements, okay, so I need
01:12these import statements in order for this code to work down here, because this
01:18code down here references these various classes, and that's what these are,
01:23classes and packages. So you can see TweenEvent.
01:26Well, I'm using that tween event right here.
01:30So I need these three lines, so I will just take them three and I'll cut them
01:34and I'll scroll up to the top, because I want to make sure those import
01:38statements are at the top and the code is there before I start to actually use
01:42it, because this Flash ActionScript code executes from the top down.
01:48So lets scroll down, and again down here at the bottom, I want to take this code
01:53and I want to cut it as well and put it in the appropriate area. And I'm going to
01:59scroll up. About line 18 you'll see this introScreen.
02:03When you click on that Play button, it fires off as clickAway, which then in turn
02:09fires off this moveScreenOff function, which is right here.
02:13So what happens right now is it just moves the screen's X position. It makes it
02:20equal to the screen's width, and it multiplies it minus one.
02:25So what this does is it basically figures out the screen width, takes the screen
02:32width and makes it a negative number.
02:38So that would mean if it's normally say 800 pixels wide that number in
02:43there will be minus, or a negative, 800. That's what this does okay.
02:49So that's the current functionality, and this is pretty fancy.
02:52I actually want to use that in a second, okay.
02:54But again, below that right in here, I'll just paste in that code snippet that I
03:02added earlier, and I will scroll down a little bit.
03:05Again, right in here for this intro tween I have this introTween variable, and it
03:13creates this new tween. And the first thing you needed to do is define what will
03:18be animated, and in this case I wanted to be the introScreen.
03:23So the introScreen is actually a variable that gets passed in, and inside of
03:30this moveScreenOff, it's actually known as screen, so whatever gets passed into
03:34this function it references back to that name. So all I really need right in
03:39here is not introScreen.
03:43I just need screen okay, because I'm going to use this movieScreenOff function a couple of times.
03:51So again screen refers to introScreen. It's going to move its X position. That
03:56could be Y position.
03:57It could be its size.
03:59It could be its alpha or its transparency, if you will. But I want to move it
04:04to the left and then right.
04:05Okay, so that's the property I'm going to move.
04:07This is how I'm going to move it, so it's going to move it sort of in a strong
04:11motion from the right to the left.
04:14Okay, there's bounce.
04:14There is also elastic that I can use.
04:17Next step, right here this is its starting position, its ending position, and then
04:25numbers in seconds that that transition will take place.
04:29So that's how the tween class works, okay.
04:33And for it's start position, well, I don't want it to start at zero, because,
04:37quite frankly, the screen isn't at zero.
04:39I want it to start from wherever the screen is, so I can type in screen.x, so
04:46wherever it is, start there and then move it to, not to a thousand, because
04:50that's going to move it to the right, what I want to do is I want to take this right here.
04:55I want to take what is the equivalent of minus 800, or whatever the size of that
05:01item is, and that's where I want the ending position to these. So I'll cut that and
05:07paste that right in there.
05:09In fact, I don't even need this line right there anymore.
05:14Okay, so its starting position is wherever it is.
05:17Its ending position is its width in a negative number. So that's what this does.
05:22It's going to happen over the course of one second. True means you're setting
05:26the interval to seconds okay, and what this means is it doesn't matter what the
05:32frames per second of the Timeline is.
05:35It doesn't matter. It's going to play and get it from point A to point B over the
05:40course of one second, regardless of the frames per second.
05:43But again, what I want to do--and I'm just going to pin this code, so that code
05:48doesn't moves because I want to keep it up--but I do want to change the frames
05:51per second to 24, okay.
05:55So that's what I want to happen. Everything on the timeline is going to play 24
05:59frames per second. This is just going to get there in one second. And after
06:04that's done, when that motion has finished, then go ahead and fire off this tweenFinish.
06:16Okay, so for the introTween right here, on motion_finish go ahead ahead fire off this function.
06:23Well, what do I want to happen during that time?
06:25Let me scroll down, because that's when I want to initialize the game, throw in a
06:30trace statement and add that intro frame.
06:33So I'll cut all of that code and move that right into that function right there.
06:39All right, so far so good.
06:41In fact, the last thing I'm going to do is I'm going hit this Auto Format
06:44button, just so it formats and indents all my code. I can see that this function
06:49is nice and neat now, and I'd say this is ready to go.
06:53Okay, so again I've cleaned up this moveScreenOff function. So whether it's the
06:59introScreen or something else, it's going to animates it using this Tween class.
07:05But I'm using this moveScreenOff in another place.
07:08In fact, clear down at the bottom, right here, line 132, I'm calling that same
07:18moveScreenOff. And in this case it's going to move off the endScreen. And that's
07:25the power of ActionScript animation is here I'm able to also move the endScreen
07:30as well as the introScreen, all from within one function using the Tween class.
07:36All right, so let just do a test movie.
07:42There it is. We see that Timeline animation. I'm going to click Play. We'll see
07:46it slide off to the left, and again it goes through the game, and then we have
07:53this high score screen.
07:55If I click Play Again, it's going to animate this off the screen as well, just like that.
08:00So two animations done pretty easily by using the Tween class as well as the on-
08:07motion finish. It really gives me a nice effect, and it also makes it reusable.
Collapse this transcript
Moving the player
00:00The foundation of pretty much any game is going to be how the player moves, and
00:06in this case, what I want to do is I want the player to move from left to right,
00:11the X position, based on the mouse position.
00:15So again, I'm going to go ahead and move this player spaceship to the left and
00:19right based on the mouse position. But the first thing I need to do is I'm going
00:23to dynamically add this player to the stage when it's time to play the game,
00:29okay, and I'm going to use code snippets to do this.
00:33So with the Code Snippets panel opened, I'm going to go down to Add from
00:38Library, because I want to add that player from the library, and I'll just
00:42double-click that code snippet and it adds it into my Actions frame1 is where it
00:50puts it. And let's go ahead and take a look at this code, because currently it
00:54defines a variable, because I need to give this a variable name and make sure
01:00Flash knows that it's a MovieClip.
01:02Okay, so right up here, I'm just going to call this player.
01:05And where do all my variables go?
01:08Well, they go near at the top, so I'm going to cut out that variable, scroll up
01:12to the top. It goes to the top of my Actions panel, right here.
01:18So this player variable is a MovieClip, so I'm telling Flash that's what it is,
01:25and that's how it's going to treat it.
01:27Next up, it says in the library set a movieclip linkage property to Player, okay.
01:31So I'm just going to go ahead and pin this code--again, make sure that code is
01:35pinned so it doesn't move--and I'm going to go over to my Properties panel, and
01:40I'm going to select the Library tab, and you can see that the player MovieClip is
01:45selected and its linkage property is Player, okay capital P. So that's what I'm
01:50referencing is that name right there, okay?
01:53So again, back in my code it says, new Player, capital P, those names will
01:58match up and create the instance based on that library item.
02:05It's the equivalent of just taking that MovieClip and dragging it onto the stage.
02:10So player = new Player().
02:12Well, let's go ahead and move this into place, because when do we want this to happen?
02:16Well, we want that to happen when the game first starts.
02:20So I'm going to cut this line of code, and I'm going to scroll up, and within
02:29initialize game, the initGame, I'm just going to hit Enter, hit Tab, and paste
02:34in that right there.
02:35So right here, that's what's happening: 'Pulling in the Player from the library'.
02:46So that gets initialized. The player is going to appear on the stage, but right
02:51in here, this is what we need to create the various player properties, and
02:56various things that the player can do.
02:58Okay, so start player.
02:59We want to really start its movement.
03:02So let's scroll down.
03:03That's where I'm going to add these two lines.
03:06I'm going to cut them.
03:08I'll just delete those lines as well. But again, those last two lines are going
03:14to go right in here.
03:16So create the player and the properties is what's going on right in here.
03:20I'll paste those lines in, hit Tab, and let's go ahead and review this code.
03:26I'll expand it out a little more.
03:28Okay, we're creating the player, so the instance name here, well, that's player.
03:32Okay, so again, it was created right here, and then I'm setting the player's Y
03:38position, so up and down where it is vertically. It's going to make it equal to
03:44the stage height, so if I make it equal to the stage height, it's actually going
03:48to push it off the stage, so I need to make it equal to the stage's height minus
03:53the player's height.
03:56So again, if you take a look at the Library panel, this dot right here, I need to
04:03take in account of the height of this graphic. So I need to subtract to that
04:08from the player height, and it basically pushes it up into view. So that's perfect.
04:13It dynamically gets added, and sure enough, addChild, what do we want to add? The player.
04:20So this is technically where the player appears.
04:23You can set various properties before you actually see the player, but this is
04:28where the player actually appears.
04:29Okay, so the next thing we need to do is we want this player to move,
04:33so let's go ahead and add some motion based on the mouse position.
04:39So under the Mobile Game folder in my Code Snippets panel, Move using the Mouse.
04:44That's exactly what I want to do.
04:45I'll double-click on that.
04:48It adds this Enter_Frame right here, okay.
04:54So I'm just going to go ahead and cut this, and I'm going to move it up to into
04:58position, right underneath Start_Player right in here. Okay, so at about line 88,
05:05I'm going to paste that in, okay.
05:07But I don't want this to just start firing off immediately, because this player
05:12hasn't been added yet using addChild.
05:15So I actually want to take this Enter_ Frame EventLlistener and I need to move it
05:19up into here, because this is where I define the properties, and this is where it
05:24gets added to the stage, and then he's going to start moving using this
05:29movePlayer function.
05:31So right down here, for this instance name here, again, we're using the player,
05:37move its X Position based on the stage's mouseX position, so as I move back and
05:43forth, the player is going to drop into place.
05:46Now I am talking about of course a mobile game, and again, if the user touches
05:51the screen, that's considered a mouse position.
05:54So as you can hear that mouse X, again it'll work on mobile devices.
06:00Next up, we set up a couple boundaries, so we need to make sure that it doesn't
06:06go off the right or left side, and sure enough, right in here, if the player's X
06:14position is less than zero-- and I'll just copy this name--
06:20then make its X position equal to zero. So keep it from going off the left-hand
06:24side. Otherwise, if the player's X position is greater than the stageWidth minus
06:32the player.width--so again, it's off to the right-hand side--make sure you stop
06:39it accordingly as well.
06:41Make this player's X position equal to the stageWidth minus the player.width.
06:47So oftentimes these two will be the same. You're doing a check to see if it's
06:51going beyond a certain point and then you're making sure you stop it at that point.
06:56All right, with that in place, we'll just do one more quick change, and we'll go
07:03ahead and see this player move, and that last change is basically going to be
07:07commenting out some code.
07:09So I'm going to scroll down, this gameState, STATE_END_GAME, well, I don't want to
07:17game to end, so I'm just going to comment that out by putting two forward
07:20slashes in front of it, okay.
07:22So now we're going to get the opportunity to move around that player, so let's
07:26go to Control > Test Movie > Test.
07:28There is our animation.
07:32We'll click Play, and there is our player.
07:36We can see it move with the mouse. As the mouse moves around, we can see that player move.
07:42I'm going to try to go off the left-hand side, and you can see it stop. It stops right there.
07:48Let's try to go off the right-hand side.
07:50You can see it stopped right there as well.
07:52A little bit of overlap, but again, I can just adjust this graphic, and it will be fine.
07:56This is how basic game movement can be done. Based on the mouse position, it's
08:01really just a matter of defining or getting that Mouse X position in this case,
08:06and making sure your player is equal to that Mouse X position.
Collapse this transcript
Adding enemies
00:00The next step in this game is what I want to do is I want to add multiple
00:04enemies to the stage.
00:06This enemy movie clip happens to be this flying saucer right here.
00:11So I want to add it to the stage many times, okay.
00:14So again, that's what I want to add, and I am going to add them using the
00:18Code Snippets panel.
00:20And again, I've added this Mobile Game folder earlier. That's in the code snippets
00:25movie is when I added it.
00:27And in order to add a code snippets, I'll just double-click on it. Create many
00:31enemies is what I want to do which normally, fundamentally I am against, but in
00:37this case, I think it's okay.
00:39All right, so the first thing that I am going to do is create an enemies array okay.
00:46So an array is basically a big box that can hold many things.
00:54That's what an array is. Unlike a variable that just holds one thing, an array
00:58can hold plenty of things. That's why it's called enemies.
01:02Okay, well what do we do with those variables?
01:05Well, we take them and I'll cut this variable, and all I need to do is just
01:11move it up near the top,
01:13okay, because I want to make sure all of my variables are together.
01:16So I'm telling Flash, "Hey, you know what, enemies is an Array. Treat it as such." Scroll back down.
01:24Well, where do we create this array?
01:26Well, I wanted to happen when the game initializes.
01:30So I'll cut those two lines, including the comment, and I want to find out where
01:35the game initializes.
01:37Scroll up. There we are. Starting at around line 67, this is our initGame where the
01:44players created. Just below that is where I'll paste it, and here is where the
01:52enemies array is created.
01:55So that array is created, and then I want to go ahead and start adding all of
01:59those enemies to that array when the game starts to play.
02:05Okay, so right down in here, this makeEnemies is what I want to get rid of
02:11because, again, I've added that function down at the bottom.
02:14So right down here, I want to take this function, I want to cut it scroll up.
02:21This is basically a skeleton of function. Nothing's really in it, other than that
02:24trace statement, and I'll paste it right in there.
02:28Now surprisingly, it's actually got to go okay, but let's just kind of review it.
02:32Again, when the playGame function is being called makeEnemies, that function is
02:38being called and all of these various tempEnemies are created, okay.
02:43So I'm creating this variable called tempEnemy. It's a MovieClip. And what it does
02:48is it grabs the Enemy from the library.
02:51So right over here if I look in my library, Linkage, capital E for Enemy, grabs
02:58that movie clip, and it creates this tempEnemy from it.
03:03And what it does is it gives it a speed = 3, because I actually might want to
03:08control this later on. Maybe as the levels go on, as you get further in the game,
03:12maybe the enemies get faster.
03:15The tempEnemies x position is = 400 and then go ahead and add that enemy to the stage okay.
03:24Not only that, but go ahead and push that enemy into that array as well.
03:29So take all those enemies, as you add them to the stage, you know what, put them
03:33inside of that big box known as the enemies array.
03:36So that's what's going on. Okay, now I can run this.
03:39In fact, what I can do is I can go ahead and add a trace statement, and right in
03:46here, I can just go ahead and put in, in quotes tempEnemy.
03:52So we'll just see this print out, and we will see one enemy appear, but they're
03:56all going to start being stacked one on top of the next at the X position = 400.
04:02So let's just see what this looks like. As I test this out, it's not going to be
04:06anything too exciting, because as I hit Play, so I didn't change the Y position,
04:12but it starts stacking them up right here, okay.
04:16And you could see right in here it says tempEnemy many different times.
04:20Well, it's created all of those various enemies. It's just it's puts them all in one position.
04:25So let's control that a little more, okay?
04:27Because I'd like to do is I would like to say, for instance, move those enemies so
04:32they appear at a random point along the X axis, so from the left to the right.
04:39So instead of 400, let's go ahead and enter in Math.random.
04:44As I type in rand, I can see that selected ,I can hit Enter, have the closing
04:51parentheses, and what that will do is that will get a random number between zero and one.
04:59Okay, so that's what Math.random does.
05:01Okay, so I'll just copy this and add that as a note, a random from 0 to 1,
05:13which isn't a very big number, okay. So it's going to be on the 0 pixel or the 1 pixel.
05:18So what I do is I want to multiply that by, say for instance, 800.
05:23All right, now what's going to happen is it's going to get a random number
05:29between 0 and 800 is what that will do.
05:33That great, because it's going to spread them all out, okay, but this random
05:38number actually gets anything from 0.0 to 1.0.
05:43So you could end up with something like 0.235, and those numbers I don't really
05:50want. I want to round them, so Math.round. I want to round the number either up or
05:58down depending on whichever is closest.
06:02Okay, so I want these tempEnemies to appear on whole pixels.
06:07So it's going to get a random number, and in this case it just rounds it to the
06:11highest whole number is what that does.
06:13All right, this is more like it. I think this is going to work out pretty well.
06:17Again, it's still going to put it across the X axis. The Y position isn't going to
06:22change, and that's fine.
06:24Let's test this out, and let's just see what we're up against. Click Play.
06:30You could see them all being added right up there.
06:33So it gets kind of intimidating for our player as we see all these spaceships
06:37stack up. But obviously you can see that it's working, and again, the next step
06:42would be to actually start moving all of these various enemies down the screen.
Collapse this transcript
Adding movement
00:00So let's take a look to see where this project is at currently.
00:04I want to go ahead and test this file. We have our intro, and when I click Play,
00:12you'll see lots of alien ships start to gather at the top of the screen, which
00:17does look intimidating. But at this point, you know what, I want them to basically
00:22attack, so I want these ships to start moving downward.
00:26And I'm going to do that by adding a code snippet.
00:30So in the Code Snippets panel, right down in here, Move Enemies.
00:35Again, I want to move many different things. I'm just kind of go ahead and
00:38double-click on move enemies, and it adds this code right here.
00:43Now that is plural, so it's going to move a lot of things, but let's take a look
00:48at this specifics, okay.
00:50Now, this is a complete sort of code chunk, so you can use this in many different
00:54projects, because quite frankly, there is this reference to this array.
00:58Well, I already have an enemies array, but basically you need to make sure you
01:03have an enemies array if you don't already have one. You need to create it in
01:08this case. I've already created that. Did that in the previous movie.
01:12Also, you want to create a tempEnemy from the Enemy library as well.
01:16So right over here, there is my enemy, and that's been done already as well, so
01:21I'll just remove it.
01:22So it's basically two references to it just tell you not to forget to do that,
01:27because that's what's being referenced right in here.
01:31So for that enemies array, for however many are in that box, if you will, go ahead
01:37and loop through this for loop that number of times.
01:41This tempEnemy is going to be equal to the enemies and whatever time it's
01:47through, so it's going to loop through this whole box each time.
01:50TempEnemy just refers to each and every item that comes through, but right here,
01:54this is where most of the action happens. It says "Move the tempEnemy down," so
02:03that'd be its Y position "from its current position" and then add to it plus,
02:13basically, its speed.
02:16Where does this speed come from?
02:17Well, let's a take a look. As I scroll up, right up in here, here is
02:23this tempEnemy.speed.
02:26And that's what I want to do. I want to a reference this speed number,
02:28which happens to be 3.
02:30This will change sort of throughout the game, but I do want to move up
02:34this function now, okay.
02:35So I am going to take this moveEnemies function and the various comments, and I
02:40am going to cut it, and I'll scroll up. Right underneath makeEnemies, moveEnemies.
02:49This is going to loop through the contents of that enemies array and start
02:54moving all of those items down, based on the speed.
02:58Okay, well how often is this being called?
03:00Well, let's take a look.
03:01For one, before I just wrap this up, I do want to make sure that this has :void, okay.
03:07So I don't want it to return anything or anything like that. It's just a common
03:11practice to add void there.
03:13And right up here makeEnemies, once they're made, I want to go ahead and
03:19moveEnemies like that.
03:22Okay, so that's when it's being called is right in here within playGame, which
03:26gets called plenty of times, okay.
03:29So let's go ahead to see this in action. This is actually pretty exciting.
03:33So let's go to Control > Test Movie. Let's test it out.
03:37If I click Play, well, you should see all of those enemies start to march down,
03:43and here they come. Quite intimidating.
03:46In fact, there is not much I can do, especially since I can't fire.
03:49So that works pretty darn good. I really like how all of those enemies kind of
03:53come down. But really my next step with this is I want these enemies to sort
03:58of move at, sort of a random pace and kind of move from the left to the
04:02right and even tilt a little bit as if they're actually sort of coming through the atmosphere.
04:07So that's the next step, but I'd say this is a great place to start, and now I
04:12can add individual movement to each one of these as well.
Collapse this transcript
Adding chance and randomness
00:01Let's take a look at where the game is at now.
00:02I'm going to do that by just going to do a test movie. I'll click Play.
00:09Notice that really my spaceship doesn't stand a chance against all of these UFOs.
00:14There's absolutely no way.
00:16So what I want to do is I want to create an element of chance,
00:20so maybe only a couple come down. Maybe a little more. Maybe a little less.
00:24It all just kind of depends.
00:26Another thing I want to do is for the little spaceships that come down, I want
00:31them to come down kind of wobbly and a little more random than they do now, as
00:35opposed to just straight down.
00:37Okay, so that's the goal, and what I'm going to do in order to do this is I'm
00:42going to go ahead and use this Mobile Game folder, these code snippets, and I'm
00:49going to select Random Chance is the one I'm going to use.
00:52Let's just double-click on that.
00:55It adds it to my Actions frame and right in here, notice that it establishes a
01:01variable called level, which is a number.
01:04All my variables need to go to at the top, so I'll just cut that out and scroll
01:10up to the top and paste that variable in there, because depending on the level
01:16will determine how many of those little spaceships appear.
01:20Okay, so level 1, where would I put this? Well, I would want to put this where
01:25the game initializes.
01:27So, let's just go ahead and take a look at that. So, I'll cut that and scroll up
01:37right where I initialize the game.
01:39Here we are, initialize the game between lines 60 and 70.
01:45It's where I create my players.
01:48And sure enough, right in here--
01:50in fact, I'll just put it right above those other items, level 1.
01:56Okay, so we're initially going to set it to level 1, and it's going to create the
02:00player, and it's going to create the enemies. And when I create the enemies, I
02:04want to drop this in, okay.
02:07I'm going to drop in this code where my enemies are created, so I'll cut that,
02:11scroll on up, and right in here where I create my enemies, that's actually
02:19where I want to add a little bit of an element of chance, okay.
02:23So I'm just going to paste that in.
02:26Okay, so I'm going to create this new variable, and it's called Chance.
02:29It is a number, and it's going to be a random number between zero and 60.
02:35That's my chance number.
02:37And for that chance number, if it's less than or equal to one plus the level,
02:46then go ahead and do something.
02:48In this case, what I want to happen, that do something happens to be create an Enemy.
02:55There we are.
02:56So again, open curly brace encapsulates creating an enemy and then that closing
03:04curly brace, closes it.
03:05So, I'm going to click this Auto Format button.
03:08We can see how it nests everything together.
03:10All right, so again, right in here, pick a number between zero and 60, and then
03:21right in here, if the number is less than or equal to one plus the level, so
03:35that could be two on up, okay.
03:38So it's the equivalent, at least in this first case, of typing in 2 right there.
03:44It may seem like a pretty small number, but keep in mind that this make enemies
03:48function gets fired off 24 times every second, okay.
03:53So sure, it might be a small chance that this happens, but that's going to mean
03:58less enemies that are generated, and also keep in mind that it's going to still
04:03fire off 24 times every second.
04:08Okay, so that's all I need to do.
04:09Just if you meet these requirements, go ahead and create an enemy, so let's
04:13run this. Click Play.
04:19That's more like it.
04:21This is manageable; I can probably shoot them down a lot easier.
04:25But now that they are less of them, I noticed they're pretty straightforward.
04:28They're coming straight down, and really I want to make them wobble a little bit.
04:32So let's go ahead and add some additional functionality to them.
04:36For the move enemies, I want to add a little bit more randomness than what I
04:40have right in here, okay.
04:43So let's go ahead and take a look at the Code Snippets panel, because I want to
04:50have some random rotation X and even Y movement.
04:54So I'm going to double-click on that.
04:57It adds that code down at the bottom, and I'm going to go ahead and move this
05:04code right into place. So I'll cut it and then move it right into move
05:12Enemies right here, so just paste it on in there, and then I'll click the Auto Format button.
05:21All right, let's go ahead and take a look at this now, because first off, what
05:24happens is it's automatically going to rotate the enemy between 10 and 5
05:33degrees. Technically those are just going to be the numbers.
05:39Okay, but it's just going to go ahead and rotate it, and what is it rotating?
05:42It's rotating the tempEnemy, so I'll copy that and paste it in.
05:45So go ahead and rotate it any random number between 10 and 5. Rotate it that
05:52direction. Make sure you round it, so we land on a whole number.
05:55Next up, find the rotation and move the X position in that direction, okay.
06:00So right in here, notice this looks pretty complex, and let's face it, it
06:04does look complex.
06:05Let me just paste in tempEnemy, right in here, for these instance names, and it
06:10looks a little bit daunting.
06:12But know that all that's really going on here is this is a degree, so
06:16it's getting this degree, whatever angle it's at, and then it's converting that to radians.
06:23Okay, so Flash uses radians, so that's why I need to have math sign.
06:27But this chunk of code right here, all it does is return the angle in which I
06:32want this enemy to travel, okay.
06:35So let's not get too hung up on the fact that there is some math in there.
06:38That's what it's going to do.
06:40It's going to start moving that direction based on the rotation, and not only
06:45that, it's going to move it that tempEnemy speed, which is right up here as
06:51well as within this area where we actually make the enemy, okay.
06:57So that's where that comes from.
06:59We're going to go ahead and move its X position based on the rotation. Not only
07:03that, we're going to move its Y position based on that rotation as well, and
07:08I'll just paste in where it says instance name here for all of those.
07:13Okay, keep in mind that I still have this tempEnemy Y, and I want to go ahead
07:17and remove that line as well. So remove that.
07:21This is my new movement, and that's exactly what's going to take place.
07:26In fact, the last thing I need to do is just to make sure that it stays within bounds.
07:33Okay, so if it's off the stage, move it back onto the stage, whether it's on the
07:38left or the right side, and this would be the right side.
07:43Just remove some of these empty spaces like that.
07:47I would say we're looking pretty good here.
07:49In fact, I'll just click the Auto Format button.
07:53It doesn't throw me any errors.
07:55So let's go ahead and test this out. Click Play, and there he is coming down.
08:03There are all kind of wobbliness, exactly what I want.
08:06It kind of seems like they're attacking me.
08:08I don't want to have any higher of a number because they could start traveling
08:12upwards if that's the case.
08:14You can see some more are to kind of traveling like upwards, which is fine, but
08:18again, they're trying to attack.
08:20So that's why I have that number between 5 and 10;
08:22that is the random number.
08:24But this is manageable. It seems pretty fun.
08:27I can always adjust the levels if I want maybe less of these enemies to come
08:31down, but it looks pretty good.
08:33You can see how adding this element of chance and the randomness can really
08:38create some excitement for your game.
Collapse this transcript
4. Advanced Interactivity
Adding lasers
00:00So let's go ahead and see where this file's at currently.
00:03I am just going to do a test movie. We have our intro.
00:07I'll click Play.
00:09And as I get into the game, you can see that I can move my character,
00:13but I really can't destroy any of these UFOs coming down.
00:17So I want to go ahead and add some lasers to my ship, and I want to have
00:21those lasers animate up.
00:23So those lasers are, of course, ultimately going to destroy these UFOs coming down.
00:28All right! So I am going to do that by using code snippets.
00:33So I do have this Mobile Game folder in my Code Snippets panel.
00:38And just reference the Code Snippets movie, if you don't have this.
00:42And I am going to add the Add Timer because that's what I want to do;
00:46I want to shoot this laser right over here at a certain interval.
00:50So I am going to add a timer.
00:51So I am just going to double-click on that.
00:54It adds that code to my Actions panel, right at the bottom actually.
01:00And let's take a look at this because there's an array that's created
01:04that's really going to hold all of the lasers.
01:06Well, this first variable,
01:08I want to put it where all my other variables are.
01:10So I am going to cut it, and I am going to move it clear up to the top. Ok, right up here.
01:16This is where I want to put my Lasers:Array.
01:20So I am telling Flash that that lasers is an array and treat it accordingly.
01:27Right here, this Lasers equals new Array,
01:29well, that's actually where the array gets created.
01:33So I am going to cut that, including the comment, and I am going to move that
01:37into where the game is initialized. All right!
01:40So let's move this on up right in here, this initGame function
01:47right in here. We can see the player's created, enemies are created,
01:50and right in there I can just go ahead and paste in lasers, and I'll just hit the
01:55Tab key to move that in.
01:57So amongst these other items being created, this Lasers:
02:00Array will be created as well.
02:02Now, let's take a look at the timer.
02:03So here's the timer.
02:05And since this timer is going to be dealing with these lasers that come from our player,
02:11well, I want to go ahead and take that and I want to move it up into place as well.
02:15So I am going to cut this, all the rest of that code,
02:18and I am going to move it up above the enemies, above that playGame, right in here.
02:24So you have moved player right in here.
02:26Well just below that, about line 113, and I'll paste that in.
02:31Some now let's take a look at this code, because I do have this laserTimer
02:36timer that is created.
02:37So this is its specific name, and note that it's going to ahead and execute a
02:41function every half a second.
02:44And that's what this 500 is.
02:45It's actually milliseconds.
02:47So, say for instance, if I wanted it to fire every second, then that would
02:52be 1,000 milliseconds.
02:54But right now I'll just keep that as 500.
02:56So every half a second it's going to go ahead and add this timer, and it's going
03:02to fire off this function called the timerListener right here.
03:06And this timerListener really does all of the work. It says, you know what?
03:10Let's go ahead and make a new movie clip called tempLaser from our Laser
03:16that's in our library.
03:17So let's just make sure it's in there.
03:20Move that off to the side. And sure enough, in my library you can see Laser is the
03:25linkage name of this item right here, capital L.
03:28That looks good.
03:29So, it creates it from the library, and it gives it an x and y position based on
03:36the player's position.
03:37So I am going to go ahead and pin my code-- press that pushpin--
03:41so I can select this player.
03:43OK, now let's review this, because basically it's saying, you know what,
03:47basically place the laser at the tip of the player's ship. All right!
03:54So it says it's going to place it at the player's x-position.
03:57If I just had that, it would put that laser right at this crosshair right here.
04:02I not only want it to get into that position, but I want to take in account the
04:06player's width, and I want to cut that in half.
04:09And that will give me this point right in the center right here.
04:13So that's what this is.
04:15Take the players width, cut it in half, and make sure it gets added to this x-position.
04:20Ultimately it ends up at right there in the middle, which is perfect.
04:23Place its y-position, so vertically place it right on the tip right where
04:28that ship is as well.
04:29Give it a speed of 10, which we could always change that later, and then also
04:34go ahead and put it into this lasers array as well.
04:37So, just load up to that box full of lasers.
04:40We'll need it later anyway.
04:41But do add it to the stage, and that's what addChild is.
04:45So that creates the timer.
04:47Well, in order to activate the timer, I need this laserTimer start.
04:52If I wanted to stop it, it would be, you guessed it, stop.
04:54I'll just undo that.
04:57So when do I want to start this laser?
04:59Well, I am going to cut this, and I am going to move it up into where the
05:05player is created. So right in here, about line 88 it says startPlayer and
05:10right in here addChild.
05:11That's when the player gets added to the stage,
05:13so right after it gets added to the stage, start firing the laser.
05:18So immediately after it's added, start shooting that laser. All right! Great!
05:21Let's go ahead and run this.
05:25I'll do a test movie. Click Play. Now they are just going to get attached,
05:31so you can see them being attached right next to the ship in their correct
05:34position right at the tip.
05:36That's perfect, okay so far, for what I am doing.
05:39But now I need all of these lasers to travel upwards.
05:42Okay, so as they get attached to the stage, I want them to move upwards. All right!
05:47So let's go ahead and see some of that action.
05:49And in order to do that, I am going to go to my Code Snippets panel and I am
05:54going to add Move Laser.
05:55So I am going to double-click on Move Laser.
05:58And it really is just a function.
06:00It does say that there is a Lasers array that is required, which I just
06:04added that earlier.
06:06And if there is a Lasers array, basically there is going to be this function, and
06:11it's going to go ahead and loop through that array, and for each laser in the
06:16array, which is known as i, it's going to go ahead and move it up.
06:20So that's what this says right here.
06:22Okay. Move the laser up based on the speed defined earlier,
06:30the tempLaser speed.
06:33So we go ahead and move its y-position minus equals.
06:36So whereever it currently is, go ahead and start moving it up every time this
06:41function is being called.
06:42And then if the laser, say hits the top, then I can go ahead and add in a
06:47trace statement right in here, within quotes, laser just hits the top.
06:55So I'll deal with sort of removing it later.
06:57But that's what I want to do.
06:58In fact, I am going to ahead and take this whole moveLaser function and I am
07:02going to move it back up into position, because I want it to be within the
07:06context of the code that it's dealing with, which is just beneath the laserTimer,
07:11so about line 130.
07:12Paste it in, and I would say that's look pretty darn good.
07:16The only thing I have left to do is call this function moveLaser.
07:21So I am just going to go ahead and select that function name, and I am going to copy it.
07:25And I am going to move down right in here the playGame function.
07:30While I am making the enemies and moving the enemies,
07:33well, let's just go ahead and paste that in, move that laser as well, just like that.
07:38So as long as that we are in this playGame state, the laser is going to be
07:43firing, or actually moving in this case, as well as firing if you will with that timer.
07:48So let's go ahead and test it. Click Play.
07:52Sure enough, we can see them coming out of the tip, going upwards, traveling off the stage.
07:58Everything looks pretty good.
07:59You could see laser hits the top and again it's sort of displaying that over and over again.
08:04So I would say that looks pretty darn good.
08:06The next step now is to really have more interaction between these
08:11lasers and the UFOs.
Collapse this transcript
Detecting collisions
00:00Essential to almost any game is going to be collision detection, so determining
00:05when items hit each other or if they go off the screen--some sort of detection
00:11to see where items are at and then giving a result there.
00:15In this case, I need to determine when these UFOs hit the bottom, when they
00:21hit my player, and then also when my lasers hit the UFOs, otherwise known as tempEnemy.
00:29So those are the different sort of collision detections that I need to do.
00:33All right, and what I am going to do is I am going to go ahead and just have
00:38some trace statements anytime some thing gets hit. You can see I already have
00:42some laser hits the top, so that's a sort of collision detection that's
00:46currently going on.
00:47So let's just go into my Actions panel, and as I scroll down, let's jump into my
00:59player, the movePlayer.
01:01We can see that. And in moveLaser, you can see this trace("laser hits top").
01:06Well, I really don't need this.
01:08In fact, that's probably going to get in the way, so I'm just going to delete it.
01:10I already know it works; everything is fine.
01:13Let's scroll down a little further. We can see makeEnemies, then moveEnemies, and
01:19there is already a little bit of detection going on in here because I check
01:23to see when the tempEnemy is sort of on the sides and it sort of moves them
01:27back into the playable area.
01:30Well, I need to check to see if the tempEnemy hits the bottom as well.
01:37So if the tempEnemy is sort of beyond the stage, so if its y-position is greater
01:43than the stages stageHeight, just like that, closing parentheses, two curly
01:50braces, and anything within these curly braces will go ahead and execute.
01:55Here's where I can put a trace statement. It says enemy scores.
01:59So the enemy hits the bottom, then the enemy scores, and that's what this statement does.
02:05So I check for the bottom.
02:07But I also need to check to see when the enemy hits the player, and that's going
02:12to be determined basically by performing a Hit Test Object.
02:18So right down here in the Mobile Game folder, in my Code Snippets panel, Hit
02:23Test Object is what I want to add, so I'll double-click on that, and it adds this if statement.
02:29So if the tempEnemy hits the player, then do whatever you need to in here.
02:35So I'm going to just take this, and I'm going to cut it and move it back up to
02:39where I was, which was right in here.
02:42Now, I could add it right at about line 207, like that, but really, I want to
02:52condense this code a little better.
02:54In fact, what I can do is within this if statement right here, I can add two
03:01pipes, two of these bars. Again, this basically means "or."
03:06So if the tempEnemy hits the bottom or the tempEnemy hits the player, and I'll
03:12just click and drag that with it selected,
03:15I can drag that on in there.
03:18If the tempEnemy hits the player, then also give the enemy a score as well.
03:22So that's basically combining two if statements. And in fact, I can just remove
03:26this if statement, including those comments. Great!
03:30That is actually going to work out just fine.
03:33In fact, let's go ahead and do a test movie. Click Play.
03:40So it is sort of printing out in the Output panel, STATE_PLAY_GAME and it's kind
03:45of hard to see, but I actually can go ahead and close that SWF file and check
03:52out my Output panel and see where that's at.
03:55In fact, what I am going to do is I am just going to go ahead and click and drag
03:58this Output panel over here to the Library panel.
04:02Now, I can scroll through this much easier.
04:05You can see it says enemy scores.
04:07So I did actually implement that correctly.
04:10Everything looks good, but the enemy is scoring plenty, and quite frankly, I need
04:15to score as the player.
04:16So that's the next step, but it gets a little more tricky because I need to
04:21determine when a laser hits the enemy, and both of these are basically an array of items.
04:28So I need to determine when one item in an array hits another item in another
04:33array, and I am going to do this by adding a code snippet and adding Array Item Collisions.
04:41So it gets a little more advanced, but the concepts are still there, as you'll see.
04:46So I've double-clicked on that.
04:48We can see we have this function called testCollisions, and it does check for the
04:53collisions between items in an enemy's array and a laser's array, and again those
04:59are already created.
05:01It is just with this function, it actually checks for them.
05:05So in here, in this function, it creates two variables called tempEnemy and
05:10tempLaser, and right in here, if I hit Tab, I basically loop through the
05:18enemies array, and every item in that enemy's array becomes tempEnemy.
05:25So it checks for all those items in there, and now they are known as
05:27tempEnemy, and it says hey!
05:30Loop through that and with these lasers, right in here, loop through the
05:37laser's array like that, and for every item in the laser's array, it is now
05:43known as tempLaser.
05:44So these are like the temporary names of each one of those. And then from there,
05:48I can determine when the tempEnemy hits the tempLaser.
05:54So I need to check for all the items in the enemy's array and all the items in
05:58the laser's array to determine when the enemy hits the laser, and sure enough,
06:04here's the trace statement, Laser hit tempEnemy.
06:08And lastly, we have this break statement, which just stops this whole loop from happening.
06:14So these two words match up. Just go ahead and stop this for loop.
06:18So that's how you loop through two arrays, and then you check to see when one
06:23item hits another item.
06:25Last thing I need to do is just move this up.
06:27So I'll select all this code, and I'll cut it and move it on up. Under
06:33moveEnemies, again where I have that hitTestObject, about line 210, I can paste that in.
06:42All right, so that's in the right location. testCollisions is in the right location.
06:47I will copy this name because I need to make sure this function gets called, and
06:52I want this to get called with everything else during the playGame function.
06:57So as you're moving those lasers, just go ahead and test for those collisions as well.
07:02All right, so there it is.
07:06Everything seems to be in place.
07:08Let's just do a test movie, and again we're just going to see some things
07:12happened in the Output panel.
07:14So I'll click Play.
07:15We can see STATE_PLAY_GAME, and as I start hitting those tempEnemies, the laser
07:22hits them, then you see those trace statements.
07:25Let me go ahead and close that and take a look at the Output panel.
07:29You can see the enemy scores plenty as they hit the bottom, but you can see
07:33right up in here, Laser hit tempEnemy.
07:36So it is all actually working.
07:38I performed a couple different hit tests, not only with the position of items as
07:42the UFOs come down when they collide with the player, as well as when the laser
07:47collides with the UFO, which happened to be in arrays.
07:51So the next step is to go ahead and add some explosions when these
07:55items collide.
Collapse this transcript
Adding explosions
00:00Let's go ahead and take a look at where this game currently is in
00:04its development stage.
00:06I'm going to click Play, and if I move this SWF over a little bit, notice that
00:11there is a couple of collisions going on here, these lasers with these UFOs, and
00:16we should see these trace statements appearing.
00:19So I need to actually have an explosion when these lasers hit the UFOs and when
00:23these UFOs hit the bottom or hit my player.
00:28So I need to attach this explosion from the library in a particular position.
00:34So let's go ahead and go to the Library panel, because in the Library is
00:39going to be this explosion movie clip.
00:41I'll just double-click on it.
00:43Notice it contains a couple of frames of this explosion.
00:47So this is what I want to attach to the stage and then play, and then this last
00:52frame is just a stop action so it doesn't continually loop, but that's
00:56basically what my explosion movie clip is.
00:59I am going to go back to Scene 1, and in this first frame of this Actions layer
01:04is where I want to add a code snippet.
01:05So I am going to open up my Code Snippets panel. In the Mobile Game folder, right
01:10down here is Make Explosion.
01:12Please refer to the Code Snippets movie if you don't have this. But all I am
01:15going to do is just double-click on Make Explosion, and it adds this code here.
01:21So what happens is it creates an array called explosions.
01:25So here's the explosions array, and then there's that function that's going to
01:30deal with this array.
01:31But the first thing I'm going to do is I'm going to go ahead and take this
01:34variable and I'm going to cut it, and I'm going to move it up to the top of my
01:39Actions panel. Right in here, just beneath Lasers, I am going to go ahead and
01:45paste in this variable explosions.
01:47So now Flash knows that explosions is an array.
01:51Next up, I want to go ahead and create the array, and that's what this line does.
01:56I am going to take that in the comment.
01:57I am going to cut it, and I want to create the array when the game starts.
02:03So I'm going to scroll up toward the top, and right in here, when the game
02:11initializes, these other items are created, and sure enough, just beneath Lasers,
02:16that's where I want to paste in my explosions array.
02:19So I will hit Tab just to even those out.
02:23So the explosions array is created.
02:26Now, let's scroll back down, and let's take this.
02:30So this Make Explosion, I'm actually going to take this and move this up as
02:34well. I am going to cut it, and I am going to move it up just beneath my
02:40testCollisions, about line 237 and I'll paste it on in, and let's take a closer look at this.
02:47It says add an instance of explosion from the library to the stage, and it's
02:52putting it at 400 pixels in by 300 pixels down.
02:57That's the X and Y.
03:00So let's go ahead and make sure that there is an explosion in my Library. Sure
03:06enough, its linkage name is Explosion, capital E. So here is my makeExplosion
03:13function call, and it's going to go ahead and pass in these two numbers, which is
03:17just the position of this explosion.
03:19But this makeExplosion actually creates this tempExplosion. That's the name of
03:24this new movie clip.
03:25So it basically grabs this Explosion symbol, puts it on the stage--
03:30that's what this does--and it puts it on the stage, its x position being ex,
03:36which is right here.
03:38So it's pretty much whatever property that gets passed in.
03:42If this is 400, well ex actually equals 400.
03:45So it's the equivalent of having the position be 400 right there.
03:50I'll just undo that.
03:52Same thing for the y; whatever number gets passed in to this function right
03:55here again becomes the y position, and then it adds it to the stage depending on
04:01those two positions, and then it puts it inside of this Explosions array.
04:06So that's what it does.
04:07It pulls an item from the Library, places it on the stage at this
04:11particular location is how this works.
04:14All right, I'm going to go ahead and just remove this comment, and quite frankly,
04:19I don't want to just call this function as it is right here.
04:23What I want to do is I want to determine when the--say for instance the laser
04:28hits the enemy, then make that explosion.
04:31Sure enough, that's right here.
04:33So I'm going to go ahead and take this line and I'm going to cut it, and right in
04:38here where the laser hits the enemy, well, I'm going to go ahead and replace
04:43that trace statement with this makeExplosion.
04:47That's all well and good, and that will work when it gets hit, but I need to be
04:51concerned about the position now because I want to position it at the same
04:55location as the tempEnemy.
04:57So I am going to copy that word and paste it right in here where it did say 400.
05:02Remember, that's the x position, so the tempEnemy's x position, and the second
05:09parameter right here is going to be the y position.
05:12So make an explosion exactly where that tempEnemy is and attach it to the stage
05:18and actually put in the array.
05:19All right, that looks good, and again that's just when the laser hits the enemy.
05:24Well, I'm going to copy this line because I also need to determine when the
05:30enemy hits the bottom or when the enemy hits the player, I want that enemy to blow up.
05:39So that's right here, trace("enemy scores").
05:42Well, I am going to go ahead and replace that with that Make Explosion now as well.
05:47All right! So we should see all of these various explosions appear on the screen.
05:52Notice that nothing is being removed from the stage or anything like that just
05:56yet, but we should see lots of explosions, which is always cool.
05:59Let's click Play, and again, as I come down, I'll go ahead and shoot this guy.
06:04You can see all of those explosions as I hit him, and then watch him. As he hits
06:09the bottom, it'll go ahead, and explode or has they hit my player,
06:13you can see all of these explosions all over the place. So that's great.
06:16It's all working very nice.
06:18The only issue is is that these UFOs are invincible, and the next step will be to
06:23actually remove items from the stage.
Collapse this transcript
Removing assets from the stage
00:00The next step in the development of this game is I need to go ahead and remove
00:04the various elements from the stage, because as you can see how this game works,
00:09as these lasers hit these tempEnemies, well, the enemies should be disappearing, and
00:15so should the lasers.
00:17Not only that, the enemies should be disappearing when they go off the stage, and
00:22these lasers should also be disappearing as they go off the top.
00:26And lastly, even these explosions, even though you don't see them anymore after
00:30they blow up, they still exist on the stage.
00:33So we need to remove those.
00:34There is just an awful lot of cleanup that we need to do now.
00:37I'll go ahead and close that file. And just to review where this file is at, all
00:43of these items are actually in arrays.
00:46So there is this laser array.
00:49There is this enemy array and then there's an explosion array, so I'm basically
00:53going to remove elements from these various arrays.
00:57So I am going to go ahead and open up the Code Snippets panel right down here,
01:01and in the bottom, Remove Elements from Array,
01:05that's what I want to add.
01:06So I'll just double-click on it, and it will add it to my Actions panel and let's
01:11just start here at the top. And a lot of these are similar, but let's go
01:14ahead and take a look.
01:15This one says Remove enemy 1 from the enemies array.
01:20Well, I already have an enemies array created, so that's fine, but this
01:25is what's happening.
01:26It's going to go ahead and remove enemy number 1 from that array.
01:30So this number gets passed into this function.
01:33So idx becomes 1, and in here it says removeChild, so remove from the stage
01:40enemies in that array number 1.
01:42That's what that is. I'll just undo that.
01:45But remove it from the stage. Not only that, remove it from the array as well.
01:51So this again passes in 1, and this second number here just says to remove only
01:57that one item and that's it. So I'll undo that.
02:00So that's the basic structure of this function.
02:03But when and where does this function call go?
02:07What is it called. Where does it go in my code?
02:10So let's go ahead and take it, and let's cut it, because I actually want to call
02:14that function when there is that explosion.
02:16So let's just scroll up, and what I want to do is I want to go ahead and right in
02:24here for this moveEnemies function, as I scroll down, I want to go ahead and
02:29remove that tempEnemy. When it goes off the stage or if it hits the player,
02:34that's when I want to remove that enemy. So, line 210.
02:39I'll paste it in there.
02:40Hit the Tab key a couple of times.
02:43So removeEnemy, that's when I want to call that function right in here,
02:46after that explosion.
02:48But I don't want this equal to 1.
02:49In fact, what I want to do is I need to reference this for loop.
02:53So here is the for loop.
02:55So whatever tempEnemy you're on, that's actually referred to as i, so
03:00that's what that is.
03:00I am just going to copy that, so right in here,
03:04I am just going to paste i. So whatever tempEnemy that is, it's actually known
03:07as i. I can go ahead and change that comment to that, and that looks more
03:13appropriate, removeEnemy(i).
03:16I'll go ahead and copy those two lines because I need to remove the enemy, not
03:19only when it goes off the bottom of the stage or hits the player, but also
03:23when the laser hits it.
03:25So, I'll scroll down. Right in here,
03:28there is this Make Explosion as well. Just beneath it, I'll paste in those two lines.
03:35There we are, removeEnemy. Well, is it known as i still?
03:39Well, let's take a look.
03:41The for loop for the enemy, sure enough, is i. I just want you to be aware
03:45of that. Make sure that says i, and it will get removed when the laser hits the enemy.
03:52So far so good.
03:53Let's scroll back down.
03:54So we have done the removeEnemy.
03:58Let's take a look at removeLaser.
04:00It works the same way, so there's this removeLaser function.
04:04It removes whatever number I'm referring to.
04:07It removes it from the stage and takes it out of the laser's array.
04:10So it works the same way.
04:11Well, where is it going to go?
04:13Let's first take this and let's cut it and scroll up to where we were a second ago.
04:22Test for collisions, right in here.
04:25Again, where the laser hits the enemy, right underneath this removeEnemy, I can
04:29paste in that removeLaser. Tab that in.
04:34Remove that reference to 1. And for removeLaser, what number of my referencing, or what variable?
04:41Let's take a look.
04:42It's not i because that refers to the enemy.
04:44I need to go ahead and take a look at the laser.
04:48So the laser is actually j, so that's what I need is j right here.
04:52So I'll just type in j, and again when the laser hits the enemy, go ahead and remove it.
05:00Not only that, but when the laser goes off the stage or off the top, I also need
05:04to remove it there as well.
05:07So let's scroll up.
05:09That's where we make enemies.
05:10Let's go a little further up, right here, 134.
05:14There is this moveLaser function and right in here, there is this if
05:17statement says, you know what, if it's off the top of the screen, then go
05:21ahead and do something.
05:23In this case, what I wanted to do is I will just paste in this function
05:27call, right in there.
05:29And again, let's take a look. For that variable i, in this case it is i, I
05:34can type in i, go ahead and basically remove that laser when it's off the top of screen.
05:40So that takes care of the laser when it goes off the top as well.
05:43So we have taken care of the enemy-- let me scroll back down to the bottom--the
05:48enemy as well as the laser we are removing those.
05:51Now what about the explosion?
05:53The explosion is pretty interesting because you see this explosion and then you
05:58don't see it anymore, but it's actually still on the stage.
06:01So we just need to remedy that.
06:03So we're not going to treat it the same way.
06:05So we don't even need to call this function at all. I'll just delete that.
06:09I do need this function.
06:11But how I am going to treat it is, as I scroll down here, is I am going to go
06:15ahead and check all of the items in this explosions array, all of those
06:21various explosions.
06:22If at anytime that explosion, if its current frame is greater than or equal to
06:29the total frames, so what that means is if the explosion has already played,
06:34then go ahead and remove that explosion.
06:38So this is just a for loop.
06:40That does all of that work, okay.
06:42So loop through everything, see where the current frame right is.
06:45If it's basically at the end, then go ahead and remove that item.
06:48So I need to take this and I am going to cut it, and I'm actually going to just
06:53put it in another function.
06:55I am going to put it in the moveLaser function. I'll scroll up.
07:02There it is, moveLaser, where we just were, not within this for loop. All I am
07:07going to do is just after that closing curly brace, but still leaving one below
07:14as well, and I am going to paste in that chunk of code, okay.
07:18So if I scroll up, just keep in mind I'm in moveLaser. It really doesn't matter
07:21what function I am in in particular because I just know that this function gets
07:25fired off at the frame rate of the movie.
07:27So it's constantly checking.
07:28It's always checking these various explosions, and as soon as one has already
07:33played, it's just going to go ahead and remove it.
07:36All right, I'd say we're looking pretty good.
07:38Let's scroll back down and just do a little bit of cleanup with this code,
07:41because these functions, well, I don't want to leave them at the bottom.
07:45In fact, I am going to cut them, and I want to move them into sort of the game
07:50area, where I have all my game code.
07:53So I have this Make Explosion function, this testForEnd, and then just right down
07:58below testForEnd, that's where I'll just paste in those functions.
08:02These are game functions.
08:04That's where I want them.
08:05All right, let's just go ahead and test this movie now.
08:11I'm going to click Play, and as we see those collisions happen, we'll see some
08:16explosions, but those elements will be removed, such as the UFO, known as the
08:22tempEnemy, the lasers will disappear when they collide, and even as they go off
08:27the bottom of the screen, they'll be removed as well.
08:30In fact, if I expand out the size of the stage, you can see that the lasers do
08:35disappear once they reach the top.
08:38So again, that's just a little bit of cleanup that we've done.
08:40The game is much better this way.
08:42I'm actually able to destroy these enemies.
08:44So the next thing is actually going to be implementing some explosion sounds
08:48to this game.
Collapse this transcript
Adding audio
00:00The next step in the development of this game is I really want to bring it
00:04to life with some audio, and the perfect place for some audio is just when
00:08these explosions happen.
00:10Other than that, I don't want to add a lot of audio, because we have to remember
00:14this is a mobile game and with all of this audio playing, they could be again
00:19in line to get sandwich out in the public or something like that, and they might
00:23would not want all of the this audio coming from their phone. Or they could be
00:26even listening to music through their phone as well.
00:29So use your audio sparingly.
00:32In this case, it's just going to be for these explosions.
00:35All right, I'll close that file, and let's go into the Library panel because notice
00:41there are a couple audio files.
00:43There is this explode.wav, for instance, and this is the file that I want to use.
00:48In fact, with this selected, what I can do is I can hit this little Play button
00:51right up here, and that's what I want to play when that explosion happens.
00:57Notice its linkage name is Explode, so I want to reference that, and let's
01:04just go ahead and go into our Actions panel for that first frame in the Actions layer.
01:10And when I am going to add this explosion sound is going to be in this
01:14function on line 255.
01:16When I make that explosion, that's when I want to go ahead, and right in here, I
01:21want to sort of play an explosion sound.
01:27Let's go ahead and take a look at our Library, because I am going to be pulling
01:31this item from the Library.
01:32So I am going to say variable.
01:34I am creating a new variable, and I am just going to call it sound:
01:38and it's of Sound type, so that's the type it is.
01:42It's a sound file is what I am using.
01:44But I am creating a new explode instance from this item in my library.
01:52Now it's known as sound, that particular one, so I can go ahead and the next
01:57line, I can type in sound.play.
02:00Now there is many other things you could do with the sound class and various
02:03sound items, but again, what you want to do with the sound is you want to use it
02:07really sparingly, and that's what I am doing here.
02:10I am just going to use it here.
02:11Notice there is also this shoot.wav file in here, and I could use that for the
02:17laser, but that laser is being fired off every half of a second so it gets a
02:21little annoying, to be honest with you.
02:23But with this implemented, I can go ahead and do a test movie.
02:30And now, let's check that out.
02:32I am going to click Play and try to shoot the UFOs, and we should hear that explosion.
02:39(explosions)
02:51So sound is that simple, as simple as writing two lines of code and making sure
02:56it's in your library.
02:57Just use it sparingly, but you can see how it really brings your game to life.
Collapse this transcript
5. Scoring
Adding scoring and levels
00:00This game is coming along nicely, but what it's really missing is it's missing
00:04scoring and various levels.
00:06So if I click Play, as I destroy these UFOs, the score, nor the level will add up.
00:13There will be nothing in here.
00:21So my goal is to go ahead and populate those two text fields.
00:24So let's take a look at them.
00:26I am going to turn off these three layers: the introScreen, options menu, and the endScreen.
00:31There is right in here for this text layer,
00:34if I just select the Zoom tool, I am just going to zoom in on that text here,
00:39just use my Selection tool to select these text fields.
00:43Let's take a look at them. If I select this score text field, I'll go to the Properties panel.
00:49Now notice that this text field, its instance name is score_txt.
00:55It's set to Classic Text and it is a Dynamic Text field.
00:59So I want to dynamically update the score as I sort of destroy those various enemies.
01:05Also notice that the Font Family is just _sans.
01:09So it's going to use the default sans serif fonts regardless of the device.
01:13Not only that, the size is about 24 point, which seems large normally, but
01:19it's going to look great on the phone.
01:21Same thing for my Level text field.
01:23If I select it, its instance name is level_txt, it's a Classic Text field set to
01:30Dynamic Text, and has the same Character properties as well.
01:35So I am going to start basically with the score right here.
01:39So I am going select it.
01:40The score_txt is what I am going to keep in mind, and I am going to select that
01:44first frame in the Actions layer and open up the Actions panel.
01:49So at the top what I have is I have this variable called level currently, and
01:54it is a number, because I am already actually using that and I'll show where that is.
01:58But what I want to do is I want to add another variable called score, and it's
02:03also going to be a number, just like that.
02:05All right, let's see where level is being used.
02:09So I'll just scroll down, because when the game gets initialized, the level
02:16starts out equal to 1.
02:19Well, what I want to do with that variable is I want to put that number
02:24actually in the text field.
02:26So level_txt, its Text Property is going to be equal to the level number.
02:36I wrapped that in String because what it does is it takes this, which is normally
02:40known as a number, and it converts it to a string, so it just treats it as text,
02:45since it's going in a text field.
02:47Again, I want to work on the scoring, so score is going to be equal to 0.
02:53So start out at zero, and then for the Score text field, its instance name is
02:57score_txt. Its text property, same thing.
03:03All right, so they both start out when it initializes, Level is going to be 1,
03:08the Score is going to be 0. And for the score, each time the laser hits one of
03:14the enemies, I want to add to the score.
03:17So let's go ahead and take a look at that. As I scroll down,
03:21for moveEnemies, I can see them move test for collisions. Right down in here for
03:27test for collisions I have is this.
03:30So when the laser hits the enemy, not only does it make an explosion, does the
03:35enemy get removed, the laser gets removed, but right in here, add to the score.
03:41That's what I want to do. And I do that just by typing in score++, and what that
03:48means is is it takes whatever the score is and it just adds one to it.
03:53So that's just adding one to the score.
03:55The next thing I need to do is for the score_txt, for that text field, go ahead
04:01and update it with that score variable.
04:07So it adds to the score and then it gets put in the appropriate place.
04:11So the score is all squared away.
04:14What about the levels?
04:15Where is that being used?
04:17Well, let's take a look.
04:17Let's scroll up, because when the enemies are created, this is where the level is being used.
04:24So remember, this is initially set to 1, and I will just undo that. But know that
04:31each time basically the enemies are being made, there is a chance that maybe,
04:37what, two enemies will be made, each time this function is called, so that's how
04:42that works, and that's just for the first level, there's maybe only two enemies
04:46that are going to come down.
04:47But as the levels go on, I actually want more enemies to be created, and that's
04:53why if I undo that, it's going to add say level at--let's say it's going to
04:57had 2 to it level two, so there is a chance that three enemies will come down as well.
05:03So that's the number of enemies as the game progresses, as you advance in levels.
05:09Well, how do you advance the levels?
05:11So I will just undo that because what I want to do is I want to go ahead and
05:15right down here in the check for end, testForEnd, right here in line 276,
05:24testForEnd, right in here, this is what I want to do is want to go ahead and
05:27advance the level if they reach a certain score.
05:30So right in here, I am going to go ahead and type in if the score, if it's
05:35greater than 10 for instance, then I can go ahead and advance the level.
05:41So if the score is greater than 10, advance the level.
05:45So that will be equal to 2 the first go round, and then also update the text
05:50field whichever level you are at.
05:56Okay, so that's if the score is greater than 10, then advance the level. But I
06:01always want this to be greater and greater as well.
06:03So the first go around for level one, you only have to kill 10 guys.
06:08For level two, you have to kill 20 guys.
06:11So what I need to do right in here is just--I'm going to type in
06:14level*10. Multiply it by 10.
06:19Okay, so this makes each level a little bit harder, and it's going to be 10 the
06:24first level, 20 the second level, 30 the third level, and also more of the
06:29enemies come down as well, and of course that's updated in the text field.
06:33So that's how we deal with multiple levels.
06:36It gets a little bit more advanced because more of the enemies are generated, and
06:40also I'm also adding to the score.
06:42So let's go ahead and test this.
06:50I'll click Play, and let's watch the score and the levels.
07:13So I've just right-clicked on this SWF file, just to pause it. So you can see,
07:17I have a score of 23.
07:18I am now actually up to level 3, which is great. That's exactly what I want.
07:25So the thing I need to do next is I actually need to give my guy a number
07:30of lives, and they get subtracted as these UFOs reach at the bottom, because
07:34as it stands right now, I can just play all day racking up my score with no
07:39consequences.
Collapse this transcript
Subtracting lives and ending the game
00:00At this point in the development of the game, I have scoring hooked up, I have the
00:04various levels, but I don't have the number of lives.
00:07So what I want to do is I want to add a certain number of lives to the player,
00:12and when the lives are at zero the game will end.
00:16The lives will be subtracted when one of those UFOs reaches the very bottom, or
00:22if a UFO actually hits the player.
00:24So that's how you lose lives, and I think we'll start out with 2 lives initially.
00:30Okay, so let's take a look at this text field right here. It's on this
00:34text layer. I just have those other layers turned off.
00:37I have it selected, and you can see that its instance name is lives_txt.
00:43Okay, so I want to initially give it some lives--two lives to begin with.
00:48So let's go into the Actions panel with that first frame selected in the actions
00:53layer, and let's scroll up to the top.
00:56Just right up here at the top, this is where I'll set all my variables, and I want
01:00to set another variable called lives, and it is going to be a number.
01:06And with this variable created, what I want to do is I actually want to make it
01:12equal to two when the game starts.
01:15So right down here on line 74, within this initialize game function, right down
01:22in here, this is where I want to give it a number of lives. Lives is going to be equal to 2.
01:28So that's where we'll start out and put in that text field the number of lives.
01:37Okay, so it's set 2 initially, and like I was explaining, when any one of those
01:44temp enemies hit the bottom or hit my player, I want to subtract from the number of lives.
01:49So let's scroll down into the moveEnemies area, right down here near the bottom.
01:56I have this makeExplosion, I have this removeEnemy, and I am going to go ahead
02:01and write a comment: Subtract a life, basically.
02:07So let's go ahead and make lives, subtract one from it, and that's just two
02:13subtraction symbols right there, and that's going to subtract from the current number.
02:18So that will subtract it one clear down to zero.
02:21I still need to update that text field with the lives the new number there, so
02:30that's how I do that.
02:32And I'd say that looks good. So it's going to continually subtract,
02:36so I need to check to see when the lives equal zero and then show the end screen
02:42when there it happens.
02:43So I am going to scroll down a little further. In this testForEnd, I am checking
02:49the levels various things like the score.
02:51Well, right in here check to see how many lives that the user has.
02:55That's what I want to do.
02:56So, if (lives == 0) then go ahead and do this, whatever is between these two curly braces.
03:06And in this case, what I want to do is I actually want to change the gameState, so
03:11right here I am going to just take these two lines.
03:14I am going to cut them, and I am going to move them on up in here.
03:18In fact, I am going to uncomment this one, because at this point when the
03:23lives = 0, then we need to change the gameState to STATE_END_GAME, which is
03:29then going to fire off that particular function to end the game, which honestly
03:34just shows the endScreen.
03:36So that's what's going to happen when the lives equal zero.
03:40So let's just go ahead and test this, and I'm just going to have two of the UFOs
03:50hit the bottom, or hit my player, so we could see the end screen.
04:02There we are. Great! That worked pretty darn well.
04:06I'd say that went exactly as expected.
04:08Now at this point, since I'm showing the results screen, what I want to do is I
04:12need to do a little bit of cleanup.
04:14That's really my next step, and my character is still moving, but you can see
04:18that the lives work, the game ends, it shows me this final screen, and then I just
04:23need to do a little bit of cleanup.
Collapse this transcript
Creating a results screen
00:00Creating game play is one thing, but just as important is removing this game play
00:06and showing the end screen.
00:08So let's start off and I'm just going to go ahead die immediately.
00:14Wait for those guys to reach the bottom.
00:15All right, so this is the Results screen.
00:20As you can tell, there are some UFOs that just are frozen in time are right here
00:25on the screen, and not only that, my spaceship is actually moving around, and it's
00:31still generating these lasers even though they're not moving.
00:33So just as important as creating this gameplay, I need to actually remove all of these elements.
00:38And most of these elements are actually dynamically created, so I need to go
00:43through and dynamically remove all of those items.
00:47And I am going to do this by looping through these arrays, so I need to remove
00:52the explosions, the enemies, and the lasers.
00:56So in the Mobile Game folder in our Code Snippets panel right down here, Loop
01:01through Items in an Array.
01:02That's what I want to add.
01:04It adds this code right here.
01:08All right, so I'll just hit Return to bring that up.
01:12So it loops through an enemies array, and it will do something to each one of those items.
01:18Well, I actually want this to happen when the game is over with.
01:22So I am going to go ahead and move this code.
01:24I am going to move it into the removeGame function.
01:27So I am going to cut it.
01:29I am going to scroll up right here at line 325, here is my removeGame function,
01:35and I am going to paste it right in there.
01:37And I'll just click that Format button, so everything indents appropriately.
01:42And now it says for the enemies array, loop through it and then do something for each item.
01:48Now if you do recall what we're doing for each one of these items is we already
01:53have these removeEnemy functions, removeLasers, and removeExplosion functions.
01:58So I want to use these same functions for removing all of those elements.
02:03So I'm just going to go ahead and copy this function name or removeEnemy. I am
02:06going to come down in here and paste it right over that comment.
02:11And I am going to go ahead and call that function, and I am going to pass
02:15the variable i into it.
02:17So I'll copy that and paste that right in there.
02:19So it will loop through the entire length of the array, everything in that array,
02:24and it will remove all of those enemies.
02:27All right, I need to do the same thing for the lasers,
02:31so next step is going to be Remove lasers. Take all of this for loop, paste it below.
02:40And for this array it is actually called Lasers. Not only that, I don't want to
02:46establish another variable with the same name. I actually need to change that
02:51variable name. I'll just change it to j. So wherever I see i I'll just change
02:56to j, just like that. And removeEnemy,
02:57well, no I don't want to remove the enemy; I just want to remove the Laser and
03:02type j right in there.
03:03All right, so far so good.
03:06I'll copy this for loop again, because again I need to do the same thing for the
03:10Explosions. Paste that in.
03:12And again, for the explosions, I'll just type in k for the variable. The array,
03:19again, is going to be explosions.
03:21K for that variable name, just like that, removeExplosion, just like that, type
03:31in k, and there we are.
03:34So that will loop through all those arrays and remove all of those elements.
03:38That's great. That's exactly what I want.
03:40I just need to do a couple more things in here. I need to go ahead and remove the
03:44player, and that's as simple as typing in removeChild(player).
03:51There is only one. I can call it directly by its name. And then to stop that laser
03:56timer, I'll just type in laserTimer.
04:00Again, that's what actually makes all of those various lasers, and all I need to
04:06do is stop it, just like that.
04:07All right, I'd say that it looks pretty darn good.
04:11Again, this is in the removeGame function.
04:14And again, when that endGame gets called, it's going to remove basically all the
04:19game elements like its shown in here, and then of course it shows the endScreen
04:24and does some other things.
04:26So let's go ahead and run this: Control > Test Movie.
04:31And I am going to quickly die in here, so you can see the results screen.
04:46There we are, a nice clean results screen. I don't have any of the UFOs frozen or
04:52any lasers frozen or explosions, and I'm not controlling the player anymore
04:56because the player doesn't exist.
04:58That looks great. I have ended the game correctly.
05:02Now, really the next step is to start to add some various scoring on this
05:06final screen.
Collapse this transcript
Displaying the score
00:00Once the player has completed the game, you want to go ahead and display the results--
00:04so, how well the player did. And if they've done really well, you want to give
00:08them the opportunity to enter their name to be added as the highest scorer.
00:13This is all going to be done in this endScreen layer. If I turn that on, you can
00:17see this endScreen movie clip. There is its instance name.
00:21I'll just double-click it, and in here we have a couple of different elements.
00:25First off, we have this high score text field. This is a dynamic text field.
00:31So again, this is selected, and what will be populated in here is how well
00:35they've done, their score, what level they got to, information like that.
00:40If they've done pretty well, what's going to happen is they're going to have
00:43the opportunity to enter in their name in this input text field, and it's known
00:47as High-score name.
00:48When they click Enter, it's again known as enter_btn.
00:53It will actually populate this same dynamic text field with, again, confirmation
00:58of how well they did with their name, and then the player can go ahead and play again.
01:03So that's what I want to do, and I'm going to do this all using ActionScript.
01:06So, I'm going to go back to scene 1 and I'm going to this Actions layer, that first
01:10frame, because right there that's why I'm going to add a code snippet.
01:14I encourage you to check out the Code Snippets video if you do not have these
01:18code snippets, but what I want to add is this Show Results code snippet.
01:23So I'll just double-click on it and it adds that code snippet right here.
01:27So I want to move it, but first off, I want to go ahead and review where it's going to go.
01:32Okay, so I have this Show Results function, and right in here, there is a couple
01:37of things that happen, and I do want to display the high score, but I want to
01:42have these first two lines, what they do is they basically hides the text field
01:48and the Enter button, so that's what goes on right here.
01:52Go ahead and turn off the enter button and turn off the high-score name. Just by
01:56default go ahead and do that.
01:59Next step, what this line does is we need to basically enable the play button,
02:04so enables the play button.
02:09That's what happens right here. Go ahead and make sure the play button works.
02:13And what that play button does is if they click on it--and again regardless of
02:18how well they do, we want to make sure this play button works--
02:21but when they click on it, it's going to fire off this function called Move
02:24Screen Off, which is going to move off the endScreen.
02:28So if you do recall up at the top, this is actually being used for a couple of
02:33different things, so I'll just scroll up.
02:35moveScreenOff is used for the intro screen as well, but I just want to point
02:38out then it's going to go ahead and slide its X position off. And quite frankly,
02:44I need to just make sure that it does get reset back to zero, so if you play
02:50the game a couple of times, I want to make sure that endScreen always starts in
02:56the correct position.
02:57So in this endGame function, that line 323 endScreen X position is equal to zero.
03:06If they've played the game a couple of times, you want to make sure that the
03:10game always starts out at zero and doesn't appear off to the side, because
03:14again, if you play it a second time, you wanted to make sure that it is in the right position.
03:18Okay, so again, that's the code currently in here, and what I want to do is I want
03:23to go ahead and add the results, so Show Results, and that's where this code
03:28snippet comes in right here.
03:30I'll just go ahead and take this code snippet, and I'll cut it and paste it right
03:35in here at about line 359.
03:39Paste it on there. Right here Show Results,
03:43if the score is greater than zero, which is what this says, then go ahead and in
03:48that high score text field it says, "Hey you know you made it to whatever level you
03:54made it to, with a high score of whatever your high score is that's greater than
03:58zero," and this \n just gives it a line break because on the next line it says
04:03enter your name below.
04:04All right, so right here, come back over here.
04:09I'll just add another comment: turns on the enter button and the input
04:15text field.
04:17Again, what I turned off up here, I need to go ahead and turn on if they did an
04:22okay job, if they've killed the at least one guy, then they get the chance to
04:25enter in their name and I make sure the enter button works as well.
04:31All right, moving on down, quite frankly, if they did a terrible job, your score
04:35of zero is what this really says and does not be zero, so here's where you can
04:40add whatever sarcastic comment you want, but know that this is your 'do better
04:44next time' line right here within this Else statement right here.
04:49And of course the Enter button, as soon as they click on the Enter button after
04:53it gets turned on here, after they click on it, it's going to go ahead and
04:57display their results.
04:59So it says, "Hey, great job," just confirming how well they did, their name that
05:03they've entered in, that text field, what level they made it to, and what their score is.
05:09So again, if I can just show you that, after they enter their name, they're going
05:12to click Enter that's going to display that information back up in here.
05:16And I need to turn those two items off when they do hit the Enter button.
05:21So again, right in here, I'll just add a comment if I could about line 376: hides
05:29the enter button just selected and the input text field.
05:38That's what happens there. It hides those two, but they can still click on that
05:42play button and they will and play the game again.
05:45All right, everything seems to look pretty good, but again, I want to go ahead and
05:50test this out by playing the game, so let's just test this out real fast.
05:55Click play, and again I just need to destroy at least one of the enemies.
05:59Okay, I've destroyed one.
06:07All right, so you made it to level 1 with a high score of five. Enter your name below.
06:21So again, I can just jump in here, enter in my name in this input text field.
06:26This Enter button, when I click on it, it will display my results, as well as my name,
06:30and these two items will disappear. Sure enough, that happens "Great job, Paul!
06:35You made it level 1 with a high score of 5.
06:36All right, so I have this working out great. You can see that it's populating
06:41the high score screen, but what I want to do next is I want to go ahead and save
06:46these results as my current high score.
06:48And each time I play the game, I want to go ahead and try to beat the previous
06:53high score, but again that's going to happen in the next lesson.
Collapse this transcript
Saving and loading game results
00:00This game currently does a good job of just keeping track of the level the
00:04person was on and their score when they finish the game basically, by the time
00:09all of their players die.
00:11In fact, they can even enter in their name.
00:13But none of this data is being stored, and what I want to do is I want to store
00:18the highest score, known as sort of the all-time high score.
00:22So that's what I want to store, and each time you play it's going to compare
00:26your score against the all-time high score.
00:30So that's what I am going to do.
00:31I am going to go into my File, this first frame here, and open up the Actions
00:37panel. And right down here, near the bottom, is this Show Results function, and
00:43there's a couple if else statements.
00:45But what I want to do is just after sort of turning off a couple this button and
00:51the high score name,
00:52I want to go ahead and basically create a shared object.
00:56It will store the all-time high score.
01:04That's what I am going to create. So I am going to say var so, for SharedObject.
01:11SharedObject. Again, as I type that it's highlighted and I could hit Enter to select it.
01:17SharedObject = SharedObject.getLocal. So I am going to get the local shared object.
01:26This is going to be the local shared object on their phone.
01:29And then I need to give it a name, so I can say "alltimeHighScore" and that's
01:36the shared object that's going to be stored.
01:39So I have created this shared object, and within this first if statement what I
01:43need to do is I need to go ahead and see if there is a current score in there.
01:51So for the shared object, s-o, for that shared object, the data property, score, for
01:59that score, which I'm just going to define, says for this shared objects data
02:04score property is it equal to anything?
02:07Or is it undefined. Because if it's undefined, that's when I want to go ahead and
02:15show them this message.
02:16It says, "You made it to level whatever, and go ahead and enter in your name."
02:20So again, if this is undefined, so go ahead and display this message.
02:25Now the other reason I want to display this information is if they have the highest score.
02:30So again, two of those vertical bars, those pipes as they are known, and I am
02:36going to basically say if it's undefined or if the score is greater than the
02:43data in that shared object, known as score.
02:47So if you basically got the highest score or if it's undefined, then go
02:52ahead and display this message saying congratulations, go ahead and enter in your name.
02:58So that's what this does.
02:59We want this to happen, and it will happen the first time through.
03:03So that's we are going to do.
03:04I am going to scroll down a little more. And else if the player didn't get the
03:12highest score, then display another message.
03:16It says your score of whatever doesn't beat 0. Well, your score of whatever you
03:21had does not beat--just outside of that string.
03:25I am going to add a Plus sign--so.data.score.
03:31In fact, what I also want to do is I want to go ahead and add the person's name as well.
03:37So let me just scroll down here a little more.
03:39Your score of whatever doesn't beat this other score "by" so.data.name.
03:48And then we will even.
03:49I do have to put a Plus sign right in here, but down here at the end, we will go
03:53ahead and put a period at the end of that sentence.
03:56So if your score of say 3 doesn't beat the high score of 5 by whoever.
04:01So basically that's what that says.
04:02So that looks pretty good, but I need to go ahead and save the score and even
04:08save the person's name if they happen to get the high score.
04:11So remember, we turn on the high score name and the Enter button.
04:15Let's take a look at that right down here. Again we are basically turning
04:19on and making that the enter button "work" if you will. So here it is.
04:27Here is my Enter button.
04:28It does say, "Great job," yada yada yada.
04:31It drops in their name and all that good stuff.
04:34But what I want to do in here, because this is only displayed if they haven't
04:39entered in their name yet or if they have the highest score,
04:42so in here what I need to do is I need to define all of the elements within
04:48that shared object.
04:50So define all of the properties of the shared object, known as s-o.
04:58So right in here I can go ahead in so.data.score = whatever score they had.
05:09I can even save the level that they were on, so.data.level = level and so.data.name.
05:22What about that name?
05:23I can't just type in name. What the name is is it actually has to equal to
05:27whatever they entered in within this highscore name.
05:31So I need to actually take this and copy it and paste it right in here.
05:38So for that endScreen that highScoreName text field, well, whatever is in that
05:43text field needs to be saved in the name for that shared object.
05:49The last thing I need to do is I can go ahead and say, for that shared object, I
05:55am going to actually go ahead and flush it, which is basically push that data
06:01into that shared object.
06:02That's all that does.
06:04Now if you do quit out of the game, it will automatically perform a flush, but
06:07you can always add that right in there, just to be on the safe side.
06:11All right, with all that working, I am going to go ahead and run this
06:14project, do a test movie.
06:20So the first thing I need to do is establish a score to begin with.
06:34All right, you make it to level 1 with a high score of 8.
06:37Enter your name below.
06:40Paul. I will click Enter, Great job, Paul!
06:43You made it to level 1 with score of 8!
06:46This is inside of Invaders.swf, so if you could imagine this on my phone, the
06:50great thing is this also works on the desktop.
06:54So what I'm going to do is I am just going to close this file out and I am going
06:57to open it back up again.
06:58I am not going to republish it.
06:59I am just going to go ahead and go to my desktop.
07:03Here is the SWF that was just created, and again as if I was going back to this
07:09game and playing it again, that's essentially what I'm doing.
07:12But again, it stores that data. Whether you're on the desktop or the phone,
07:16it works either way.
07:17So I am going to try to beat, say for instance, the score of 8.
07:36All right, you made it to level 2 with a high score of 14.
07:40Enter your name below. Paul number 2. Enter it.
07:45So it works just as expected, just like before.
07:47Now when I play this game one more time, what's going to happen is I am
07:50not going to hit 14. I am going to get something lower so it's not going to display either one, this
07:55text field or that button.
07:56Okay, so I will close this down, go back out to the SWF.
08:00I am going to launch this SWF, and this time I am just going to die immediately.
08:08All right, there we are. As you can see, your score of 1 doesn't beat 14 by Paul 2.
08:24So that was my second time through the game.
08:26So as you can tell, it is actually storing that data.
08:30It's storing that shared object data, also known as the Flash cookie, which is
08:34another slang term, and it works quite well.
08:38Now, there's other uses for this as well.
08:40You can not only store say the highest score, but you can also store the
08:44various levels. The person can come back and play that same game at the level
08:49they left off at, or what you could even do is you can extend it further by
08:54adding a SQLite database.
08:56But for the purposes of this game, this is really all I needed to do.
Collapse this transcript
6. Mobile-Specific Functionality
Detecting movement with the Accelerometer
00:00Currently this game works great as a desktop game or any browser because all of
00:06the interaction just moves based on the mouse position.
00:10So it will work fine on a mobile device,
00:12but what I want to do is I want to tap into mobile-specific technology, such
00:17as the accelerometer.
00:19So what that means is I want my spaceship player to move. As I tilt the device to the left,
00:26I want that player to slide to the left; as I tilt it to the right, he is going
00:30to slide to the right.
00:31So that's the interactivity that I want to introduce for this game, and I am
00:37going to do that by using the Code Snippets panel.
00:40So in the Code Snippets panel, there is this Mobile Game folder, and just refer
00:45to the Code Snippets movie if you don't have this. Right down at the bottom
00:50is this Move with Accelerometer code snippet.
00:53So I am just going to go ahead and double-click on that to add that code.
00:57All right, here is the code.
01:00I'd say that looks pretty good. It seems like a lots going on, but I am just
01:05going to break this down for you, because it is this pretty straightforward.
01:09So just like anything else that needs to be added, there is, first off, a variable
01:14that's created var accel.
01:16So I am just going to go ahead and take this, and I am going to cut it and I am
01:19going to move it up into position,
01:20up near the top, right in here. And I am basically telling Flash that accel is
01:26from the accelerometer class, so treat it as an accelerometer.
01:31Right here, this is where a new accelerometer is created.
01:35It's called accel. And next up right here, if the phone has accelerometer support,
01:48then do what you need to.
01:50In this case, there is an event listener.
01:53Basically, as the user tilts their device, it's going to fire off this
02:00function called accelMove.
02:03If the user does not have accelerometer support, we need to provide something
02:09else, or some other way for the user to interact with that content, and then right
02:13down here, here's the accelMove function right here.
02:18So let's move some of this into place.
02:20In fact, let's go ahead and take this code and I am going to cut it--the
02:25accelerometer, the if and the else statement--
02:27I am going to cut it and I am going to move it up into position, right up here,
02:33right in here for START_PLAYER.
02:36Notice here is my START_PLAYER function, and right in here this is what makes the player move.
02:42There is this Enter frame event that fires off this movePlayer function.
02:46So that's sort of the typical functionality that we've been dealing with.
02:50But what I want to do is I want to go ahead and right in here I just want to
02:53paste in that code, just about that event listener.
02:56I am going to go ahead and hit this Auto Format button, so that the code
03:00formats appropriately.
03:02All right, a new accelerometer is made.
03:04It checks for support, and if there is support, then it's going to fire off that accel move.
03:10Otherwise, if it does not have accelerometer support, well, I want to include
03:14this event listener.
03:15So I am going to cut this and paste it right in here.
03:21So it will work just as usual if they don't have accelerometer support;
03:25otherwise, if they do, just go ahead and call this Excel move function.
03:28I am going to move that function up as well, so I am going to take this,
03:31I am going to cut it, and
03:33I am going to move it up near the top right in here.
03:37Here we are, just beneath that START_ PLAYER function but before that move
03:43player function, I'm pasting in the accelMove.
03:46Okay, all of this red I need to replace, and what I am going to reference is the
03:51player, so I am going to type in player.
03:53So for the player, move its x position, move it to the left and right, take
03:58its x position -= so it's going to move in that direction based on the accelerationX.
04:04So right in here this is going to basically say that the accelerationX is a
04:10property of the accelerometer.
04:12So that's moving it to the left and right.
04:14This actually returns a number between 0 and 1.
04:24So it's not a very big number, and that's why we multiply it.
04:28Okay. The higher number, the faster it will move as you tilt the device.
04:34But there are other properties as well.
04:37There are properties such as a Y position and even the Z position as well.
04:42So you can really have a player move around in many different directions, but for
04:46our needs, we are just going to go ahead and use accelerationX.
04:50So this does all the work.
04:51It moves the character. If the player is off to the left, then make sure you
04:58stop the player. There we are, and I will just copy this name; otherwise if it's
05:02off to the right, so the stage width minus the width of the player, then move
05:09that player back into position.
05:12So that's what's going on here.
05:13It's pretty much the same thing that's going on down here with the move player.
05:18So they both move the player.
05:20And I would say this is pretty much good to go.
05:22So I can go ahead and run this, but I can't run this in Flash and expect to be
05:28able to test out the accelerometer.
05:30What I need to do is I need go ahead and test this in Device Central.
05:34So I am going to go ahead and select Device Central.
05:38It will launch Device Central.
05:40If you don't have it selected, just go ahead and select Motorola Droid 2, which
05:44was added earlier when I was browsing devices.
05:47But here it is. And what I want to do is I want to go ahead and open up this
05:51Accelerometer panel.
05:53So here's a virtual device, and as I tilt it, that's actually going to change the
05:59x property as I tilt it to the left and to the right.
06:03Okay, so that's what I am going to use to control my player. If I click play,
06:07there is my player, and I play it by of course tilting the device and you can
06:12see the player move.
06:14It's tapping into the accelerometer, and it works quite well.
06:17So I am just going to go ahead and play this for a little bit. There we are.
06:24So again, it's tapping into the accelerometer. Device Central does a great job of
06:29simulating the accelerometer, and obviously you can see how you can actually
06:34control your player using this device-specific technology.
Collapse this transcript
Using the swipe gesture
00:00One of the great things about creating games for mobile devices is also the touch screen.
00:05Now, with touch screens you can have gestures.
00:08And what I meant by gestures is you can, for instance, use two fingers to rotate
00:13an object or the same two fingers to maybe magnify an image--things like that.
00:19And you can add that into your game.
00:21And I am just going to go ahead and integrate that into this IntroScreen right here.
00:27So if they're playing this game on a device, they're going to be able to just
00:30take one finger and swipe with it.
00:34So they don't have to hit the Play button. The whole screen is basically the Play button.
00:39And when they swipe with one finger, this screen is going to slide off. All right!
00:43And I am going to do this by using the Code Snippets panel in that Mobile Game folder.
00:49I am going to add a gesture swipe.
00:52So I am just going to double-click on that, and it's going to add that code to my
00:57Actions Frame in the Actions layer down at the bottom.
01:01So it says Gesture Swipe.
01:03I am first defining the input mode as being GESTURE.
01:08Because I can also include various touch points, but gestures are basically a
01:13subset of a multi-touch touch points.
01:16So this basically is going to be really easy to use because in this case I am
01:21going to use Gesture Swipe.
01:23Also, what's available is zoom, Gesture rotate, and Gesture pan.
01:30All right, in this case I am just going to use the Swipe.
01:34So that's defined in right in here instance_name_here.
01:37Well, what do I want to swipe?
01:39I want to swipe the intro screens.
01:41Let's go ahead and take all of this code and I am going to cut it, and I am
01:44going to move it up to the top of the Actions panel, where I see this intro
01:49screen. Right here, notice this is the EventListener and the function for the Play button.
01:57So just beneath that, I am just going to go ahead and paste in that gesture
02:03swipe, right in here.
02:04And so what I want to do is I am going to go ahead and move the introScreen.
02:08In fact, the whole thing is going to be the button.
02:10So I am just going to copy that word "introScreen" and I am going to paste it right in here.
02:15So when there is a Gesture Swipe that's performed on the introScreen, go ahead
02:20and fire off this function called swipeAway.
02:23Within this function, I can go ahead and tap into a couple of
02:26different properties.
02:28Say for instance, if there is the Swipe Left, which is the offsetX, okay.
02:33So if the offsetX is equal to negative one,
02:37it's going to move to the left.
02:39So Swipe Left is going to be minus one, and then if you want to go to the right,
02:44it's going to be one.
02:47If you want to go up, then the offsetY is going to be minus one.
02:55And the offsetY, if you want to move it down, then it's going to be one.
03:01And that will move it down. All right!
03:04So these are the different swipes you can do.
03:07You can basically swipe any sort of object you want.
03:10But really in here in this if statement is where I can go ahead and do something.
03:16In this case, the thing I want to do is I just want to go ahead and move that screen off.
03:21So if they swipe to the left, go ahead and call this function
03:26called moveScreenOff.
03:27So I am just going to copy that and paste it right in here, moveScreenOff.
03:31Well, what do I want to move off?
03:33I want to move off the introScreen.
03:37So again, if they swipe the left,
03:38just go ahead and call this function, pass in that screen name,
03:42and what this tween does is this actually moves it off to the left-hand side.
03:47It's really that simple. And again it's just a simple gesture swipe is what's
03:53going to happen here on the introScreen.
03:56So normally, I would test it in Device Central.
03:59Device Central actually does have, if I do a Device Central test,
04:03it does actually have a Multi-Touch panel.
04:07But quite frankly, not all of the Multi-Touch features work in it.
04:11So the Gesture zoom, the Gesture rotate will work in here.
04:15But in this case, what I want to do is I want to test this out on an actual
04:19device to show you that this is working.
04:22So here's the game on an actual device.
04:25And what I want to do is I am just going to go ahead and swipe with one
04:28finger to the left.
04:30And you can see I am actually right into the game, and I can start playing it.
04:34Again, gesture swipe is really easy to use, and it adds additional functionality
04:39that you can use in your game.
Collapse this transcript
Using hardware keys
00:00The great thing about Android devices is that they have physical buttons that you
00:04can use to add additional functionality to your game.
00:08Those buttons include the Options menu, the Search button, as well as the Back button.
00:14And probably the most common used is going to be that Options button, or
00:18Options menu, because again, you can use it to add, for instance, this Options
00:23menu that I have right here.
00:25So just turn off the introScreen, and you will see it right there.
00:28It's called Options menu.
00:30And that's what I want to toggle on and off when you click on that Options key
00:35on the actual device.
00:37And I am going to add some functionality by using code snippets.
00:41So refer to the Code Snippets video if you don't have this Mobile Game folder.
00:46And if I scroll down, Mobile menu key, that's what I want to use.
00:51I will just double-click on it to add that functionality.
00:55And now I can put everything in its proper place.
00:58So all variables actually are going to be at the top, so I will take this first line,
01:02I'll cut it, scroll up to the top, and paste it about line 22.
01:08Paste it right in there.
01:10So, this is a Boolean,
01:11so it's a true/false statement, and it's set to true by default.
01:15So it is hidden by default.
01:18In fact, the optionsmenu, you can see its visibility is set to false.
01:23So, hiddenOptions is set to true by default,
01:26so let's keep that in mind.
01:28Scroll back down and again I am just going to go ahead and move up all of this
01:33code, because sure enough, I want it to start right here after line 440.
01:37In fact, I'll add my first comment right in here: When any hardware key is pressed...
01:47Well then, go ahead and fire off this function.
01:49So again, KEY_UP for anything, then go ahead and do this optionsKey function. Okay.
01:56So right in here, you can see Options menu.
01:58You can use this keyCode, 16777234, or a keyboard menu.
02:05Now if you use keyboard menu, it actually will throw you an error in Flash,
02:10so that's why I am going to use 16777234. Okay.
02:15But I want to add to this as well because I don't have an options key on
02:19my desktop computer.
02:20So I want to go ahead and add two vertical pipes, those vertical slashes, and
02:24what I want to do is this adds an Or statement,
02:27so I can say, if this is pressed, or, and I'll just copy this line right here. 13.
02:35And what 13 is is the Enter key on the keyboard. All right!
02:43So again, for development purposes, that's why I want to add this in.
02:50So when I test it on my desktop, I just want to make sure everything is working just fine.
02:55Okay. So if I hit the Enter key or hit the Options button right here, show the optionsmenu.
03:01That's its instance name and by default, go ahead and show it.
03:07So if it's equal to true, then place the optionsmenu on top of everything else.
03:18That's what this setChildIndex does.
03:20Because remember, all of those graphics for the game--
03:24the enemies, the lasers, even my players-- they are all pulled from the library
03:28and added directly to this stage.
03:31And they're added on top of one another.
03:33So I want to make sure this menu is on top of all of that stuff by adding a setChildIndex,
03:39making sure it's at the very top.
03:41But again, right here, I am going to copy this optionsmenu instance name and
03:44paste it over instance name here.
03:47I'll do the same thing here.
03:49So again, turn it on,
03:50put it on top, turn it on, give it an EventListener.
03:54This EventListener fires off this exitApp function right down here.
03:58And again, this is commented out as well.
04:00This is using NativeApplication functionality.
04:04This is going to go ahead and exit out of the application when they go ahead
04:09and click on the menu.
04:10Now, of course, while I am testing this out, what I want to do is I want to add
04:14a trace statement that says exitApp,
04:17just so I know it's working on the desktop.
04:19So I've added that trace statement in.
04:21It's looking good so far. I can move on.
04:24Within this else statement, I can go ahead and say "If the optionsmenu is
04:31visible, then hide it..."
04:34And that's what this does. I'll copy this optionsmenu name and paste it right in there. It turns it off,
04:39removes that EventListener.
04:41And then lastly this is just the toggle.
04:45It turns it on and off. The hiddenOptions makes it true or false each time around.
04:49So that's how we have our Toggle button created, which is our optionsmenu.
04:53And I am just going to go ahead and test this out on my device.
04:57I am going to press the Enter key.
04:59So I'll just do a Control > Test Movie.
05:00I am going to test it in Flash. Here is my game.
05:05Click Play. You saw it work. You saw the menu up here on my desktop.
05:17So the next step is to go ahead and test this out on my device.
05:21So I am just going to go ahead and put this on a Droid 2.
05:25Here is with the game on a Droid 2.
05:27I am going to go ahead and click Play.
05:29So I am able to play the game.
05:33Then I can hit the Options button.
05:36It shows the Options menu.
05:39I can press the Options button again
05:42and the Options menu is hidden.
05:44So again, that's how you can tap into the actual hardware keys of the device to
05:50extend the functionality of your game.
Collapse this transcript
7. Optimizing for Mobile Devices
Handling exits and idle mode
00:00As you're developing your game, you need to keep in mind how this is going to
00:04play on an actual device.
00:06In the case of this game, since we're controlling the player based on the
00:10accelerometer by tilting the device, we're not touching the screen at all,
00:14so, there is a chance that the screen could actually dim while we're playing game.
00:19So I actually want to keep the screen awake, is a mobile- specific thing I want
00:23to do, as well as I want to make sure this Exit button works.
00:27So let's go ahead and take a look at that.
00:28I am going to use a code snippet in order to do that,
00:31and just refer to the Code Snippets movie if you don't have this Mobile Game folder.
00:37I am going to scroll down, right down here, Keep Screen Awake.
00:42So I am going to double-click on that,
00:44and that's going to add these two lines. All right!
00:48So this looks good, and this is really going to be mobile-specific technology
00:53that we're dealing with.
00:54Let me just hit Enter a couple times.
00:57So keep the screen awake if you're using the accelerometer or whatever else.
01:01Notice as I uncomment this line, it's using this SystemIdlemode.KEEP_AWAKE.
01:09So this will keep the screen active while the user plays the game. Okay.
01:14So, and this might be great if you have, say for instance, a lot of text or any
01:18time that the user will not be interacting with the screen but is probably
01:22using the app or playing the game. All right!
01:24So that's in place, and I've just uncommented that.
01:27It should be just fine.
01:28Also right in here, and this was added earlier,
01:31it's the same thing.
01:33So I am still using this NativeApplication and then exit.
01:37This will exit out of the app, okay.
01:40And these are specific to AIR,
01:43so this is AIR for Android devices functionality.
01:49And this is also AIR functionality as well.
01:55And I make that distinction because this is what's going to happen when
01:58you test this in Flash.
02:00So I am going to do a test movie,
02:04and you'll notice immediately that it already throws a couple of errors. Okay.
02:09It says line 471 and line 476 says, undefined property NativeApplication,
02:16and that's because NativeApplication is specific to AIR.
02:20Okay, so it has to be an app in order to use that functionality, and not a
02:25SWF, like I have here.
02:27So it actually is working.
02:29I don't want that error to concern you at all.
02:31But for right now all I need to do is just comment those two lines out and make
02:37sure I ACTIVATE or, let's say, UNCOMMENT ON PUBLISH. All right!
02:46That's what I want to do for both of these.
02:48So I am just adding these notes in here, and when I get in to the publish
02:51settings, I am going to go ahead and uncomment these.
02:54There is something else I need to do down here.
02:56So I need to uncomment this line on PUBLISH, but I also need to set the
03:02necessary permissions as well.
03:08Okay, so there is going to be a certain file, and I just need to basically tell
03:12Android that it's going to keep the screen awake, and just to make sure that
03:16permission is in there.
03:17So that's one thing I need to do as well.
03:19But this is actually set up, and it will work just fine.
03:22So again, when I get to the point where I publish out this AIR app and play it on the phone,
03:26I will test for this functionality and everything will work out fine.
Collapse this transcript
Handling activation and deactivation
00:00Since this is a game for mobile devices, what we need to do is figure out how to
00:05handle the game, say for instance, when the user gets a phone call, or when the
00:10user launches another app.
00:12So we need to handle this activation and deactivation of this game, and I'm going
00:18to do that by using the Code Snippets panel. Down here there is App
00:24Activate/Deactivate.
00:25So I'm going to go ahead and that to that frame on my Actions layer where all my
00:33other code is, right in here.
00:36So there's a couple of EventListeners right in here. So the first one is going
00:40to listen for the deactivate, so anytime you know, say for instance, when the user
00:47gets a phone call or launches another app, and this app is actually basically in
00:57background mode. What we do with everything?
00:59Well, what we do with that information is we're going to go ahead and pause the
01:03game anytime the game is deactivated or running in the background.
01:08We want to pause it because not only do we not want the game to continue to play
01:12and watch our players die without us knowing it, but we also don't want to tax
01:17the battery by having various functions run in the background.
01:21Next up, when the user comes back to the game, what happens? All right and
01:28that's when this resume game function fires off, and both of these functions
01:33are right down here.
01:34So I need to determine what listener events are running, timers, whatever is
01:39running, and then pause those elements. And once the user comes back to the game, I
01:45need to resume those elements as well.
01:47So let's go ahead and take a look, and quite frankly, I really only need to pause
01:53the game when the game is in one state okay, because if they are on the intro
01:58screen or on the results screen I don't need to pause anything.
02:01But if the gameState is equal to STATE_ PLAY_GAME, then go ahead and pause the
02:17necessary elements or stop them from running.
02:20Same thing. I am going to just copy that if statement and paste it right down in
02:24here for the resume game as well.
02:27So let's go ahead and populate this, so let's take a look at the game state.
02:31First off, I just scroll up near the top, and where I want to start is there is
02:38going to be this game loop that runs continuously okay, and I want to stop this
02:44Enter frame. And this is the big thing you need to look out for is intro frames
02:48can be expensive, as far as the processor goes, because its executing at the
02:52frame rate of the movie.
02:54So I want to copy this ENTER_FRAME addEventListener, scroll down, paste it right
03:03in here within this if conditional statement, and change add to remove.
03:08Now that event listener will be removed, and when the game is resumed I need to
03:13make sure that that event listener continues.
03:16All right, next up, I need to go ahead and take a look at some of the game play.
03:20Say for instance, I know the player starts moving around and all that good stuff
03:25when we get back into the game, so start player, we have this laserTimer start,
03:32so that laser is constantly firing every half a second.
03:35Well, I need to stop that laser as well, so I'm going to take that line right
03:39there on 120, line 120 here.
03:43Also, if you notice there's also this accelerometer that's being executed as well.
03:48But first, let's paste in this laserTimer start and change it to stop.
03:55Okay, so that will stop the laser, and I need to resume it, of course, when they
04:00go back into the game. Let's go back up to where we were, right in here, and again since they're on a
04:06mobile device it's calling this accelMove basically every time the user tilts
04:14the device, so I need to stop this from happening.
04:17So I'm going to take this EventListener and put it down in here as well, change
04:22add to remove, and also paste it right down in here.
04:28That's actually going to work out pretty well.
04:31In fact, both of the deactivate and activate are going to fire off and work
04:36appropriately, but I want to pause the game in another place as well.
04:40Okay, so I'm just going to copy this pause game, because if you recall, I also
04:45have that Options menu right, okay.
04:49So if they click on that Options menu button, it's going to go ahead and enable
04:56the Options menu. Well, at that point, again, I also need to pause the game, and then
05:03when they press it again when the menu goes away, then I need to resume the game,
05:08just like that. So again, call that function, and now we are set.
05:15So both of those in place, I can go ahead and test this out in Flash to
05:20make sure it's working. Let's run this.
05:24So I'm going to play the game. I'm going to then hit the Enter key and that's going to
05:29show the Options menu, and then I'm going to hit the Enter key again and then the
05:34gameplay will resume. There we are.
05:41You can see how it's paused the game.
05:43I can see my lasers have stopped, this interframe has stopped as well, everything
05:47has stopped moving and that works great.
05:49If I hit the Enter key again, it will continue.
05:55So again, this is one of the things that you're going to have to do is deal with
05:58activation and deactivation of your application, because it's really going to
06:02help your user's battery life and also it's going to enable better game play
06:08because it's going to pause and resume the game at the appropriate times.
Collapse this transcript
Optimizing graphics
00:00One thing you are going to want to do with your game is you are going to want to
00:03optimize all of the graphics so it plays well.
00:07This is particularly important on mobile devices, because, again, they just have
00:12smaller processors. They are running on about a 1-GHz processor, and in general,
00:18I need to make sure these graphics perform well.
00:21What's interesting is that on mobile devices, vector graphics don't perform as well
00:26because these small phones have to do all these calculations of all of these
00:31various curves for vector drawings.
00:35So what I want to do is I want to take my important vector graphics and
00:40convert them into a bitmap.
00:43Flash actually allows me to do that.
00:45For instance, if I just come to this text layer right here and I'll just zoom in,
00:51for instance, on some of this text here,
00:53well, this is actually quite frankly, this is some text here that is
00:58vector information.
01:00Okay, so what I want to do with this is I am just going to drag this text into
01:04the library, and I am going to call it text.
01:06Okay, so this could be anything on the stage. In this case, it's just some text.
01:11But I want to take this vector information and in the Properties panel, I want
01:16to cache this as a bitmap.
01:20So under display, cache this text as a bitmap.
01:24This happens at runtime.
01:26So this is exactly what I want to do and this is going to work out quite well.
01:30Well, for most of my game play all of the graphics are actually
01:35dynamically created.
01:37They are pulled from the library, if you will, and what I need to do is I need
01:42to go ahead and cache these as bitmap as well. But again, they're added using ActionScript,
01:48so I need to use a code snippet to be able to make sure those are all cached as bitmaps.
01:56So right down here, there is Optimize Graphics.
01:59That's the code snippet I want to use. Just double-click on that, and it's going
02:04to add basically these two code snippets right in here.
02:07Okay, so this first comment says, this is great for graphics that move along the
02:12x and y position or not at all, okay.
02:15So this is a perfect case, for instance, for my lasers and my player.
02:23So that's what I'd want to use this for, cacheAsBitmap = true.
02:27The second one down here--and I'll just hit Return a couple of times--
02:31this is great for graphics that scale, rotate, or even maybe change their alpha
02:35or their transparency.
02:37So for this one, I actually want to use this for my enemies.
02:40Okay, just like that.
02:44All right, so let's start with this first one right here.
02:46I am going to take this line, I am going to copy it, and I am going to go up to
02:51where my player is created.
02:53I should have, actually, a start player right up here.
02:57Scroll up a little more, right in here.
02:59Here's my start player, and I'm going to look to where these properties are
03:04added, because the CacheAsBitmap is basically a property, so that's what I
03:08want to add, and I am going to add it before the addChild, before I get to put onto the stage.
03:13I am going to paste in that line.
03:16So for instance name here, that's going to be player, all right.
03:20So my player is going to be cached as a bitmap. Let's take a look.
03:25We have this LaserTimer.start ().
03:27That's referring to the laser timer timer class.
03:31So I'll scroll down, and that's right here.
03:34This function, line 171, right in here after the x and y, I want to go ahead
03:40and paste in that line, and for all the tempLasers, go ahead and cache them as bitmaps as well.
03:47Okay, so those two are set up.
03:48Let's scroll down a little further, and I don't need these lines, so I am just
03:53going to delete those lines of code, and right in here
03:56I am going to go ahead and use this cacheAsBitmap matrix, and that's going to
04:01enable me to cache all the different instances of these enemy spaceships.
04:06So as they rotate, it's actually going to cache all of those rotations as a
04:10separate bitmap is what this does right here.
04:13But I still also need to include the cacheAsBitmap as well.
04:16So I am going to take these two lines, I am going to cut them, and I'll just
04:21delete those comments down there at the bottom.
04:23Now let's scroll up where we have the makeEnemies.
04:28Here we are, makeEnemies, about line 229.
04:31Here is this function, right in here, sure enough.
04:36Here is that property for moving its x position, and I'll paste those two
04:40lines right in here.
04:42So again, I'm dealing with tempEnemies, so I am just going to copy that instance
04:47name and paste that in where it says instance_name_here, just like that.
04:52Okay, there we are, so even the tempEnemy as it rotates, because it will rotate as well,
04:58well, I want to go ahead and cache each one of those as a bitmap.
05:02You might be asking, what do you do with animated graphics?
05:05Well, let's take a look, because there is that explosion as well.
05:09So here's this explosion, and I'll just double-click on it, and I'll zoom in on this explosion.
05:15You can see that this is vector data right here, okay.
05:19So this is just a vector line that I created in Flash, and I want to replace
05:23these actually with bitmaps.
05:25So the cacheAsBitmap or cacheAsBitmap matrix does not work on animated movie
05:31clips like these, because there's one frame, two frame, and then three frames.
05:36So in this case what I want to do is I am going to create a new layer and I'm
05:39just going to import some bitmaps of that same explosion.
05:43So I am going to import to stage. In the graphics folder, I am going to select this
05:47Explode_1, and since these are all numbered, it's going to bring in all three of
05:55these, and that's exactly what I want.
05:57So this is PNG file that was created actually from this original vector.
06:01I just took a screenshot, cropped it down, and saved it as a PNG file with
06:06transparency, and I'm going to select Open, and it's going to bring in this entire sequence.
06:13So those three images, I'll select yes, and there they are.
06:18I can first kind of space these out, each keyframe, so these explosions
06:25appear at the appropriate time. And not only that, I can get rid of these two
06:30layers below just by deleting them, clicking that trashcan, and deleting those two layers.
06:35The last thing I am going to do is I am just going to go ahead and like center
06:39those graphics kind of like that.
06:41All right, there we are, and I'd say we're good to go.
06:46Lastly, here are my PNG files.
06:48I can go ahead and move those into the assets folder, just to do a little bit of cleanup.
06:52But again, for animated graphics you might consider using bitmaps by importing them;
06:57otherwise, you can use cacheAsBitmap or cacheAsBitmap matrix, just as long as
07:03you are basically taking most of your vector content that's moving around a lot
07:07and converting it to bitmap.
07:09It really is going to help performance on a device.
Collapse this transcript
Optimizing ActionScript
00:00Optimizing your ActionScript code is just as important as optimizing your graphics.
00:06That's what I want to do now.
00:07I am going to go into my ActionScript code. Here it is, and quite frankly,
00:13there's a lot going on.
00:15I need to optimize this, and what that means is I need to go ahead and look
00:19at ways that I can condense down some of this functionality, whether there's
00:24enter frames that can be condensed down maybe into one enter frame, or if there are
00:29for loops that can be condensed down as well.
00:32I also need to take a look at expensive processes, and when I say expensive
00:37processes, I mean, more than anything, enter frame events. Okay.
00:42So I am going to go ahead and take a look at my enter frame events, and I want to
00:47make sure there are not a lot of them, for one, okay.
00:49So as I scroll down, about line 62 there's this Enter_Frame game loop, and this
00:55constantly checks the game state we are in. And that's fine, but what I am going
01:00to do is I am going to select this word and I am going to do a Command+F, or
01:04Ctrl+F if you're on a PC, and I am just going to do a Find Next.
01:09All right, here is another one.
01:11Here is the enter frame for moving the player.
01:14Well, I need to do that as well, so that one is okay to keep in as well.
01:19Find Next, and again, this is a remove event listener.
01:23I, in general, only have two enter frame events: one for the game loop and then
01:30one to move the player.
01:32So that looks pretty good.
01:33But I want to go beyond an enter frame event, just looking at how many there
01:38are, because I need to be aware of how often this enter frame event is firing
01:43off, and it does so at the frame rate of the movie.
01:46So I am going to pin my code, and I am going to take a look over here in
01:50my Properties panel.
01:51The Frames Per Second for this document is 24.
01:55I might consider dropping that down to, say for instance, 20 frames per second.
01:59Okay, it should still play smooth and look pretty good while not being that
02:05taxing on the mobile device.
02:07If I drop it down to say 15, it's going to get a little more choppy, so I think
02:1120 is pretty good for this project.
02:13Okay, so the enter frame looks good.
02:16I am going to go ahead and just take a quick glance at some of the other code as I scroll down.
02:23Let's go ahead and take a look at, let's say for instance this timer event.
02:27This works out pretty well, and these are really good to use because I get to
02:30determine, independently for each timer that is, how often it executes, and that
02:35one executes every half a second, so that's pretty good.
02:38I might want to take a look at maybe moving my enemies or even, like, testing the
02:42collisions for instance.
02:43Let's take a look at these testing collisions.
02:46284, that's where the function is, and I am noticing that this loops through the enemies array.
02:53Okay, it loops through the enemies array, because it checks to see when an enemy
02:58and a laser collide.
03:00So that's what this test for collisions does, okay.
03:03But I might consider combining this with something else.
03:06In fact, if I scroll up, I can see that with this moveEnemies there is this same for loop.
03:15So I want to go ahead and combine these two.
03:17In fact, I want to get rid of this test collisions altogether.
03:21All right, so in order to combine this, first thing I am going to do, since I am
03:25not going to need this anymore, I am going to remove that enemy for loop.
03:28Anytime you remove an opening curly brace, you need to remove the closing curly
03:34brace as well. Same thing for this function as a whole.
03:37I can remove it, remove that closing curly brace, and I don't need this tempEnemy
03:46anymore either, because it's going to be up above. There we are.
03:50So this is what I have it condensed down to.
03:53So this is a lot better.
03:54I have eliminated that for loop.
03:56I can go ahead and take this code, and in fact, I did notice one more thing right
04:01down here for this break enemy.
04:03Well, that doesn't exist anymore.
04:05It's not in that for loop, so I can remove that as well.
04:07So again, I am going to take this chunk of code and I am going to cut it, and
04:13right up here within this for loop, right in here, after this closing curly
04:21brace right here, hit Enter, and this is for the Make Explosion, and paste that in.
04:32Lastly, I will go ahead and click my Auto Format button, just to make sure
04:36everything nests in nice and neat, and I'd say that looks pretty good.
04:40But keep in mind that I did go ahead and I removed that test for
04:45collision basically.
04:47So right up here in this playGame, since that testCollisions function no longer
04:52exist, I need to remove it as well.
04:55And anytime you're removing a function, it's always good just to select it and
04:58again just find it, so Command+F or Ctrl+F and just click Find Next, just to make
05:04sure it's not anywhere else, if you're removing things.
05:07So this Find and Replace is going to be really helpful when you start
05:10condensing down your code.
05:12All right, so that's fine.
05:13I can remove that, and again, I am just simplifying this code.
05:17And again, I can go in and kind of clean it up and make sure everything is
05:22pretty squared away.
05:23A lot is going on here, and I'd say this is pretty powerful. Know that it
05:27actually does not execute any of these commented lines,
05:31so I don't need to worry about that.
05:33Trace statements, I am not going to worry about those either.
05:36In general, I think this game is good to go.
05:38I will do a quick test movie in Flash,
05:42just to make sure everything plays correctly.
05:44(explosions)
05:56All right, so everything was playing well.
05:58It seemed pretty smooth.
05:59I liked how it was working, but the next step is to really try this out on a device.
06:03Now, you are not going to get ideal performance compared to a Desktop, but again,
06:07you want to make sure you optimize all your graphics and your ActionScript code
06:12before you release it or publish it into the Android marketplace.
Collapse this transcript
8. Publishing
Creating the application files
00:00Now that the game is optimized and complete, I want to go ahead and publish the
00:05necessary files that are needed create my app.
00:09Now, one of those files is going to be the SWF file of the actual game.
00:14Then there is going to be a signed certificate that I am going to generate that shows
00:19the user who actually created the game.
00:22There is going to be an XML descriptor file, which describes the application,
00:28sets the necessary permissions, all of that stuff, as well as some icons.
00:33So those are all the elements I need, and I am going to start with the SWF file.
00:37So by going to File > Publish Settings, well, I don't want to output an HTML file,
00:43so I'll just uncheck that.
00:45In fact, I just want to publish out an SWF file.
00:49So I am just going to click on this folder icon, and I'm just going to output
00:51everything to the Desktop.
00:53So I am going to output Invaders.swf, okay. Click Save and I'll click Publish.
01:01All right, with that done, what I want to produce next is going to the
01:06signed certificate.
01:08Okay, so in order to do that, I am going to go to the Flash tab, and I am going to
01:13change this to Adobe AIR 2, because every Adobe AIR app gives you the option to
01:20create a signed certificate.
01:22So change that to Adobe AIR 2, click on Settings, and don't worry about filling
01:28any of this in; just go to the Signature tab.
01:31So from here, a signed certificate is needed for every app that's going to go
01:37in the Android market.
01:38It's going to be a p12 file, and I am going to go ahead and create a new one.
01:42So I am select New. Publisher name.
01:45Just start filling in the blank.
01:53I am going to give it a password, and then I want to make sure it's a 1024-RSA
02:02file. That's the type it is so it will work for Android apps.
02:07I can go ahead and save it out, so we'll just call this lyndaMobileGame, and
02:15I'll click this folder. And again, I am going to save it to the Desktop.
02:19Select Open, OK, and a self-signed certificate has been created.
02:27I'll click OK, and I'll cancel out of this.
02:29All right, so I have those two files created.
02:33Now, I am going to go to my Desktop.
02:36This is an XML file that was created from AIR,
02:39so it created in AIR 2 file.
02:41I don't need this file at all.
02:43In fact, I am just going to right-click and move it to the trash.
02:46I want to use one that's specific for mobile devices, okay.
02:50So I have my SWF, here is my signed certificate, and now I need to go ahead and
02:55get my Invaders-app.xml file.
03:00Okay, so this is an XML descriptor file that describes how to treat this SWF, and
03:06really what it contains and defines the permissions.
03:09So I am just going to take this and I'll copy it, and I'll close that, and I am
03:15just going to paste it onto my Desktop right there.
03:17Another thing I am going to do is I am going to go in to the Assets folder, into
03:21the Graphics folder, because I am going to need these icons as well, the 36x36-
03:27pixel png file, a 48x48 and a 72x72.
03:33These are the various icon buttons that are required for my apps.
03:37I am going to take all three of those.
03:38I am going to copy them and place them on my Desktop as well.
03:44So everything is looking good so far.
03:46I have my icons down here, these two files as well, and I need to go ahead and
03:53define the elements and basic information in this xml file.
03:59So I am going to go ahead and open it up in Dreamweaver.
04:03Now, you can use any text editor that you want, but I am going to go ahead and
04:06use Dreamweaver to define the various elements of this file.
04:10All right, right at the top, notice that it does say this is an application
04:15that's using Adobe AIR 2.5,
04:18so there is additional functionality in this.
04:20AIR 2.5 is for mobile devices--
04:23not that you have to worry about that, because you don't even need to touch that.
04:26I do need to first give it an ID.
04:28This is going to be a reverse domain format is what's commonly done.
04:32So I am just going to do com.lynda.Invaders.
04:38So this last word right here is going to be at the end, which
04:42describes, basically, the game.
04:44This is the version number, okay, so you can actually continually add to it, and
04:49then you're going to have the file name.
04:52In this case the file name is Invaders.
04:57Next up is going to be the description, so "This is a Space Invaders
05:04style game, but better." How about that?
05:07All right, so you type in the description. Right in here if you want to localize
05:11it based on the language, you can go ahead and define what that is. That's grayed out.
05:16I don't even need to worry about that at all.
05:19I do need to give it an actual name for this game as well, so Doodle Galaxy
05:27Invaders is its actual name, and we can localize the name as well by language.
05:35Down here for the copyright, put in your copyright.
05:40Add the content right in here.
05:42Well, it's not Invaders_Final.
05:43This is whatever the name of your swf file is.
05:47Visibility is set to true, fullScreen, of course, is going to be true.
05:50It's going to go full screen by default, actually.
05:53We want to define whether it autoorients, so this might be false or it might be true.
05:58Well, I don't want this to autoOrient.
06:00In fact, I want its aspect ratio to be landscape,
06:05so I am just going to remove that or portrait.
06:09Next up, the rendermode, it's going to be CPU or GPU.
06:15GPU is the Graphics Processor Unit, so this going to help me basically
06:21optimize the graphics and use the actual hardware of the device to move all
06:26the graphics around.
06:27So I want to change this to GPU, and you want to use GPU if you used any
06:33cacheAsBitmap calls, which I did earlier. customUpdateUI,
06:38well, it could be false or true.
06:40I am not going to worry about that updating UI at all.
06:42This isn't necessarily some rich Internet application;
06:45it's just going to be set to false.
06:47BrowserInvocation, false.
06:50Basically if you don't know what it is, you can set it to false, and right in
06:53here, here's where I define my icons,
06:56so that's why I moved them to the Desktop.
06:58They are all in place.
06:59Know that these are the three sizes required, and they are in the appropriate
07:04location. And right down in here, this is where we get to the meat of this file, if you will.
07:10This is where I define all of my permissions.
07:13So when the user downloads this app, it's actually going to give them just a
07:17little pop-up that says, hey, you know what, this is going to access the
07:21Internet, for instance.
07:22So again, if my app needs to access the Internet, then I'd make sure this is in here.
07:28But it doesn't, so I can remove it.
07:31Okay, right to the external storage, I don't need most of these, but I did keep
07:35them in here in case you were curious about using any one of these.
07:38You can read the PHONE_STATES, use the camera, record audio, all sorts of things.
07:43But specifically for this game, I want to just remove all of these, except for
07:49the WAKE_LOCK. So the WAKE_LOCK I want to keep, because again, it's using
07:55accelerometer, and I want to make sure that screen doesn't go to sleep.
07:59All right, with that all done, all I need to do is just save this file, and it's
08:06all set up, so I'll just minimize that.
08:09All these files are ready.
08:11Now, the next step with these files is to take them all and package them up into
08:17an APK file, the actual app file that will go on a device.
Collapse this transcript
Creating the app (Mac)
00:00Once you have all of your files that you need for your app, the next step is
00:04going to be taking all of these files and packaging them up into an APK file,
00:11which is the format of a mobile application for Android devices.
00:15And in order to do that, what I'm going to use is the AIR SDK.
00:20So if you go to adobe.com/products/error/sdk, you can go ahead and download it.
00:30Now the key tool I am going to use here is this Adobe AIR Developer tool.
00:35From the command line, I am going to go ahead and use this Adobe AIR
00:38Developer tool, ADT.
00:40So, just go ahead and download it.
00:42I've actually already downloaded it.
00:44You can see it right here.
00:45And I'll just unzip this file, and here's the Adobe AIR SDK.
00:54I am going to go ahead and drag this onto my Desktop, and I am going to place it
00:58right there, and I am going to close this folder.
01:01So, what I want to do is with this AIR SDK, inside of this bin folder is
01:08where this ADT file is.
01:11This is actually an executable.
01:13And I want to run this adt executable and use it to basically package up all
01:19these elements into an APK.
01:21And I am going to do that using the command line.
01:23So, on a Mac, just go ahead and hit the Search button and search for Terminal.
01:29As soon as it's selected, you can go ahead and hit Enter.
01:33It is in your Applications folder. And I am just going to go ahead and increase
01:37this font just by hitting Command+Plus, because I am going to go ahead and use this
01:44to target that ADT file, as well as these files.
01:50So, the first thing I need to do is point to my Desktop.
01:52So I need to say the current directory, cd desktop, and I want to point inside of
01:59that AdobeAIRSDK folder/bin into this folder right here, because I want to
02:10really get to this ADT file.
02:12And really, to make this even easier on me, what I can do,
02:15and I encourage you to just take all of these files, the icons, xml,
02:20certification, and just drag them into that same bin folder.
02:23That's just going to mean that I have less to type in here.
02:26All right, so that's the folder I am targeting.
02:28That's going to be the current directory.
02:30That's what it is known as, the cd.
02:32Now, I want to go ahead and use adt to package up those files.
02:37So I'm going to say ./adt, which is in that folder.
02:43I want to package and target an APK file.
02:49The storetype is going to be pkcs12, and keystore is also what I need, because
03:02I need to go ahead and use that mycert.p12, and notice how it is right here in mycertp12.
03:11Okay, so I'm not including that certification file, which I need. The next thing I am
03:16going to do is I am going to go ahead and give this APK file a name, and I am
03:20just going to call Invaders.apk.
03:25And within that Invaders.apk, this assigned Invaders.apk file, it's going to
03:31have the Invaders-app.xml, this xml file.
03:38You can see right here.
03:40And then it's also going to have the Invaders-swf file.
03:47Now, if you remember, this XML file actually does point to these PNG files.
03:53So, if there's any reference to any external graphic, you need to make sure you
03:57include that in here as well. And that's the case with these icons, so icon48,
04:05icon72, and icon36.png.
04:14All right, so this needs to be exact, so go through and make sure there are no
04:19spaces. Make sure the capitalizations are appropriate.
04:23And if everything is set up correctly, all you need to do is you hit Enter, and
04:27it will ask you for the password when you first created this p12 file.
04:32So I will enter that in, and then it goes through and creates an APK file.
04:41So, as I scroll down, you can see here's the Invaders.apk file.
04:47So again, as long as you have the Adobe AIR SDK, you can access this ADT
04:52file, which you can use using the command line to package up all these files to make an APK.
05:00So really, the next step is to take this APK and put it on an actual device.
Collapse this transcript
Creating the app (PC)
00:00So, I am going to show you how you can go ahead and package up all of these
00:03files into an actual app file that can be distributed.
00:08So, I want to make an APK file.
00:11I'm going to use a couple of different things.
00:14I am going to use JAVA, I am going to use the Adobe AIR SDK, and I'm going to
00:17package everything up using the command line.
00:20Now, it sounds pretty complex, but it is fairly straightforward.
00:23But the first thing we need is we need to go ahead and get the Adobe AIR SDK.
00:29So go to Adobe.com, and you can just do a search for Adobe AIR SDK if you want,
00:34or the actual URL is adobe.com/products/air/sdk.
00:42And notice it does list out what it contains, and really what I want to use is I
00:47want to use the command line, this ADT tool, the Adobe AIR Developer tool.
00:52So, I am going to use ADT to package up those elements, all of those files.
00:57Okay, so, of course, I'm on a Windows machine, so just go ahead and click
01:01Download now for Windows.
01:03I've actually already done that.
01:03So, again, right here, this is what I have downloaded is this folder.
01:09Again, you will download a zip file and just unzip it, and this is what you get.
01:13Luckily, you only need to be concerned about one file in here, and it happens to
01:17be in this bin folder.
01:19I will double-click on that, and that adt is what I need.
01:23In fact, what I am going to do is I am just going to go ahead and drag all of
01:27these files into this folder, because again, I am going to use this batch file to
01:34package up all of these files into one APK file.
01:39Now, I am going to do this using the command line as well as Java.
01:44So, I first need to make sure Java is installed.
01:47So, I am just going to go to Computer, and I'll go into Local Disk, into your
01:53Program Files, and you should see Java in there.
01:57In fact, if I open that up, here is my jre6.
02:01And again, in this bin folder, you can see it's java right here.
02:06Or what you could do is you could just go ahead and search for java.exe.
02:10But again, I just want to be aware that it is there, and I want to be aware of
02:14the path to it exactly as well.
02:17If you don't have Java installed for some reason, you can actually download the
02:21latest version from the Internet.
02:24But again, I can see that it's installed.
02:26I just want to be aware of where it is, because I need to do something with this path.
02:30I need to add it to my system file, so I am going to go ahead and just copy
02:37this path right here.
02:39And what I need to do is I need to go ahead and do a search for Advanced
02:47System Setting.
02:49All right, so just do a search just by clicking the Windows icon,
02:54Advanced System Settings.
02:56So, that's what I need.
02:57It is in the Control panel.
02:59But this is just a quick way to get to it.
03:01You want to open up your Advanced System Settings.
03:03And I need to add that path to my Environment Variables.
03:08So, when I use the command line, my command line needs to know where that java.exe is.
03:15So I am going to select Environment Variables, and again, I want to be aware
03:20of that path and I quite frankly need to add it to the path, not PATHEXT, the path right here.
03:27So, just select Path, double-click, and I need to add it,
03:31so I don't want to remove anything, please don't, but right down here at the end,
03:35just do a semicolon and then paste in that line.
03:40Again, it's pointing to that java.exe. Just paste in that line.
03:43You can see it right here, ;c clear down to that file.
03:49Once that's done, I will click OK. Click OK again.
03:53And again, there might be a chance that you already have that path added, so
03:56please review those various paths in there, just to make sure you don't.
04:00But nonetheless, it is added now.
04:02I will go ahead and click OK, and now that's set up.
04:05So I can go ahead and close that window.
04:07And let's just move this off to the side, because what I want to do now is I am going
04:11to go ahead use the command line to package up everything.
04:15So again, click on the Start menu, and just do a search for 'cmd' for commandline.
04:20There is my executable.
04:22I'll go ahead and select it, and that will open up this command line window.
04:27So this is where we are going to get all of our work done.
04:29So, I what I want to do is I first need to target this folder.
04:33So, I am going to change the current directory, so I am going to cd for current
04:36directory, desktop. What is on the desktop?
04:41Well, this folder is.
04:42I need to type in AdobeAIRSDK.
04:45I'm typing it exactly as it's listed here, caps and all, and then the bin folder,
04:52forward slash, just like that.
04:55Hit Enter and now I'm targeting this folder.
04:59Now, with all of those various files, especially the ADT batch file, I want to say adt,
05:05with that batch file, I need to package up and target an APK file.
05:13So that's what I am going to make.
05:15The storetype, again, I am going to add the storetype because it is going to be
05:19a secure APK file I am going to make.
05:21Storetype is going to be pkcs12. That's the security for mycert.p12.
05:31Storetype is pkcs12 keystore mycerts.p12, again, that's the certification file.
05:42Everything is typed correctly, space, Invaders.apk is what I am going to make.
05:49And now I can start listing out all of these files right in here that I want
05:53to package up with it.
05:54All right, so Invaders.app.xml Invaders.
06:02Again, I've got to be aware of my capitalizations in here.
06:05swf icon36.png icon48.png, and then there is icon72.png.
06:19All right, so keep in mind there is no spaces before these dashes and the word,
06:24and I'm just reviewing everything.
06:26Everything looks pretty good.
06:28There is just a single space in between all of these file names.
06:31Everything looks good.
06:33So all I need to do is hit Return, and what it asks me for is the password.
06:38And again, that's a password that I used when I created this mycert file, so in
06:43this case it's Johnson.
06:47I've typed it in. Even though it doesn't show it in there,
06:49know that I did type it in. I will hit Enter.
06:54And then sure enough, what does it do?
06:56It creates that APK file, almost like magic. But this is great!
07:02So this is actually my packaged-up application, my app file for Android devices.
07:07So I can go ahead and take this.
07:09I could quite frankly upload it to the Android market if I want to, but what I
07:13want to do with this now is go ahead and test it out on an actual Droid device.
Collapse this transcript
Publishing to an Android device (Mac)
00:00Now that I have an APK file created, what I want to do with this file, which is
00:05really my game, is I want to go ahead and put it on my Droid device.
00:09So, I happen to have a Motorola Droid 2 that I want to put this APK file on,
00:13but there is one thing I need to do on my Droid device first.
00:19So here's my Motorola Droid 2.
00:21I am going to go ahead and hit that Options button down in the lower-left, and I
00:25am going to go into the Settings.
00:28Now, some of these buttons might not be exactly the same depending on your
00:32Droid device, but it will be in Settings, and if you go into Applications, there
00:38should be Development.
00:40So, select Development to set Development options.
00:45And right within this list, notice it says USB debugging.
00:50So, debug mode when USB is connected, go ahead and check that.
00:56You can also allow for mock locations if you are doing any location-based gaming
01:00or anything like that, but again, that's the only box you need to check.
01:04And when you're done with that, you can just go ahead and hit on that Home
01:08button, and now the device is ready and good to go.
01:11So, from here, I can go ahead and connect this device to the computer through a USB cable.
01:21Now, with my device connected by USB, what I want to do is I want to transfer
01:25this APK file to the device.
01:28But what I need to do in order to do that is I need to go ahead and get the SDK
01:33from Android, the Android SDK.
01:36If you go to developer.android.com, it's that second tab right here, so select
01:42SDK, and then right in here, if you're on the Mac or Windows, you just select
01:47the appropriate one.
01:48I am going to go ahead and download this Android-sdk release 8.
01:53It will download it.
01:56Now, I can go ahead and double-click on that file, and notice here are all of the
02:02assets that I had just downloaded.
02:04So, I am going to go ahead and take this entire folder and I am going to just
02:08put it on my Desktop.
02:09I am going to make it easy to get to.
02:12So, I will take this entire folder and put it right on my desktop, close that,
02:17and I'm going to rename this because I just want to reference it as
02:22AndroidSDK, just like that. All right.
02:26Now, if you recall, all of my files are in here, but really all I need is this Invaders.apk.
02:35So, again, I want to package that up.
02:37I am going to use this AndroidSDK to do that.
02:40I am going to go into tools, and the first thing you are going to need to do is
02:45launch this android SDK right here.
02:47This is an executable. Click Open.
02:50It will launch. And you need to go to Available packages.
02:55And if you check the Android Repository, it will go ahead and install all of these items.
03:03So, that's exactly what I want to do.
03:04I want to install everything.
03:06I am really only going to use one file out of here, but let's just go ahead and
03:09install all of these items.
03:12It might say there might be some dependencies. Either way,
03:16I am just going to select Accept All and then install everything.
03:24Everything is installed.
03:26It says, well, do you want to restart ADB now?
03:28I will say sure and restart it.
03:32Ten packages are installed.
03:33I will go ahead and click Close.
03:35Its going to do quite a few things, by the way, but really what I want to do is I
03:40want to go ahead and take a look at one tool in particular that I have
03:44downloaded, because it did install all of these various folders right in here.
03:50But again, I am going to go ahead and open this folder, and within
03:54platform-tools, you'll find this ADB file.
03:59This is the Android Debug Bridge.
04:03So, that's actually what I am going to use to take this APK and install on my device.
04:09But to make this even easier on me, I am going to go ahead and take this APK and
04:14drag it right into this folder here.
04:16All right. Okay, so let's set this up.
04:21Again, I am just going to use this APK file and that adb tool right there.
04:27So, I am going to go ahead and use my search tool to search for the terminal and launch it.
04:33Here's my new terminal window.
04:34I will just do Command+Plus to increase that font size, and now I can go ahead and install.
04:40This file is using the adb executable.
04:44But what I need to do is I actually need to target the directory that they're in.
04:48So, I am going to say cd for current directory,
04:51there on the Desktop, forward slash.
04:55They are inside that Android SDK folder, SDK/platform-tools, just like that.
05:06So, I am targeting that folder. I will hit Enter.
05:10Now, with that folder targeted, I can go ahead and use that adb, that Android
05:16Debug Bridge to install the Invaders.apk file.
05:22Just like that, I will hit Enter.
05:25It goes through and installs it.
05:27As you can see, success, and it tells me exactly where it installs it.
05:32So, works out great. Now I can go ahead and check it out on the device.
05:38Here's my Motorola Droid 2.
05:39I am going to go ahead and go where all my applications are, and I'm going to
05:45scroll to where that Doodle Galaxy Invaders app is.
05:49As you can see, it does have the icon.
05:52You can see it right there.
05:53Again, there's no transparency on it, but again, I can just go ahead and select
05:57it and the game will start.
06:04Here's the intro screen.
06:05I'll go ahead and select Play to play the game. (explosions)
06:25So, I am able to tilt the device and play the game, and depending on how well I
06:30do will determine what I get for the high score screen.
06:34Whether I enter in my name or maybe-- especially if it's my first time, I get the
06:39opportunity to inter in my name. And if I do select that input text field, I can
06:45go ahead and type in my name.
06:46It actually activates the keyboard.
06:50I can type in my name, click Done, click Enter.
06:56It says, "Great job."
06:59Now, I will go ahead and play again.
07:00So, I can play the game again, but you can see that it's working, and I was able
07:03to publish out to the device pretty easily.
Collapse this transcript
Publishing to an Android device (PC)
00:00Now that I have an APK file created, what I want to do with this file, which is
00:05really my game, is I want to go ahead and put it on my Droid device.
00:09So I happened to have a Motorola Droid 2 that I want to put this APK file on,
00:13but there is one thing I need to do on my droid device first.
00:19So here's my Motorola Droid 2.
00:21I'm going to go ahead and hit that Options button down in the lower-left, and I
00:26am going to go into the Settings.
00:28Now some of these buttons might not be exactly the same depending on your
00:32droid device, but it will be in Settings, and if you go into Applications there
00:38should be Development.
00:40So select Development to set Development options, and right within this list
00:47notice it says USB debugging.
00:50So debug mode when USB is connected, go ahead and check that.
00:56You can also allow for mock locations if you do any location-based gaming or
01:00anything like that, but again, that's the only box you need to check, and when
01:05you're done with that you can just go ahead and hit on that Home button and now
01:09the device is ready and good to go.
01:11So from here I can go ahead and connect this device to the computer through a USB cable.
01:21Now, we're really getting down to the fun part where we actually get to take our
01:24game and put it on an actual device and play with it.
01:28So again, here's my game right here. It's an APK file. And I want to take that and
01:33push it out to this Motorola Droid that I have connected via USB already, so
01:39it's already connected, and that's how I choose to push it out to the device and
01:43know that I can also take this APK and I can email it to the device, or I can
01:48put it on a URL and go to that URL on the device as well.
01:52So there are many different ways, but I find connecting it through USB just
01:56makes development much faster.
01:58So really, what I need to make this happen is the AndroidSDK.
02:03So I'm going to go ahead and go to developer.android.com, and right there on the
02:10Home page you'll see Download, and this is where you'll download the SDK.
02:14I will click Learn More, and again, this is constantly being updated, but you
02:19can see right here in this main area, here's my Windows link for that SDK, and
02:26I can select it and download it.
02:29It is a zip file, so again just download it, unzip it.
02:32I've actually already done that, so it's right here, so I'm going to click
02:35Cancel, and I'll just open up that folder.
02:38As you can see, these are all of the items that I've already downloaded, but
02:43before I get into that information, I would like to point out a couple resources
02:46on this page, because everything from the blog to the forums is really helpful,
02:50but know that since I'm connecting this device through USB, I need to make sure
02:55my USB drivers are up to date.
02:57So if yours aren't for some reason, you might consider going out here and just
03:01updating your drivers, okay.
03:03My drivers are doing fine on this droid device, so I don't need to do that and
03:06you might not need to either.
03:08But nonetheless, just go ahead and download that SDK. Here it is.
03:13I'll open this up, and the first thing you would want to do is run this SDK manager.
03:19This is really going to install everything that you need.
03:23So I'll run this, and as you can see, I pretty much have everything already
03:27installed. So again your list may vary, but again, I have one item on my list that
03:32I can go ahead and install. But again, just make sure they're all checked and
03:36then select Accept All, just to install everything, just to make sure you have
03:41everything you need. Click Install. In this case, I'm fully up to date, so I can
03:46just click Close, and that could take up to 10 minutes, by the way, but notice it
03:51does install quite a few things and a lot of tools you might end up needing.
03:56But again when it comes to just pushing out an APK to a device, I am really only
04:02need the Android Debug Bridge, so let's take a look at where is that at.
04:05Now that everything is installed, if I go back out here to this folder, notice
04:10that there's this Platform tools folder, and this gets installed when you run the SDK manager.
04:17So I'll open this up, and nonetheless here is the ADB, our Android Debug
04:22Bridge that I want to run.
04:23Now I'm going to use this to push out this APK to my device.
04:29So I'm going to takes this APK and I'm just going to drag it into that same folder.
04:33Okay this is just going to make development a little quicker for me.
04:36I'll move this off to the side, and again I'm going to use this to package up
04:40this APK, and I'm going to do that through the command line.
04:43So I'm to go ahead and select the Start menu, and I'll just type CMD. There is my
04:49executable so I can hit Enter, select it, and here we are.
04:54So the first thing I need to do is I need to target that specific folder.
04:58So I'm going to say CD for Current Desktop and type in Desktop and point to that
05:04specific folder, so AndroidSDK. So whatever that folder is called, again, I have
05:10just renamed it and to make it a little shorter and easier to type, and I want to
05:15make sure that everything is capitalized appropriately. AndroidSDK/platform dash
05:22tools right in that folder.
05:25I'll just do a forward slash, hit Enter, and now I'm targeting this folder.
05:29Now the next thing I need to do is I need to go ahead and use the ADB, so just
05:33type in ADB install and then Invaders.apk.
05:39Since it's in that same folder, this is really easy to type. I would say it's
05:42pretty darn straightforward, and that's all I need to do.
05:46So again, I'm going to make sure my device is connected by USB.
05:49I'm going to make sure my drivers are up to date, and if they are, all you need to do is hit Enter.
05:53It will go ahead and take that APK. Sure enough, success is what it says here, and
06:00it pushes it out to my device. And here's my Droid 2, so all I need to do is go
06:07to my Applications area, where all my applications are at. Notice how it says
06:13Doodle Galaxy Invaders.
06:15I can go ahead and select that app to launch it.
06:23Here's the intro screen. I can go ahead and click Play to play the game.
06:28So using only really one of the features of the Android SDK, I can easily take any
06:42APK and push it out to a connected device.
Collapse this transcript
9. Uploading to the Android Market
Uploading to the Android market
00:00Now comes the exciting part,
00:02taking your application, your APK file, and uploading it to the Android Market.
00:08And it's quite easy, to be honest. All you have to do is go to
00:11developer.android.com. Off to the side, you'll see this Publish where we're actually
00:17going to publish to the Android Market, and it's an open service, so it's actually
00:21free to upload as many apps as you want.
00:24Okay, so it's free to upload them.
00:26Now, there is one thing I need to warn you about, because as I click on that
00:31link, I can see various apps, but right down here at the bottom, this is where you become a developer.
00:37So you click here and then you fill out a form.
00:41I have already done that, but the one thing you'll need to know about becoming
00:44an Android developer is that it will cost $25, okay.
00:50Its 25 bucks for a year, and again, that means unlimited applications you can
00:56upload. And with such a market growing, all these various Android devices being
01:01released almost weekly, it makes it a really good investment.
01:05So again, once you pay that, you can go ahead and upload as many apps as you want.
01:10I already have a couple in here, okay.
01:12And not only is it doing some tracking, but notice that they are free as well.
01:18So I get some stats. But what I want to do is I want to go ahead and upload the
01:22application I have made.
01:23So I am going to select Upload Application.
01:27Now right in here, it's basically fill-in-the-blank.
01:31So again, I want to take that APK file and I want to upload it.
01:34So I want to select Choose File. I'm going to go to my Desktop. I'm going to go into the
01:41AndroidSDK, and the last place I had it was in the platform-tools folder, and here
01:49is my Invaders.apk file.
01:52So I'll click Choose, and then I'll click Upload.
01:55All right, there it is.
01:59Notice that it does have an icon. There is actually a couple different icons, the
02:03VersionName, different things.
02:05There is 1 permission that we warn the user about, which means that there is
02:09that wake lock call, so the screen is not going to go to sleep so they could
02:13still play the game.
02:14All right, so it will give you any sort of warnings just for you to be aware of,
02:17just to make sure you have those in there as well.
02:20All right, next steps are the screenshots.
02:23Okay, so you can upload screenshots that appear when you see the description of your game.
02:29So I'm going to go ahead and choose a couple of those. In my Assets folder, in
02:34Graphics, I'm going to go ahead and choose those particular files.
02:41These screenshots for my app are these 480 x 854 images.
02:46So they're JPEGs--they could also be PNG files--but they're just quick
02:50representations of the game play and the first screens.
02:54So I'll choose that one.
02:56I'll upload it. There it is.
03:00I'll go ahead and add another. Choose File and add that second screen as well.
03:06All right, so there are my screenshots.
03:12Next step the high-resolution application icon, and again, this is a 512 x 512
03:19image. It could be a PNG or JPEG, so I just go ahead and create those files.
03:22Again, they're already created. Here it is, this 512 image. That's what it looks like.
03:28Choose that. Click Upload.
03:30The promotional graphic which will be used, say for instance, on that
03:36previous page I have shown,
03:38I want to probably make that as well and upload it, so that's 180 x 120 image.
03:44And notice that a lot of these images are pretty similar, just in different sizes
03:48and different crops.
03:50Click Upload and this feature graphic is also optional, but again I have made
03:58one, so I'll just go ahead and upload that large image.
04:01It's actually 1024 x 500. Choose it. Click Upload.
04:06All right, so I have all my graphics uploaded.
04:09I can have a promotional video.
04:11I want to opt out promoting the application, say for instance, if it's for
04:15select group of people, I might consider checking that box, but really want this
04:19to be free for everybody.
04:21And now I can jump in here to the Listing details.
04:24So again, I'm supplying to the Android Market the various details of this
04:28application, Doodle Galaxy Invaders description.
04:36All right, a little description. Pretty much they just need to know that it's like
04:42a space-invaders-type game, but I'd want to talk it up as much as possible in
04:46here in this description, just not too much text is all.
04:50Any recent changes, again, if this is the 2.0 version and I've added, say for
04:54instance, more aliens, I might want to describe that, and then promotional text.
05:01There is my promotional text.
05:03Application Type, it could be an application, but of course this is a game, so
05:06I am selecting Game.
05:08Category, well, again, I got plenty in here. This is just a casual game. That was
05:13the purpose of it, for a user just to play while they standing in line waiting
05:17at the bank, or whatever the case may be.
05:19Price is going to be free.
05:20Now I can set up a merchant account, and I would go ahead and do that and
05:25Google Checkout would be used. But what I need there is I would need a text ID
05:29and some other technical information. But in this case, since I want everybody
05:33have this game, I want to go ahead and keep it as free.
05:35Now, a good idea is to have potentially like a lite version of a game L-I-T-E and
05:41then the full version that you'd actually charge for.
05:43So you can do both with one game, but in this case this one's just going to be free.
05:49Okay, Copy Protection is going to be off, so anybody can copy it as many times as they want.
05:55The content, well, this is pretty much for everybody, and locations, there is no
06:00location issue, because there is really not a whole lot of text in it.
06:03I think anybody, regardless of where you live or what language you speak, should
06:08be able to play this game.
06:10Lastly, we have the contact information, web site, email, as well as phone number.
06:15You can enter in, and this came from my profile, so when you first sign up as a
06:19developer, that's where this information will come from, and you can always change it.
06:22All right, so Android Content Guidelines, those are additional guidelines just
06:28saying you adhere to all the basic rules and suggestions as far as the graphics
06:33go and the content as well.
06:35So again, this is a free market, so these are just guidelines. Overall you're not
06:40going to have a problem at all submitting your app, but it does meet the Android
06:45content guidelines, and I want to check this last check box as well.
06:50I'll click Publish, and there it is, Doodle Galaxy Invaders.
06:57Currently is not rated, nor has been downloaded. So as you can see, it's really easy
07:02to upload a game, or pretty much any mobile app, to the Android Market.
Collapse this transcript
Downloading from the Android market
00:00Now, comes the fun part.
00:02Now I get the opportunity to go out to the Android Market on an Android phone
00:07and then download my game app.
00:09So I am going to launch the Android Market, and I'm just going to do a quick
00:13search on "doodle galaxy," and I'll just hit go.
00:26There it is, Doodle Galaxy Invaders. It is a free app.
00:31I'll just select it.
00:34It does give me a little bit of a description. I can see the images that I
00:38uploaded. It all looks good.
00:41I can see comments and similar, but what I really want to do is I just want to click Install.
00:48And it does say this application has access to the following: the system tools,
00:53which is going to prevent the phone from sleeping, which is what I want it to do. Select OK.
01:01It downloads my app, and now if I go where all my applications are, there it is,
01:13Doodle Galaxy Invaders.
01:15I'll just select it. Sure enough, there it is. I can go ahead and play the game.
01:25Now, I'm playing the game. You can see me shoot.
01:30So again, it's all available in the Android Market.
01:34Mere minutes after you upload an app, it actually will be available.
01:38And again, so I encourage you to go ahead and try that with your own app, just go
01:42ahead and create one, upload it, and then check it out in the Android Market.
Collapse this transcript
Conclusion
Next steps
00:01Thank you so much for watching.
00:02I hope you enjoyed not only the game, but the development process as well.
00:07In my opinion, I think Flash makes it really fun and easy to create games,
00:11so I encourage you, if you have access to the code, go ahead and take the
00:15code, modify it, swap out the graphics, and make your own game. Extend the code as well.
00:20I want to go ahead and point you into a couple different resources other than
00:24lynda.com that you can use.
00:26In fact, one is Adobe.com. Select Learning. There is something called Adobe
00:32Developer Connection.
00:33So go in there, and you'll notice there are two areas right down here for
00:38mobile, as well as gaming.
00:40So, great resources. There is going to be code snippets, tutorials, various things
00:44for gaming and mobile. Use those resources to, again, enhance your game.
00:49The other resource is developer.android.com.
00:53So again, the Android SDK is there. That's where you're going to go ahead and
00:57upload your app as well, but on both these sites, there are forums. You can always
01:02post questions there, and usually the community is pretty good at answering them.
01:06But again, lastly, when you do finish it and upload it,
01:10I really look forward to seeing it. In fact, you can feel free to send me an
01:13email once it's up. My email is paul@paultrani.com.
01:20Thank you so much for watching!
Collapse this transcript


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,141 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked