adaam
adaam

Reputation: 3706

Conditional IF Statements in AS3

I've written this function to respond to an event listener in a separate function:

EDIT: Updated with working code.

       function greyOutItems(event:Event):void {

        if (DisableCheckBox.selected) {
            myComboBox.alpha = 0.5;

            myComboBox.enabled = false;
            trace("hide combobox success");
        }
        if (DisableCheckBox.selected == false) {
            myComboBox.visible = true;
            myComboBox.enabled = true;
            trace("visible");
        }
    }

The first if statement works well, hiding the ComboBox when the user clicks on the CheckBox, but I want the user to be able to uncheck the same CheckBox and make the ComboBox visible again.

Ideally, rather than setting the alpha property to 0, I'd prefer if the ComboBox was "greyed out" although I haven't yet found the property to do so.

Upvotes: 0

Views: 2861

Answers (3)

Josh
Josh

Reputation: 8149

Your conditionals will always be true because you are setting them to that value in the conditional. "=" is for "assigning" properties only. "==" is "equivalent to". So if (DisableCheckBox.selected = true) actually sets the selected property to true and the way a conditional works, because it successfully set the value, it is marked as a true statement.

function greyOutItems(event:Event):void {
    if (DisableCheckBox.selected == true) {
        myComboBox.alpha = 0;
        trace("success");
    }
    if (DisableCheckBox.selected == false) {
        myComboBox.alpha = 1;
        trace("revisible");
    }
}

In this case, you also should have used else instead of your second conditional, however. An else statement will run slightly faster at runtime (since it doesn't have to check for the second condition, it just runs the else if all conditions are false). Not enough to be noticeable, but it adds up if you are doing this many times

Upvotes: 1

Simon McArdle
Simon McArdle

Reputation: 893

If myComboBox is a movieclip, one way you could "grey out" the buttons rather than using the alpha property is to add a second frame to it where it's behind a grey, half alpha box or similar effects you can control much better in the Flash editor.

In your code you'd then use myComboBox.gotoAndStop(2) to grey the box out or myComboBox.gotoAndStop(1) to return it to normal.

Upvotes: 1

Barış Uşaklı
Barış Uşaklı

Reputation: 13522

Your if statements are wrong. = is assignment. You should use == or just use the selected directly in the if statement.

if (DisableCheckBox.selected) {
    myComboBox.alpha = 0;
    trace("success");
}
if (!DisableCheckBox.selected) {
    myComboBox.alpha = 1;
    trace("revisible");
}

Upvotes: 1

Related Questions