mercredi 7 décembre 2016

Use button to enable timer inside a function

I'm making something in actionscript 3, and when I press the first button btnSkaffPenger, it increases the number by 1 for each click. But my second button btnTrePrinter is supposed to increase the number by 1 every 2 seconds, automatically, but only works once, and doesnt reset. (I added so you can only press the button once, I don't think that interferes with the function resetting)

Thanks

The buttons code:

btnTrePrinter.addEventListener(MouseEvent.CLICK, trePrinter);
function trePrinter(evt:MouseEvent):void
{
    var timer:Timer = new Timer(2000);
    var harVentet:Function = function(event:TimerEvent):void{
        timer.removeEventListener(TimerEvent.TIMER, harVentet);
        timer = null;

        sumPenger++
        txtSumPenger.text = sumPenger.toString();
    }
    timer.addEventListener(TimerEvent.TIMER, harVentet);
    timer.start();

    btnTrePrinter.mouseEnabled = false;
    btnTrePrinter.alpha=0.4;
}

Full code:

import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;

var sumPenger:int = 0;

btnSkaffPenger.addEventListener(MouseEvent.CLICK, penger1);
function penger1(evt:MouseEvent):void
{
    sumPenger++
    txtSumPenger.text = sumPenger.toString();
}

btnTrePrinter.addEventListener(MouseEvent.CLICK, trePrinter);
function trePrinter(evt:MouseEvent):void
{
    var timer:Timer = new Timer(2000);
    var harVentet:Function = function(event:TimerEvent):void{
        timer.removeEventListener(TimerEvent.TIMER, harVentet);
        timer = null;

        sumPenger++
        txtSumPenger.text = sumPenger.toString();
    }
    timer.addEventListener(TimerEvent.TIMER, harVentet);
    timer.start();

    btnTrePrinter.mouseEnabled = false;
    btnTrePrinter.alpha=0.4;
}

Aucun commentaire:

Enregistrer un commentaire