Tuesday, December 21, 2010

15 game in Flash Actionscript3

Having implemented the 15-game in Java, I thought I would attempt the same with Flash + AS3. And the same was done swiftly and I have presented the output and the code here. This is the same as the applet version, you will have to use your arrow keys to play.




package{
 //----
 //IMPORT
 //
 import flash.display.*;
 import flash.events.*;
 import flash.text.*;
 import flash.ui.Keyboard;
 import flash.media.Sound;
 import flash.system.System;

 //Class creation
 public class Main extends MovieClip {
  
  private var SIZE:int;
  private var panelSize:int = 400;
  private var panelArray:Array = new Array(SIZE, SIZE);
  
  private var parentStage:Stage;
  private var baseClip:MovieClip;
  
  private var music:Sound = new BubbleSound();
  
  public function Main(stage, bClip, difficulty):void {
   SIZE = difficulty;
   parentStage = stage;
   baseClip = bClip;
   stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
   layout_in_grid();
   
   for(var i:int=0;i<SIZE*100;i++){//RANDOMIZE MOVES
    handleKeyPress(Math.floor(Math.random()*4) + 37);
   }
  }
  
  private function layout_in_grid():void{
   addChild(getGamePanel(SIZE));
  }
  
  private function keyPressed(e:KeyboardEvent):void{
   handleKeyPress(e.keyCode);
   music.play();
   if(areThingsInPlace()){
    baseClip.gotoAndStop(3,"Scene 1");
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    this.parent.removeChild(this);
    var victory:Sound = new VictorySound();
    victory.play();
    return;
   }
  }
  
  private function handleKeyPress(keyCode:int):void{
   var emptyIndex:int = findEmptyIndex();
   var x:int = emptyIndex/SIZE;
   var y:int = emptyIndex%SIZE;
    
   switch (keyCode) {
    case Keyboard.LEFT://LEFT KEY
     if(y==SIZE-1) return;
     doSwap(x,y,x,y+1);
     break;
    case Keyboard.UP://UP KEY
     if(x==SIZE-1) return;
     doSwap(x,y,x+1,y);
     break;
    case Keyboard.RIGHT://RIGHT KEY
     if(y==0) return;
     doSwap(x,y,x,y-1);
     break;
    case Keyboard.DOWN://DOWN KEY
     if(x==0) return;
     doSwap(x,y,x-1,y);
     break;
   }
  }
  
  private function findEmptyIndex():int {
   for(var i:int=0;i<SIZE;i++){
    for (var j:int=0;j<SIZE;j++){
     if(panelArray[i][j].num_txt.text == '0'){
      return i*SIZE + j;
     }
    }
   }
   return 0;
  }
  
  private function doSwap(x:int, y:int, x1:int, y1:int):void{
   var temp:int;
   temp = panelArray[x][y].num_txt;
   panelArray[x][y].num_txt.text = panelArray[x1][y1].num_txt.text;
   panelArray[x1][y1].num_txt.text = temp;
   
   if(panelArray[x][y].num_txt.text == '0'){
    panelArray[x][y].alpha = 0;
    panelArray[x1][y1].alpha = 1;
   }else{
    panelArray[x][y].alpha = 1;
    panelArray[x1][y1].alpha = 0;
   }
    
  }
  
  private function getGamePanel(difficulty:int):MovieClip {
   var gridLayout:GridLayout = new GridLayout(SIZE, panelSize);
    
   var componentsList:Array = getRandomizedList(difficulty);
   var index:int = 0;
   for(var i:int=0;i<difficulty;i++){
    panelArray[i] = new Array(difficulty);
    for(var j:int=0;j<difficulty;j++){
     panelArray[i][j] = componentsList[index++];
     gridLayout.add(panelArray[i][j]);
    }
   }
   gridLayout.x = (parentStage.stageWidth / 2) - (gridLayout.width / 2);
     gridLayout.y = (parentStage.stageHeight / 2) - (gridLayout.height / 2);

   return gridLayout;
  }
  
  private function areThingsInPlace():Boolean{
   for(var i:int=0;i<SIZE*SIZE-1;i++){
    if(Number(panelArray[Math.floor(i/SIZE)][i%SIZE].num_txt.text) != (i+1)){
     return false;
    }
   }
   return true;
  }
  
  private function getRandomizedList(difficulty:int):Array  {
   var componentSet:Array = new Array(SIZE*SIZE);
   for(var i:int=0;i<difficulty*difficulty-1;i++){
    var square:SquareClip = new SquareClip();
    square.num_txt.text = (i+1).toString(10);
    componentSet[i] = square;
   }
   var emptyClip:SquareClip = new SquareClip();
   emptyClip.num_txt.text = (0).toString(10);
   componentSet[i] = emptyClip;
   return componentSet;
  }
 }
}

The Layout grid file which I wrote on my own to simulate an exact Grid Layout similar to the one in Java.

package{
 //----
 //IMPORT
 //
 import flash.display.*;
 import flash.events.*;
 import flash.text.*;
 
 //Class creation
 public class GridLayout extends MovieClip{
  
  private var _size:int;
  private var _size_of_grid:int;
  private var _gap:int;
  private var _itemCount:int;
  private var _totalFilled:int;
  
  public function GridLayout(size, size_of_grid):void {
   _size = size;
   _size_of_grid = size_of_grid;
   _totalFilled = 0;
  }
  
  public function add(object):int{
   if(_totalFilled == (_size*_size )){
       trace("GRID IS FULL");
     return 0;  
   }
   object.x = ( (_totalFilled % _size) ) * (_size_of_grid/_size)  ;
   object.y =  Math.floor(_totalFilled / _size) * (_size_of_grid/_size) ;
   _totalFilled++;
   addChild(object);
   return 1;
  }
  
 }
}

Cheers!!
Braga

0 comments:

Your Ad Here