Learning Flash



Using Checkboxes

Prototyping isn’t (shouldn’t be) the sexiest code and UI on the block. Get it done, and get it done fast.

One way to get quick feedback is to give the user tweakable parameters, or "tweakers." The following code is a checkbox playground.

Unchecking a column sets it to 0. Locking a column keeps it locked on the last displayed value. If no columns are locked, you can check the random box, and one of the three columns may be unchecked.

Poke around the code to get a feel for it. I use a lot of global variables because the "Next" button just relaunches the movie, and global variables survive this rough reboot. Again, it ain’t pretty, but it works.

if(!initFlag) { // This initializes these global variables
                // Only the first time the loop is run
               
    _global.colbox1 = 1;
    _global.colbox2 = 1;
    _global.colbox3 = 1;
    _global.randy = 0;
    _global.lock1 = 0;
    _global.lock2 = 0;
    _global.lock3 = 0;
   
    _global.ResultA = 0;
    _global.ResultB = 0;
    _global.ResultC = 0;

    _global.initFlag = 1;
}

    //Uncheck "Random" if any of the columns are locked
if ( (lock1+lock2+lock3) > 0) _global.randy = 0;

if(_global.randy == 1){
        _global.colbox1 = 0;
        _global.colbox2 = 0;
        _global.colbox3 = 0;
       
        // At least two columns must be chosen in Random
    while ( (_global.colbox1+_global.colbox2+_global.colbox3) < 2) {
        _global.colbox1 = random(2);
        _global.colbox2 = random(2);
        _global.colbox3 = random(2);
    }
}

updateBoxes();  // calls the user defined function to update all check boxes

//__________Do Something with the results of all those checkboxes

if ( _global.lock1 == 0) {
    randA = randRange(1,9);
    _global.ResultA = randA * colbox1;  // Zeros out result of column is unchecked
}
colA.text = _global.ResultA;

if ( _global.lock2 == 0) {
    randB = randRange(1,9);
    _global.ResultB = randB * colbox2;  // Zeros out result of column is unchecked
}
colB.text = _global.ResultB;

if ( _global.lock3 == 0) {
    randC = randRange(1,9);
    _global.ResultC = randC * colbox3;  // Zeros out result of column is unchecked
}
colC.text = _global.ResultC;

resultSum.text = ResultA + ResultB + ResultC;

// User Defined Functions_________________________

function randRange(min:Number, max:Number):Number {
       var randomNum:Number = Math.floor(Math.random() * (max – min + 1)) + min;
       return randomNum;
}

function updateBoxes () {
   
    if(colbox1 == 1) {
        colm1.selected = true;
    } else {
        colm1.selected = false;       
    }

    if(colbox2 == 1) {
        colm2.selected = true;
    } else {
        colm2.selected = false;       
    }
   
    if(colbox3 == 1) {
        colm3.selected = true;
    } else {
        colm3.selected = false;       
    }

    if(randy == 1) {
        randyOm.selected = true;
    } else {
        randyOm.selected = false;       
    }
   
    if(lock1 == 1) {
        lock_1.selected = true;
    } else {
        lock_1.selected = false;       
    }

    if(lock2 == 1) {
        lock_2.selected = true;
    } else {
        lock_2.selected = false;       
    }
   
    if(lock3 == 1) {
        lock_3.selected = true;
    } else {
        lock_3.selected = false;       
    }   
   
    return;
}

stop();

Download the Flash File
Flash checkBoxMania.fla

One Response to “Learning Flash”

Leave a Reply

(required)

(required)