23 January 2012
Experience using the Code Snippets panel in Flash Professional, as well as a basic understanding of symbols, timelines, document properties, Flash Player version, and the process of publishing to Android OS-based mobile devices. Knowledge of programming with ActionScript 3 is helpful but not required.
Update Flash Professional to use the latest AIR SDK.
Intermediate
If you are interested in creating casual mobile games or applications but do not have prior experience developing with ActionScript, this article is for you. I show you how to create a simple mobile game called Jewelry Heist using the Code Snippets panel included in Adobe Flash Professional CS5.5. You'll learn how to apply and modify common interactive code snippets and device-specific code snippets to incorporate the game's features—having to have little or no knowledge of ActionScript.
The Code Snippets panel contains ActionScript 3 snippets that are commonly used in many Flash projects, including banner advertisements and games. Before completing the steps in this article, you may find it helpful to familiarize yourself with the Code Snippets panel and the prebuilt code that you can add to your projects. To learn how to use the Code Snippets panel, please read Code snippets for beginning ActionScript 3 programmers and designers.
The following code snippets are featured in this tutorial:
The sample project for this article is a simple mobile game application called Jewelry Heist. The premise of the game is to steal gemstones from the museum without touching the moving security laser beams (see game below). Watch the following video to see how the completed game appears when played on a Droid X phone. This is a demo version of the game; you can add complexity to this game by writing custom ActionScript.
In this video, you can see how the completed game appears when played on a Motorola Droid X phone.
To get started, download the jewelry_heist.zip file provided at the beginning of this article. Save and uncompress the file to your desktop. Locate the file named JewelryHeist.fla and double-click it to open it in Flash Professional. Examine the main Timeline to see how the finished project is constructed.
There are three frames in the game's FLA document. The first frame contains the game's instructions, the second frame contains the game's graphics and play logic, and the third frame contains the Game Over screen. The first task involves adding code snippets to the game instruction screen on Frame 1 so that the instructions are displayed when the game first loads (see Figure 1). It's necessary to add the code to this section (Frame 1) because otherwise the playhead would immediately move on to Frame 2 and skip over the game instructions.
Insert the code that pauses the playhead at Frame 1 (the frame that contains the game instructions) so that users have a chance to read the instructions before clicking the Play button:
Stop(); command is added.Notice that the Actions layer is automatically created above the other layers in the Timeline and the code is added to Frame 1 of the Actions layer. All of the code snippets you insert will be added to the Actions layer going forward.
Here you'll add the code to make the Play button go to Frame 2 when it's clicked:
gotoAndStop (5); to gotoAndStop (2);. The value in the parentheses indicates the frame number.In this section you will add code to Frame 2, which contains the graphics and code that enable users to play the game. If you want to keep using the previous section's sample file (Frame1.fla) as you follow this section, you must first choose File > Publish Settings and change the Player target settings from Flash Player 10.2 to AIR for Android in the Publish Settings dialog box. Click OK to save the change. If you are unfamiliar with making this change, you can simply double-click the Frame2.fla file to open it in Flash Professional and follow along with the steps provided in this section.
Frame 2 of the sample project contains graphics that simulate the inside of the museum and pedestals that each display a precious gem. The object of the game is to swipe the gems without touching the moving laser beams (see Figure 3).
Here you'll add the Touch and Drag Event code snippet, which makes it possible for game players to use their touchscreen to select and drag the gems. To steal the gems, players must touch and drag the gems to position them behind the red curtain:
Although smartphones do not have a mouse, you can use mouse events in ActionScript to trigger actions based on finger gestures on the touchscreen. Frame 2 contains three laser beams that move when you test the movie. When a user's finger intersects with any of the laser beams, the playhead should jump to Frame 3, which displays the Game Over screen:
// Start your custom code
// This example code displays the words "Moused over" in the Output panel.
trace("Moused over");
// End your custom code
Instead of tracing a text message, the goal here is to jump the playhead to Frame 3 when a player accidentally touches one of the laser beams. Edit the code in all three functions to replace trace("Moused over"); with gotoAndStop (3);. You may remember that gotoAndStop (3); is the same command you applied to the Play button in Frame 1.
Leave Frame2.fla file open to add Countdown Timer in the next section.
You can add a simple timer to this game with the Countdown Timer code snippet. One of the aspects of this sample game is that users only have 10 seconds to steal the gems and play the game. When the time has elapsed, the playhead jumps to Frame 3, which displays the Game Over message. Follow the steps below to add and update the Countdown Timer code snippet:
Edit the ActionScript code to replace this line:
var fl_SecondsToCountDown:Number = 10;
with this line:
var fl_SecondsToCountDown:Number = 1;
The new value of 1 sets the number of countdown interactions to one.
Edit the code again to replace these two lines:
trace(fl_SecondsToCountDown + " seconds");
fl_SecondsToCountDown--;
with this line:
gotoAndStop(3);
After making this change, 10 seconds after the play button is clicked, the game will display the Game Over message even if the user didn't intersect or touch any of the security laser beams.
If desired, you can continue working with the Frame2.fla file that you edited in the previous section. However, opening and using Frame3.fla file is recommended for this section of the article. Read the following sections to add code to Frame 3 of the Frame3.fla file, which contains the Game Over message (see Figure 4).
The first task involves adding code that causes the playhead to jump to Frame 2 when the user clicks the Play Again button:
gotoAndStop (5);
to this:
gotoAndStop (2);
This change enables the player to start a new game. When they click the Play Again button, the playhead jumps to Frame 2. This displays the Stage with the museum graphics so that the user can try to steal the gems again.
At this point, the game logic is complete. If desired, you can save the file and choose Control > Test Movie > Test to test the SWF file and play the game.
However, there are some remaining items that you can update within the game to improve its performance and ensure that the mobile app works as expected. In the next section of this article, you'll find suggested best practices and code to add to the finished game to use fewer system resources and run the game more efficiently.
Here are some suggested changes you can implement to update the game and polish it. These updates are recommended to ensure that the game runs smoothly when played on a mobile device. These code updates are also important because they enable a user to play the game over again repeatedly without encountering any issues.
It is a best practice to reinitialize the Countdown Timer code snippet that you added to Frame 2. In this section, you'll add a line of code to ensure that the countdown timer will reset and start counting down 10 seconds when a user clicks the Play Again button to play the game again:
fl_CountDownTimerInstance.reset();
In Google Android OS, an application will continue running in the background until the user manually chooses to quit the application and close it. Therefore, it is important to update the logic for the Quit button in the game to prevent excessive battery drainage:
Quit_MC.Quit_MC.addEventListener(MouseEvent.CLICK, fl_CloseApplication);
function fl_CloseApplication(event:Event):void
{
NativeApplication.nativeApplication.exit(0);
}
The code above ensures that when the user clicks the Quit button on Frame 3, the mobile application will stop running on the device.
Although the game is already complete, it is a best practice to clean up all of the events that occurred in Frame 2 before Frame 3 begins to play. The following function will remove all event listeners for objects that exist only in Frame 2. Without adding this code, there's a possibility that an event, such as Touch and Drag, may cause unrelated objects to move around if the user drags their finger on the touchscreen while viewing Frame 3 (the Game Over screen):
function RemoveEventListener()
{
Laser1_MC.removeEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
Laser2_MC.removeEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler_2);
Laser3_MC.removeEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler_3);
Gem1_MC.removeEventListener(TouchEvent.TOUCH_BEGIN, fl_TouchBeginHandler);
Gem1_MC.removeEventListener(TouchEvent.TOUCH_END, fl_TouchEndHandler);
Gem2_MC.removeEventListener(TouchEvent.TOUCH_BEGIN, fl_TouchBeginHandler_2);
Gem2_MC.removeEventListener(TouchEvent.TOUCH_END, fl_TouchEndHandler_2);
Gem3_MC.removeEventListener(TouchEvent.TOUCH_BEGIN, fl_TouchBeginHandler_3);
Gem3_MC.removeEventListener(TouchEvent.TOUCH_END, fl_TouchEndHandler_3);
}
After adding this code, save the file and test the movie again. Now when you experiment with playing the game, you can try clicking the Play Again button and see that the countdown timer is reset to 10 seconds so that you can play again. The garbage collection that you added also ensures that if you attempt to click and drag the gems while on Frame 3 (the Game Over screen) the gems remain stationary and cannot be dragged.
Now that you have successfully completed creating the Jewelry Heist game, you can try publishing it and testing it using your own Android OS-based smartphone. To learn how to deploy your app to Google Android devices, follow the instructions provided in Getting started with Adobe AIR for Android (article) or Publishing an AIR for Android app (video). You can publish this game to iOS or Android OS devices, smart phone, or tablets.
Experiment with the concepts you learned in this sample project and try creating a variety of simple mobile games using the Code Snippets panel, so that you can develop interactive and engaging experiences on devices without writing a lot of code.
Check out these online resources to learn more about building mobile applications:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
Flash User Forum |
More |
| 04/23/2012 | Auto-Save and Auto-Recovery |
|---|---|
| 04/23/2012 | Open hyperlinks in new window/tab/pop-up ? |
| 04/21/2012 | PNG transparencies glitched |
| 04/01/2010 | Workaround for JSFL shape selection bug? |
Flash Cookbooks |
More |
| 02/13/2012 | Randomize an array |
|---|---|
| 02/11/2012 | How to create a Facebook fan page with Flash |
| 02/08/2012 | Digital Clock |
| 01/18/2012 | Recording webcam video & audio in a flv file on local drive |