0
votes

How do I access the methods of a dynamically created movieclip/object?

For simplicity sake I didn't post code on how I dynamically created the movieclip. Instead, assume its already created. It is an object. It is called field_2. Below it is referenced by using getChildByName('field_' + field.id);

Check_box_component.as

public var testVar:String = 'test';   

public function testReturn()
{
    return 'value returned';
}

Main.as

var temp:MovieClip = MovieClip(getChildByName('field_' + field.id));
trace(temp);
trace(temp.testReturn);
trace(temp.testVar);

Output:

[object Check_box_component]
function Function() {}
test

When I trace temp.testReturn, why does it show "function Function() {}" instead of "value returned"?

This link below helped me get this to this point.

http://curtismorley.com/2007/06/13/flash-cs3-flex-2-as3-error-1119/

2

2 Answers

1
votes

have you tried: trace(temp.testReturn()); ... instead of your trace(temp.testReturn); ... ?

I think you will have the result you are waiting for.

Actually, when doing "temp.testReturn", you are not calling the function. You need to add the parenthesis to make the actual call.

When you make a trace of temp.testReturn, the function is not executed: the trace function tell you the type of temp.testReturn, which is here correctly returned as a "function" type.

1
votes

There is a difference between a function reference and a function call. Parenthesis '()' are an operator sign of ActionScript. They tell the compiler "please try to make a call to what was just behind us". Or at least I hope they are that polite.

A function in ActionScript is an object, like all other stuff. A member of Function class. You can pass it's reference back and forth, you can even call it's methods like call() or apply().

If you want a call, and not a reference, you have to use call operator.

trace(temp.testReturn());

EDIT You accepted an answer while I was typing, sorry for a duplicate answer.