Creating Number Slide Puzzle Game in Five Simple Steps using ActionScript and AIR

This is very simple number sliding puzzle game I created in Adobe Flash Builder. I would like to share with you, how simply we can create it.

Download Source
Before start project, let’s plan steps how we can create it.

Step 1: Creating ActionScript Project

Open Flash Builder. Go to File>New>ActionScript Project.
Enter project name “SlidingPuzzle” and select Desktop option under Appliction Type. Then click on Finish.
Now open the and set its width = 300, height = 325, frameRate=24 and backgroundColor=#FFFFFF.
[SWF(width=”300″, height=”325″, frameRate=”24″, backgroundColor=”#FFFFFF”)]

Step 2: Creating number slide class

Right click on src>new>ActionScript class and name it “” set superclass “flash.display.Sprite”. Then click Finish.
Now here we will make rounded rectangular slide graphics and placeholder for number.
import flash.display.Shape;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;

public dynamic class Slide extends Sprite
public var txt:TextField = new TextField();
public var format:TextFormat = new TextFormat();

public function Slide()
format.size = 36;

var sp:Shape = new Shape();;,5,90,90,10,10);

txt.x = 20;
txt.y = 20;
txt.selectable = false;
txt.mouseEnabled = false;
txt.textColor = 0xffffff;

Step 3: Setting default position of slides

Now we will define the default positions of slides. The positions we will store in array.
While user will play this game, in that time we will check slide default position with this array on each individual slide movement.
public var positionArray:Array = new Array(

Step 4: Implementing logic to shuffle the slides

First Initialize Random Array with default positions/x y co-ordinates. Then shuffle the co-ordinates and place all the slides according to shuffled co-ordinates.

protected function setRandomArray():void{
// Initialize random array with default co-ordinate
randomPosArray = new Array( {xCo:0,yCo:0},
// Shuffle co-ordinates
randomPosArray = shuffle(randomPosArray);
// Place all the slides with shuffled co-ordinates

protected function replaceSlides(arr:Array):void{
for(var i:int=0; i 0) {
arr2.push(arr.splice(Math.round(Math.random() * (arr.length – 1)), 1)[0]);

return arr2;

Step 5: Setting up user interaction and Game over check

protected function handleSlideClick(e:MouseEvent):void{
// Find the distance between empty position and user clicked slide
var dist:Number = Math.sqrt(dx*dx+dy*dy);

// If it is 100 the place the slide to empty postion and set co-ordinates as last position
if(dist == 100){
var tempPos:Point = new Point(,; = lastPos.x; = lastPos.y;
lastPos = tempPos;

// If slide is on it’s defualt position the set it as True
if( == && =={ = true;
}else{ = false;

// Check all the slides are place on position
// If all the slides are on place set Game finished.
var bool:Boolean = false;

for(var i:int=0; i 0) {
arr2.push(arr.splice(Math.round(Math.random() * (arr.length – 1)), 1)[0]);

return arr2;


0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *