Search code examples
actionscript-3flash-cs6

How to write a event listner to the stage in flash


stage.addEventListener(MouseEvent.CLICK, stageclicked);

if{ function stageclicked(event:MouseEvent):void
}

else{

//1.
var count3:Number = 4;

//2.
var myTimer3:Timer = new Timer(1000,count3);

//3.
myTimer3.addEventListener(TimerEvent.TIMER, countdown3);

//4.
myTimer3.start();

//5.
function countdown3(event:TimerEvent):void {
resttimer.text = String((count3)-myTimer3.currentCount);
if(resttimer.text == "0"){
gotoAndStop(1);  
}

}

I want to add a mouse click event to the stage. It should be like this, if someone doesn't click on the stage then the countdown should start. When it gets to "0" it should play some other scene.


Solution

  • Try this:

    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.MouseEvent;
    
    stage.addEventListener(MouseEvent.CLICK, stageClicked);
    
    var count:int = 4;
    var myTimer:Timer = new Timer(1000,count);
    myTimer.start();
    myTimer.addEventListener(TimerEvent.TIMER, onCount);
    
    function onCount(e:TimerEvent):void
    {
        count --;
        resttimer.text = String(count);
    }
    
    function stageClicked(e:MouseEvent):void
    {
        count = 4;
        resettimer.text = String(count-1);
        myTimer.reset();
        myTimer.start();
    }