# Referencing objects on stage

**URL:** <https://forum.kirupa.com/t/referencing-objects-on-stage/264277>\
**Category:** flash\
**Created:** [June 24, 2008, 4:56pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277 "2008-06-24T16:56:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bardic](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bardic/32/3708_2.png) [@bardic](https://forum.kirupa.com/u/bardic)\
**Post date:** [June 24, 2008, 4:56pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277/1 "2008-06-24T16:56:47Z")

</div>

Ok, this is a dumb question but I’m just horribly stuck.  
In my script I go toframe 5, and there are several textfields and clips on stage, but I can’t reference them from my code. I get an error saying the objects don’t exist.  
If I move the objects of stage and just move them into place later that works but I’m pretty sure there is a better way to do it.

Any thoughts or links? I’ve checked around google but I don’t know exactly what I should be searching for here.

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [June 24, 2008, 6:15pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277/2 "2008-06-24T18:15:27Z")

</div>

If the objects are not on the stage on that frame then they won’t exist. Are you sure they are still there on frame 5?

---

<div class="post-metadata">

**Author:** ![bardic](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bardic/32/3708_2.png) [@bardic](https://forum.kirupa.com/u/bardic)\
**Post date:** [June 24, 2008, 6:29pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277/3 "2008-06-24T18:29:07Z")

</div>

Ya, I’m sure. I’ll try to give a better example:

I have a 10 frame movie. First frame tells it to stop. On the stage I have a button. When I click the button, in my document class , I gotoAndStop (5). On frame 5 there is a textfield.

so the code:

```auto

function onClick(e:MouseEvent)
{
   gotoAndStop(5);
   txt.text = "Hello world";
}

```

Just example code but ya get the idea. If I do that, an error returns say that txt doesn’t exist yet it’s on stage on frame 5.

Attached is a little example file of what is happening.

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [June 24, 2008, 6:43pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277/4 "2008-06-24T18:43:49Z")

</div>

The problem is that when you goto frame 5 it is trying to change “txt”'s text before it has finished creating the text field. A way to fix that would be to add an enterframe listener and a try/catch statement that keeps trying to change the text objects text until it succeeds, then it removes the enter frame listener.

```auto

package
{
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.events.*;
    
    public class testMain extends MovieClip
    {
        public function testMain():void
        {
            btn.addEventListener(MouseEvent.CLICK, helloWorld)

        }
        
        private function helloWorld(ev:MouseEvent):void
        {
            gotoAndStop(5);
            this.addEventListener(Event.ENTER_FRAME, changeText);
        }
        private function changeText(e:Event):void{
            try{txt.text = "Hello there";
            this.removeEventListener(Event.ENTER_FRAME, changeText);}
            catch(e:Error){}
        }
    }
}

```

I’m sure this isn’t the best way to do it but it works.

edit: Actually this is simpler:

```auto
private function helloWorld(ev:MouseEvent):void
        {
            gotoAndStop(5);
            txt.addEventListener(Event.COMPLETE, changeText);
        }
        private function changeText(e:Event):void{
            txt.text = "Hello there";
        }

```

---

<div class="post-metadata">

**Author:** ![bardic](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bardic/32/3708_2.png) [@bardic](https://forum.kirupa.com/u/bardic)\
**Post date:** [June 24, 2008, 7:01pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277/5 "2008-06-24T19:01:14Z")

</div>

Thanks for the insight. I will have to try this next time I encounter this problem instead of just having all my objects hidden off stage on the first frame 😛

Now, which approach do you think is better?

---

<div class="post-metadata">

**Author:** ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)\
**Post date:** [June 24, 2008, 7:17pm UTC](https://forum.kirupa.com/t/referencing-objects-on-stage/264277/6 "2008-06-24T19:17:39Z")

</div>

this is probably the simplest solution

```auto

function onClick(e:MouseEvent)
{
   gotoAndStop(5);
   MovieClip(this).txt.text = "Hello world";
}

```

should work…havent tested though  
if you just use txt it looks for a variable thats current at compile time.  
if you type this to movieclip first it wont check until run time…the problem is this will cause you headaches if txt isnt on stage when that function is called.
