TheSoundMan
TheSoundMan

Reputation: 13

AS3 running an event for a set duration after button (or anything) event

For the purposes of the question, Imagine I have an object onstage. When I click on another button, I want the colour to change for 1 second, then revert back again when finished.

Here's what my demo code looks like:

Button.addEventListener(MouseEvent.CLICK, Colour_Change);

function Colour_Change(evt: MouseEvent): void {
    var my_color: ColorTransform = new ColorTransform();
    my_color.color = 0xFF0000;
    Coloured_Object.transform.colorTransform = my_color;
}

What I am wanting is some sort of timer function to be incorporated in the above function. I haven't got any idea how to do it, hence why there's no implementation.

Upvotes: 1

Views: 110

Answers (3)

akmozo
akmozo

Reputation: 9839

To do that you can use, as other answers said, a Timer object or the setTimeout() function, like this :

// the current color of our target object
var default_color:ColorTransform;
// the delay in milliseconds
var delay:int = 1000; 

btn.addEventListener(MouseEvent.CLICK, Colour_Change);
function Colour_Change(evt: MouseEvent): void {

    // save the current color of our target object
    default_color = target.transform.colorTransform;

    var new_color:ColorTransform = new ColorTransform();
        new_color.color = 0xFF0000;

    target.transform.colorTransform = new_color;

    var timer:Timer = new Timer(delay, 1);
        timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
            // after the delay, we use the default color of our target
            target.transform.colorTransform = default_color;
        })
        timer.start();

    // using setTimeout(), you have to disable this if using the Timer
    var timeout:int = setTimeout(
        function(){
            clearTimeout(timeout);
            // after the delay, we use the default color of our target
            target.transform.colorTransform = default_color;
        }, 
        delay
    );

}

Hope that can help.

Upvotes: 0

Trex
Trex

Reputation: 118

To further explain the Timer class as used above:

when creating a new timer

myTimer=new Timer(1000,1)

the first number in the brackets is the number of milliseconds you want the timer to run for (e.g. 1000 = 1 second) The second number is how many times you want the timer to repeat (or 0 for infinite repetition).

Every time the timer reaches the time you entered (1000), this is will trigger any event listeners for the event Timer_Event.TIMER, so for example if u wanted to make it change color on and off, you could have multiple repetitions on the timer and change the function.

Other useful things timers can do:

You can add an event listener for

Timer_Event.TIMER_COMPLETE     

(goes off when all repetitions are complete)

myTimer.currentCount

will return the number of repetitions the timer has done so far.

Upvotes: 1

Jonny Henly
Jonny Henly

Reputation: 4223

You should use the flash.utils.Timer class.
Adobe ActionScript 3.0 Reference for the Timer class

The following should be enough to get you headed in the right direction:

import flash.utils.Timer;

var myTimer:Timer = new Timer(1000, 1); // 1 second
var running:Boolean = false;

Button.addEventListener(MouseEvent.CLICK, Colour_Change);
myTimer.addEventListener(TimerEvent.TIMER, runOnce);

function Colour_Change(evt: MouseEvent): void {
    var my_color: ColorTransform = new ColorTransform();
    my_color.color = 0xFF0000;
    Coloured_Object.transform.colorTransform = my_color;
    if(!running) {
        myTimer.start();
        running = true;
    }
}

function runOnce(event:TimerEvent):void {
    // code to revert the button's color back goes here

    myTimer.reset();
    running = false;
}

Let me know if you need more help or if this example has errors via this answer's comments section.

Upvotes: 1

Related Questions