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.

Answers


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


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");
}

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.


Need Your Help

Regular expression to extract a section from the wikipedia page

regex

I am trying to parse a wikipedia page and need to extract a particular section of the page using regex. In the below data, I just need to extract the data inside {{Infobox...}} section.

user defined Error collection in Visual C#

c# dll error-handling

I want to write an user defined error collection class which should collect all the Error's. When we validate an entity object if there is no error it should go and save to the Database. if Error t...