Search code examples
actionscript-3flashflash-cs6

Increasing variable by 1 every time the frame is active with AS3


This is properly an easy question to answer for some, hehe: How can I increase a varible value by 1 every time it enters that spesific frame?

I curently have this code which runs one time and then it will not run again next time the timeline is on the frame:

// This is at the end of the animation

var DAY = 0;

DAY++;
dayTextField.text = DAY;

gotoAndStop(7); // Go to the beginning and re-loop the animation

I have tried to do this code in a function that run from a stage.addEventListener(Event.ENTER_FRAME, function), but this just increased the value non-stop over and over again.

Thank you.


Solution

  • For some wierd reason I got it to work. This code is added in the beginning and not at the end as I did last time. Here is what I did:

    var DAY = 0;
    
    stage.addEventListener(Event.ENTER_FRAME, newDayFunc);
    
    function newDayFunc(event:Event) {
        if (currentFrame == 1286) {
            if (Moon.hitTestObject(wallNewDay)) {
                DAY++;
                dayTextField.text = DAY;
                trace("What day it is:", DAY);
            }
        }
    }
    

    **Here is what I ended up using after figuring something out, hehe:

    // I figured it out. This is on the last frame.
    
    // Variables
    var DAY = 0;
    //
    // Listeners
    stage.addEventListener(Event.ENTER_FRAME, newDayFunc);
    //
    // Functions
    function newDayFunc(event:Event) {
            if (DAY < 7)) {
                DAY++;
                dayTextField.text = DAY;
                trace("What day it is:", DAY);
                stage.removeEventListener(Event.ENTER_FRAME, newDayFunc);
            } else {
                // Added other eventListeners that needed to be removed
                gotoAndStop("finished"); // The name of the finish frame
            }
    }
    //