0
votes

I have a checkbox in a component:

<s:CheckBox id="myCB_1" />

In my main.mxml I need to test for the state of the checkbox. I originally had all my code in main.mxml, but it was getting really long, and I thought that it was better practice to break my code into components. Also, I have other projects where the same concept will apply, so I really want to figure this out.

I have the following function :

private function checkAlarms(currentTime:Date):void
{
    if (!breakfastAlarmSounded)
    {
        if ((currentTime.hours > breakfastTime.hours) || ((currentTime.hours == breakfastTime.hours) && (currentTime.minutes >= breakfastTime.minutes)))
        {
            if (myCB_1.selected)
            {
                playBreakfastAudioAlarm();
            }
            if (myCB_2.selected)
            {
                playBreakfastVisualAlarm();
            }

            breakfastAlarmSounded = true;
        }
    }
...

simply addressing the component, as in:

myComponent.myCB_1.selected

doesn't work. Someone mentioned that I need to create a variable in my component that refers to the id (myCB_1) of checkbox, but I don't really understand or know how to do that, and they didn't elaborate.

How do I test for the status of the CheckBox "myCB_1" in the component from within my main.mxml?

many thanks,

Mark

(newbie)

4
Why doesn't this work? Can you show us how you're invoking it from main.mxml? How does main include your custom component?bedwyr
Please post the content of main.mxml in full -- we need to see how you embedded your new custom component in main.bedwyr

4 Answers

0
votes

With very little information, I'm going to suspect you originally had the CheckBox included in main.mxml and moved it to a custom component. If so, you need to address the CheckBox's ID via the custom component's ID. Something like this (from main.mxml):

if(yourComponentsID.myCB_1.selected)
{
    ...
}

If this isn't the case, please edit your post and give us more detail.


EDIT

You said you created a new custom component and moved the CheckBox into it. Great, that's a helpful start :) When you included your new component in your main.mxml file, it should look something like this:

<component:YourNewComponent />

Of course, however you named it (and whichever namespace is used to reference it) will be different from my example, but the principle should still apply. In main.mxml, you need to give your custom component a unique ID string so you can reference it within main:

<component:YourNewComponent id="myComponent" />

From here on, you should be able to reference the component, and any public elements within it: myComponent.myCB_1.

0
votes

It would be useful to provide more details about the context in which you're using this script. Nonetheless I'm going to throw out some information that may help.

In order for the script to access the component, it has to be within the scope of the component. Usually that means one of the following:

  • You have a <script> tag in the MXML, with code in it that references components within the same MXML file.
  • You have a <script source='external.as'/> tag in the MXML, where external.as is referencing components in the MXML file.
  • You are creating the component in your script and you have a definition for the component within ActionScript (ex. var myCB_1:CheckBox; is within the class definition).

If the script and the component aren't within the same scope then they can't see one another.

0
votes

You need to refer to the checkbox through the component. Lets say that you use your component in your main like this:

<local:MyComponent id="myComponent" />

In your script, you want to refer to it:

if(myComponent.myCB_1.selected) { // do something }
0
votes

Strangely enough, it works. I was getting a getting an 1119 error (Description 1119: Access of possibly undefined property myCB_1 through a reference with static type Class.) when I refer to the component with dot notation (myComponent.myCB_1.selected) and an 1120 error (Description 1120: Access of undefined property myCB_1) when not addressing it via myComponent.

With these errors I never thought to try running the thing. Long story short - it runs with or without addressing the component (???) go figure!

thanks for all the input and would love to hear any other comments.

MCE