1
votes

I have a problem accessing Objects on my stage...

In my case I created a movieclip (mc1) with two different movieclips (mc2, mc3) inside it and a separate dynamic text field (tf1) on the stage.

If you click on mc2 or mc3, the tf1 should change to a fixed text. Unfortunately, I always get errors accessing tf1...

Code from mc1:

mc2.addEventListener(MouseEvent.MOUSE_DOWN, Mouse_up2);
mc3.addEventListener(MouseEvent.MOUSE_DOWN, Mouse_up3);

function Mouse_up2(event:MouseEvent):void {
  stage.tf1.text="text2"; }

function Mouse_up3(event:MouseEvent):void {
  stage.tf1.text="text3"; }
1

1 Answers

0
votes

The origin of this error message comes from the fact that your textField isn't a child of the Stage but of the MainTimeline, which is itself placed on the Stage.

trace(tf1.parent); // [object MainTimeline]
trace(tf1.parent.parent); // [object Stage]

To target your textField, you just have to write:

mc2.addEventListener(MouseEvent.MOUSE_DOWN, Mouse_up2);

function Mouse_up2(event:MouseEvent): void
{
    tf1.text = "text2";
}

Note

The keyword this makes reference to the object MainTimeline contained by the object Stage. These two codes are equivalent:

tf1.text = "text2";
this.tf1.text = "text2";

Therefore, you can also write:

this.mc2.addEventListener(MouseEvent.MOUSE_DOWN, Mouse_up2);

function Mouse_up2(event:MouseEvent): void
{
    this.tf1.text = "text2";
}