0
votes

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.

stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
    function aClicked(event:MouseEvent):void {
        gotoAndStop(5);
    }

and on frame 5, the actionscript code is

stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
    gotoAndStop(1);
    a_mc.visible = false;
    a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}

see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?

I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying

"Syntax error: package is unexpected"

2

2 Answers

0
votes

Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.

Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?

0
votes

You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.

Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.

To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.