Source Code and Comments

We provide here complete source code for our GraphSlider Android AIR app that you can download and install from the Android Market. Open the Android Market on your phone, search for flashandmath, and locate GraphSlider on our apps list. You can then download and install the app.

In this app a mathematical graph with 5 to 8 nodes is given. Each node is labeled 0,1, 2, 3, ... around the outside, and there are numbered sliding tiles within all but one of the nodes. A "move" slides the tile from its current node to an empty node along an edge of the graph, and the puzzle is solved once each numbered tile matches the label of the node it is on. The puzzle shown on the screen shots below starts in the state shown on the left and is solved in the state shown on the right.

Once you solve a puzzle you get a star rating based on the number of moves it took you to do it. A three-star rating means that you solved the puzzle in the minimum possible number of moves. A two-star rating means you did it pretty well, and a one-star rating is the consolation prize for solving it at all. There are 30 puzzles packaged with this app, so you must be quite an expert (or have a lot of time on your hands) to earn a three-star rating on every puzzle.

The option menu allows you to see instructions, quit the application, or clear all of the star ratings. The app does save its state, so even if you exit the app, your star ratings will persist when you start the puzzle the next time. This saving of state is an idea that works in Adobe AIR but not in the browser-based Flash player. We discuss this part of the code included in the last section below.

Download

Working with the Source Files

The zip file contains GraphSlider.fla and puzzles.xml. The GraphSlider.fla file was created from AIR for Android template that becomes available in your Flash CS5 after you download and install AIR for Android Extension for Flash CS5 from the Adobe site. See our Android example AIR for Android Application KaleidoscopeFM - Source Code for useful links how to get started with AIR for Android in Flash CS5.

After you open GraphSlider.fla in Flash CS5, you must go to File -> AIR Android Settings and set those again in order to be able to play with the application and have it install on your phone for testing purposes. With the settings descibed below, each time you click Publish under File menu, a new version of the app will automatically be installed and launched on the phone hooked up to the computer. In order for this to work, you need to download Android SDK and sometimes a driver for your phone. Again, see the AIR for Android Application KaleidoscopeFM - Source Code example for instructions.

Under General, make sure that the file puzzle.xml is among Included Files. If not, add it by clicking on Plus button. Make sure that puzzles.xml is in the same directory as GraphSlider.fla. Make sure the app is set to Portrait mode.

Under Deployment, create or enter your p12 certificate. Then choose Android deployment type and After publishing settings. With our phone-computer setup, we worked with Device Release, Install application..., and Launch application checked.

Under Permissions, check WRITE_EXTERNAL_STORAGE. The application requires this permission to save the state of the game after the user exits.

What we have learned: Saving State

As we mentioned above, "saving state" is specific to Adobe AIR apps, so your friends here at Flash and Math are just learning how to take advantage of it. We include here comments on this part of the fla source code so our readers can learn as we do.

// Set userDataFile to be the desired file in the applicationStorageDirectory

var userDataFile:File = File.applicationStorageDirectory;

var fileStream:FileStream = new FileStream();

userDataFile = userDataFile.resolvePath("userdata.txt");

:

// The following function handles the COMPLETE event for the loading of the puzzles.xml file that contains the puzzle data.

function initData(evt:Event):void {

xmlPuzz = XML(ldrPuzz.data);

which = 0;

txtSolution.text = "";

// We check if the user data file already exists, and read it if it does.

if (userDataFile.exists) {

fileStream.addEventListener(Event.COMPLETE, fileCompleteHandler);

fileStream.openAsync(userDataFile, FileMode.READ);

} else {

// If the userDataFile does not already exist, we simply start the first puzzle.

setup(xmlPuzz.puzzle[0]);

}

}

:

// The following function handles the COMPLETE event for reading in the user data file.

function fileCompleteHandler(event:Event):void {

var i:int;

// Set stUserData to be the string of numbers in the user data file.

stUserData = fileStream.readUTF();

fileStream.close();

// The user data file consists of string of numbers, comma separated. The next three lines pull the information into an array of integers.

arrUserData = [ ];

arrUserData = stUserData.split(",");

for (i=0;i<arrUserData.length;i++) arrUserData[i] = int(arrUserData[i]);

fileStream.removeEventListener(Event.COMPLETE, fileCompleteHandler);

setup(xmlPuzz.puzzle[which]);

}

:

// The following lines are executed when we are ready to write the state information and close the user data file.

fileStream.open(userDataFile, FileMode.WRITE);

fileStream.writeUTF(arrUserData.toString());

fileStream.close();

The GraphSlider app was written by Doug Ensley.

Back to AIR for Android              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.