Search code examples
flashactionscriptactionscript-2

How to make a number variable change when a button is pressed? Actionscript 2.0


I need the number variable to change each time a button is pressed. The variable is called "total".

my stage code:

var total:Number = 4;

if (total == 5){
    gotoAndPlay (2) 
}

else if (total == 6){ 
    gotoAndPlay (5)  
}

else {
    trace("there was an error ya PUTZ!!!") 
}

my Button code:

on (release) {
    set(total, 5);
}

Nothing happens when the button is pressed. There is nothing in the Compiler Errors.

Sorry if this is a stupid question. Im ass new to code and it can sometimes be hard to find flash tutorials. even sorry-er if this is formatted weird, still learning how to use this site and all that sorta stuff....


Solution

  • First of all, if you are starting anew, I advise to start with AS3, not AS1/2. Flash is dead anyway, but AS2 is like 15 years since obsolete so the amount and quality of tutorials and references are next to non-existent.

    Then, your problem.

    I don't remember, if set command is what I think you expect it to, but the basic = operator is still more readable and easier to understand.

    Then, as you press the button, nothing seem to happen because your frame code is not a sleeping watchdog. It executes once and that's it. In order to get some reaction, you should set up some kind of watcher.

    Frame script:

    var store:Number = 4;
    var total:Number = 4;
    
    // This is a predefined method name that will fire FPS times a second,
    // where FPS stands for Frames Per Second setting of your application.
    function onEnterFrame()
    {
        // Check if the variable is actually changed.
        if (total == store)
        {
            // Refrain from action if not.
            return;
        }
        
        if (total == 5)
        {
            // Keep the new value.
            store = total;
            
            // The desired action.
            gotoAndPlay(2);
        }
        else if (total == 6)
        {
            // Keep the new value.
            store = total;
            
            // The desired action.
            gotoAndPlay(5);
        }
        else
        {
            // Revert the variable.
            total = store;
            
            // Notify the user.
            trace("there was an error ya PUTZ!!!");
        }
    }
    

    The button code:

    on (release)
    {
        total = 5;
    }