Eyla
Eyla

Reputation: 5861

actionscript 3 declearing variables

Greeting, I have a problem with listed code that every time I click on btn1 the variable (num) will be initialize to 1. so what is the best practice in my case to initialize the variable (num) for only one time when the flash loaded.

Regards,

stop();

var num:Number =1;
function b1(event:MouseEvent):void
{
    gotoAndStop(1);
    num = num +1;

    trace(num);
}

function b2(event:MouseEvent):void
{
    gotoAndStop(2);
    trace(num);

}

btn1.addEventListener(MouseEvent.CLICK, b1);

btn2.addEventListener(MouseEvent.CLICK, b2);

Upvotes: 1

Views: 141

Answers (1)

Brian Driscoll
Brian Driscoll

Reputation: 19635

If this code is in a frame on your timeline, it will be executed every time the frame is loaded. This means that your num declaration will be executed every time the frame is loaded as well.

I'd recommend putting the variable declaration in another frame and make sure that your playhead does not revisit that frame (otherwise the variable will be reinitialized).

Upvotes: 6

Related Questions