Pages

Saturday 28 September 2013

ActionScript 3.0 – Flag Variables

You can create toggle buttons using flag variables. A Flag Variables data type is a boolean object. A Boolean object is a data type that can have one of two values, either true or false.


A Boolean variable can be defined in the following ways:
var flag:Boolean = true;
var flag:Boolean = new Boolean(true);
var flag:Boolean = Boolean(true);

A simple example using flag variables is illustrated below:
Say, for example, you want to create a toggle stop and play button.
You can use the below code for the same.

import flash.events.MouseEvent;

// stop the current keyframe
stop();

// initialize isPaused variable giving it a default value of true
var isPaused:Boolean = true;

// function playPause
function playPause()
{
    if (isPaused)
    {
        play();
        isPaused = false;
    }
    else
    {
        stop();
        isPaused = true;
    }
}

// add an eventlistener to the playPause button symbol
playPauseBtn.addEventListener(MouseEvent.CLICK, onPlayPause);

// call the playPause function in the onPlayPause eventlistener function
function onPlayPause(evt:MouseEvent):void
{
    playPause();
}

No comments:

Post a Comment