Pages

Tuesday, November 8, 2011

Simple puzzle game using hitTestObject.

Flash is not only for the creating animation. Using flash, developer can create game for any plat form like desktop, mobile, iPhone and even though for android. Creating flash game is not too much hard, if you are having solid knowledge of oops(object oriented programming) concept. Action script 3.0 is purely object oriented and It is very much similar to java script language.

In this post I will show you how to create a simple puzzle game using AS 3.0 inbuilt method call hitTestObject. For this Game I am using the external action script. The Drag game is my document class and the DragDrop class I am using to drag the puzzle piece. I am sharing the source code. Fell free to change your way and have fun.



DragGame class (document class) -

package classes{
import flash.display.Sprite;
import classes.DragDrop;
import TL;
import TM;
import TR;
import BL;
import BM;
import BR;
import endSlide;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;
import flash.events.TimerEvent;
import classes.blinkText;
import fl.transitions.Tween;
import fl.transitions.easing.*;

public class DragGame extends Sprite {
private var tl:TL;
private var tm:TM;
private var tr:TR;
private var bl:BL;
private var bm:BM;
private var br:BR;
private var messageSlide:endSlide;
public var slideTween:Tween;

private var totalPieces:Number;
public var currentPieces:Number;

public function DragGame() {
totalPieces = 6;
currentPieces = 0;
createPuzzlePiece();
}

public function createPuzzlePiece():void {
tl = new TL();
addChild(tl);
randomPosition(tl);
tl.targetPiece = tlt_mc;
tl.addEventListener(MouseEvent.MOUSE_UP, checkTarget);

tm = new TM();
addChild(tm);
randomPosition(tm);
tm.targetPiece = tmt_mc;
tm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);

tr = new TR();
addChild(tr);
randomPosition(tr);
tr.targetPiece = trt_mc;
tr.addEventListener(MouseEvent.MOUSE_UP, checkTarget);

bl = new BL();
addChild(bl);
randomPosition
(bl);
bl.targetPiece = blt_mc;
bl.addEventListener(MouseEvent.MOUSE_UP, checkTarget);

bm = new BM();
addChild(bm);
randomPosition(bm);
bm.targetPiece = bmt_mc;
bm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);

br = new BR();
addChild(br);
randomPosition(br);
br.targetPiece = brt_mc;
br.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
}
private function checkTarget(event:MouseEvent):void {
if(event.currentTarget.hitTestObject(event.currentTarget.targetPiece)) {
currentPieces++;
event.currentTarget.x = event.currentTarget.targetPiece.x;
event.currentTarget.y = event.currentTarget.targetPiece.y;
event.currentTarget.removeEvent();
if (currentPieces == totalPieces) {
  //trace("you win the game");
messageSlide = new endSlide();
addChild(messageSlide);
//messageSlide.alpha = .75;
slideTween = new Tween(messageSlide,"y",Elastic.easeOut,-300,0,2,true);
}
}
else {
event.currentTarget.x = event.currentTarget.origX;
event.currentTarget.y = event.currentTarget.origY;
}

}
public function randomPosition(puzzlePiece:*):void {
puzzlePiece.x = Math.round(Math.random() * (190 - puzzlePiece.width));
puzzlePiece.y = Math.round(Math.random() * (275 - puzzlePiece.height));
puzzlePiece.origX = puzzlePiece.x;
puzzlePiece.origY = puzzlePiece.y;
}
}

}

DragDrop class -
package classes{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;

public class DragDrop extends Sprite {

public var targetPiece:*;
public var origX:Number;
public var origY:Number;

public function DragDrop() {
origX = this.x;
origY = this.y;

this.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzle);
this.addEventListener(MouseEvent.MOUSE_UP, dropPuzzle);
this.buttonMode = true;

}
public function dragPuzzle(event:MouseEvent):void {
//trace("drag");
this.startDrag();
this.parent.addChild(this);
this.filters = [new DropShadowFilter(10, 45, 0x000000, 1, 5, 5, 1, 1)];
}
public function dropPuzzle(event:MouseEvent):void {
//trace("drop");
this.stopDrag();
this.filters = [];
}
public function removeEvent():void {
this.removeEventListener(MouseEvent.MOUSE_DOWN, dragPuzzle);
this.removeEventListener(MouseEvent.MOUSE_UP, dropPuzzle);
this.buttonMode = false;
}

}

}

0 comments:

Post a Comment