Appplication File :
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” width=”800″ height=”600″ layout=”absolute” xmlns:as3signals=”sample.as3signals.*”>
<as3signals:SignalDeoms/>
</mx:WindowedApplication>
sample.as3signals –SignalDeoms.mxl
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:VBox xmlns:mx=”http://www.adobe.com/2006/mxml” width=”700″ height=”450″ creationComplete=”init()”>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.core.UIComponent;
[Bindable]
public var game_file:Hitted;
public var uiComp:UIComponent
public function init() :void
{
uiComp = new UIComponent();
game_file = new Hitted();
game_file.start();
uiComp.addChild(game_file);
addChild(uiComp);
game_file.circleClick.add(onCircleClicked)
game_file.circleScore.add(updateTheScore);
game_file.gameFinished.add(gameOver);
}
// native signal
public function onCircleClicked( event:MouseEvent ) : void
{
game_file.hitTheBall();
}
public function updateTheScore( playerName:String , score:int ) :void
{
resultBox.text = ” “+score;
}
public function startGame( event :MouseEvent ) :void
{
game_file.SetTimer();
}
public function gameOver(string:String) :void
{
Alert.show(string, “Alert Box”, Alert.OK);
this.removeAllChildren()
}
]]>
</mx:Script>
<mx:HBox id=”contextBox” >
<mx:Label text=”PlayerName :”/>
<mx:TextInput id=”personName”/>
<mx:Button label=”Start And Click The Ball” click=”startGame(event)” enabled=”{personName.text.length>0?true:false}”/>
<mx:Spacer width=”50″/>
<mx:Label text=”result”/>
<mx:TextInput id=”resultBox” editable=”false” />
</mx:HBox>
</mx:VBox>
sample.as3signals — Hitted.as
package sample.as3signals
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import org.osflash.signals.Signal;
import org.osflash.signals.natives.NativeSignal;
public class Hitted extends Sprite
{
//Time variables
private var delay:uint = 30000;
private var repeat:uint = 3;
private var myTimer:Timer;
//Signal variables..
public var circleScore:Signal;
public var circleClick:NativeSignal;
public var gameFinished:Signal
// Score variables
public var hittedCount:int = 0;;
public function start() :void
{
circleScore = new Signal();
gameFinished = new Signal();
circleClick = new NativeSignal(this,MouseEvent.CLICK);
this.graphics.beginFill(0xDDCC00);
this.graphics.drawCircle(120, 40, 40);
this.buttonMode = true;
myTimer = new Timer(1000,10);
}
public function SetTimer():void
{
myTimer.addEventListener(“timer”, timerHandler);
myTimer.start();
}
public function timerHandler(event:TimerEvent):void
{
var Xpos:Number = Math.floor(Math.random() * 500);
var Ypos:Number = Math.floor(Math.random() * 4);
this.x = Xpos;
this.y = Ypos;
if(myTimer.currentCount==10) {
gameFinished.dispatch(“YOU WON THE GAME”);
}
}
public function hitTheBall() :void
{
hittedCount += 10;
circleScore.dispatch(“gameScore”,hittedCount);
}
}
}