Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer in Action Script 3

I have problem that I need to create timer but I want to pass on a variable to it, how to do it? Is it possible in AS3?

I tried like this:

            bonusPlayer1Timer = new Timer(5000);
            bonusPlayer1Timer.addEventListener(TimerEvent.TIMER, bonusChanges(player1));
            bonusPlayer1Timer.addEventListener(TimerEvent.TIMER_COMPLETE, bonusChangesRemove(player1));
            bonusPlayer1Timer.start();

function bonusChanges(event:TimerEvent, playerBonus:Player):void {
    switch (playerBonus.bonus) {
        case 0 :
            playerBonus.multipleShooting = false;
            playerBonus.bonus = -1;
            break;
...}}

But I have error:

1067: Implicit coercion of a value of type Player to an unrelated type flash.events:TimerEvent.
1136: Incorrect number of arguments.  Expected 2.

And this error is in the bold line.

Can I use it in this way? Or I have to create two the same function for every of my players because I am not allow to pass on any different arguments to the timer function?

Thank you,

like image 670
canimbenim Avatar asked Feb 13 '26 06:02

canimbenim


2 Answers

Create a class that extends the Timer class and add a property for the Player.

public class PlayerTimer extends Timer
{
    public var thePlayer:Player;

    public function PlayerTimer(delay:Number, repeatCount:int=0)
    {
        super(delay, repeatCount);
    }       
}

Using your example the code would be something like this:

bonusPlayer1Timer = new PlayerTimer(5000);
bonusPlayer1Timer.thePlayer = new Player();
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER, bonusChanges);
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER_COMPLETE, bonusChangesRemove);
bonusPlayer1Timer.start();

function bonusChanges(event:TimerEvent):void {
    var playerBonus:Player = PlayerTimer(event.target).thePlayer; 
    switch (playerBonus.bonus) {
        case 0 :
            playerBonus.multipleShooting = false;
            playerBonus.bonus = -1;
            break;
...}}
like image 196
Nathan Smith Avatar answered Feb 14 '26 18:02

Nathan Smith


You can never pass more than one argument into the function triggered by an EventListener. You need to find other ways of passing your information around, such as the solution provided by Nathan Smith.

like image 31
Mark D Avatar answered Feb 14 '26 20:02

Mark D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!