PDA

View Full Version : Referencing SubComponents Inside Components


Sameal
06-10-2002, 08:22 PM
I have built a custom component which I call 'VComboBox'. This component is composed of a single label and an instance of Macromedia's builtin ComboBox 'FComboBoxSymbol'. I named the instance of the FComboBoxSymbol to 'comboObject'.

Now when I drag a copy of my component onto the stage and in ActionScript I use the following code:

_root.myVComboInstance.comboObject.addItem("red", 0);

It works and adds the item into the combo box inside my custom component. However when I dynamically create an instance of my VComboBox it does not work at all, like below:

_root.attachMovie("VComboBox", myNewCombo, depth++);
_root.myNewCombo.comboObject.addItem("red", 0);

The combo appears but however no item is added. And when I click on the arrow for the combo to drop-down nothing happens. I am trying to find out what I am doing wrong? Is there a way to make this work? Also why is my custom component acting completely different based on whether it was created at design-time, or whether it was created dynamically at run-time? Any assistance would be greatly appreciated.

Sameal :cool:

leblob
06-11-2002, 12:39 AM
Shouldn't it be

_root.attachMovie("VComboBox", "myNewCombo", depth++);

rather than

_root.attachMovie("VComboBox", myNewCombo, depth++);

?

My guess is : if you don't use the quotes flash thinks myNewCombo is not the name to use but a variable in wich is stored the name to use.

Sameal
06-11-2002, 12:24 PM
newControlName is a variable that is set just before the attachMovie() method.

It reads:

newControlName = "control" + controlInstance++;
_root.attachMovie("VComboBox", newControlName, depth++);
_root.myNewCombo.comboObject.addItem("red", 0);

If I was to enclose the variable newControlName with quotes then it would become a string literal which would not work in my case because I am dynamically creating any number of these VComboBox instances.

My problem lies in the fact that when I instantiate the MovieClip in ActionScript it doesn't seem to behave at all the same as when it is drug onto the stage. Why is this? Do I need to do something extra to the MovieClip before using it?

Sameal
06-11-2002, 01:40 PM
After much thought, and a good rest last night I have finally been able to figure out what is going on. My component isn't being swonked!!! Doh!

I was under the false illusion that swonking only had to do with loading and sending data to and from an outside source. However as I have learned today everything must be swonked. This is a very strange programming environment to me now, but I now see why it has to be this way.

I placed an onLoad event on _root and sure enough my components were loading correctly when instantiated dynamically. I just needed to wait until they were done loading before I used a reference to them.

For completion also the above examples are wrong, instead of:

_root.newControlName.comboObject.addItem("red", 0);

I was using...

_root[newControlName].comboObject.addItem("red", 0);

Just in case someone tries to recreate this they won't get stumped.