2

I have a very strange problem with a simple test I'm trying to do in AS3.

I have two functions on my first frame:

function function1 ():void {
    trace ('function1 executed');

    mc.gotoAndStop (2);

    function2 ();
}

function function2 ():void {
    trace ('function2 executed');

    var test_mc:test_lib = new test_lib ();
    addChild (test_mc);
}

As you can see, function1 changes the frame on "mc" and also calls function2, which adds a child movie clip from the library in the stage.

Function1 is called from inside another simple movie clip, on the 60th frame, like so:

Object(parent).function1 ();

This child movie clip that is added, is just a black square with a trace action on it's first frame.

The trace action should work as soon as the child is added, but it's not. However, if I remove or comment the line mc.gotoAndStop(2), the trace action works normally. It also works if I put the mc.gotoAndStop(2) AFTER I call the function2.

I can't see why this is happening.

This happened on a larger project that I was working so I decided to isolate the problem on a new file, and create the example above. This is very weird.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
DanielFox
  • 675
  • 2
  • 9
  • 18
  • 1
    I don't know why it doesn't work but I know how to fix this. Call `function2` with delay (ex. 1 ms). `setTimeout(function2, 1);` It works! – Daniil Subbotin Mar 04 '15 at 21:31
  • Weird, huh? As I said, it works too if I just call the mc.gotoAndStop(2) AFTER the function2(); I just wanted to know why this is happening. Seems like a bug to me. – DanielFox Mar 05 '15 at 14:49

2 Answers2

1

try shifting your function2 inside mc frame 2.

main timeline:

function function1 ():void {
    trace ('function1 executed');

    mc.gotoAndStop (2);
}

in mc -> on frame 2

trace ('function2 executed');
var test_mc:test_lib = new test_lib ();
addChild (test_mc);

You will have to slightly amend the code inside mc using .root

Fergoso
  • 1,584
  • 3
  • 21
  • 44
  • That's a possible solution. But I can already make it work by calling function2() before the mc.gotoAndStop(2), as I said. I just wanted to know why is this happening... – DanielFox Mar 05 '15 at 14:50
0

What is mc? If mc is the MovieClip that contains both of your functions, then your code obviously cannot work because after you gotoAndStop(2), you can no longer call function2() because function2() does not exist on the second frame.

apen
  • 672
  • 4
  • 14