PDA

View Full Version : Set dynamic text in mc from parent



tpann
November 25th, 2008, 02:33 PM
This is probably easy but I'm missing something... I'm trying to assign a text string to a dynamic text field within a movieclip.

In my FLA's library I have my movie clip "mc" (linkage = MC) and mc has this code in it:


// internal mc code
var tf:TextField = new TextField();
addChild(tf);

In Frame 1 I have:

var mc = new MC();
addChild(mc);
mc.tf.text = "here is some text.";

This produces the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.

What am I missing?

creatify
November 25th, 2008, 02:39 PM
one way:


var tf:TextField = new TextField();
tf.name = "txtField";
addChild(tf);



var mc:MC = new MC();
addChild(mc);
mc.getChildByName("txtField").text = "here is some text.";

but why distribute your code across timelines? That can get confusing, so:


var mc:MC = new MC();
var tf:TextField = new TextField();
tf.text = "here is some text.";
mc.addChild(tf);
addChild(mc);

tpann
November 25th, 2008, 02:44 PM
Interesting... did it work for you? It doesn't work for me. I get an error saying I'm trying to assign the property text to a DisplayObject.

creatify
November 25th, 2008, 03:35 PM
I misled you a bit with:


var mc:MC = new MC();
addChild(mc);
mc.getChildByName("txtField").text = "here is some text.";


Due to the way objects are added to the display list, you'd have to listen to an event in order to access the textField on the sprite, only after it's added to the stage if you're using timeline code within MC.

If you're setting the linkage of a clip in the library, a Class MC is automatically generated. You won't see this class, but it does exist within this ApplicationDomain.

The issue gets sticky when you start mixing timeline code within clips that have an associated Class due to the specific time/events that will actually run that timeline code. So I really recommend the attached way to do this if you're wanting to keep code on the timeline. Try to keep all of your code (especially code used to generate objects) on the main timeline. If you do add a linkage identifier to a library object, and you want that object to contain it's own code - then create a class for that object and code within that.

see attached.