Referencing objects on stage

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.

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?

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:


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.

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.


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:

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

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 :stuck_out_tongue:

Now, which approach do you think is better?

this is probably the simplest solution


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.