2

I am trying to add an init() function to a MovieClip, but when I run the function from scene1 the variables that were set in the MovieClip are not defined yet... The MovieClip was dragged to the stage from the library.

scene1:

mc.init(null);

MovieClip:

var _default = 5;

function init(num) {
     if(num == null) {
          trace(_default);
     } else {
          trace(num);
     }
}

This is tracing "undefined" instead of "5"; Is there a way of fixing this problem?

1 Answer 1

1

The problem is that any code thats placed directly in the main timeline will always run before the code thats directly in the MovieClip.

The way to get around this would be to let flash finish running that code in both timeline and the MovieClip first, and then call the function from the timeline after its done.

The easiest way to do this would be to use an event listener:

Timeline:

addEventListener( Event.ENTER_FRAME, onEnterFrame );

function onEnterFrame( e:Event ):void {
    myObject.init(null);
    removeEventListener( Event.ENTER_FRAME, onEnterFrame );
}

That way the timeline will wait until the first frame has started to call the init function of your MovieClip.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.