Source Code and Comments

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

The app gives a new 3D rendition of the classic Rolling Cubes puzzle. The puzzle is very challenging and takes a minimum of 36 moves to solve. A 'move' consists of rolling a 3D cube to the only empty space on a grid. Initially, all cubes have their black sides facing up. The goal is to invert them so their red sides face up and do it in as few moves as possible. We provide several simpler versions as a 'warmup' for the main puzzle. Below are two screen shots of the app.

Download

Working with the Source Files

The zip file contains RollingCubes.fla file and a folder of custom AS3 classes: com -> flashandmath -> cubepuzzle. RollingCubes.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 RollingCubes.fla in Flash CS5, you must enter 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. In this case, the settings are simple.

Under General, 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, nothing. This app does not require any permissions.

Desktop versions of Rolling Cubes presented here and of our recent Graph Slider are coming to flashandmath soon!

What We Have Learned

The Tricky Stage

The Stage object and accessing its properties is always a bit tricky and it is even more so on our Android phones. In order to perform effective depth-sorting of 3D cubes we use the technique developed in Z-Sorting and Perspective Projection in AS3 and Flash Player 10. We sort with respect to the distance to the observer which requires accessing perspective projection settings and cubes' coordinates with respect to the Stage. In our Android app we had to carefully set the Stage alignment:

stage.align = StageAlign.TOP_LEFT;

In order to avoid slight shifts in xy-coordinates of cubes' sides.

Accessing Phone's Menu Key

This is very easy and very similar to keyboard access in the Flash Player.

stage.addEventListener(KeyboardEvent.KEY_UP, fl_OptionsMenuHandler);

function fl_OptionsMenuHandler(e:KeyboardEvent):void {

if(e.keyCode == Keyboard.MENU) {

//Make you menu panel visible and code actions you want perfomed.

....................

}

}

Exiting Your App

One of the buttons in our options menu that appears when the user presses the Menu key quits the app. Exiting an app is remarkably simple. Within the button's click handler, put:

NativeApplication.nativeApplication.exit(0);

Download the zip file and take a look at well-commented code in source files. Have fun!

This RollingCubes app was written by Barbara Kaskosz and Doug Ensley of flashandmath.

The original Rolling Cubes puzzle was published by John Harris in Journal of Recreational Mathematics, 1976.

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.