addchild movieclip appear in wrong scene

i used addchild in movieclip at scene 2, but when i went to scene 1 with button, the object which used addchild keep appeared. and than when i back to scene 2 again, the object which used addchild move faster then before.any idea?

this is my code:

var Batumc:batu_mc = new batu_mc();
var Batumc1:batu_mc = new batu_mc();

var BatasBatu = Batumc.width/12;

        Batumc.x = 100;
        Batumc.y = 50;

        Batumc1.x = 100;
        Batumc1.y = 50;

1 answer

  • answered 2019-09-10 03:17 Organis

    Scenes are not separate entities, they exist only for you to keep your designs logically ordered. When compiled, all scenes are converted into a single main timeline MovieClip.

    Then, when your script adds anything to a timeline, Flash Player won't remove these things just because you gotoAndPlay to another frame.

    That's exactly why mixing multi-frame timeline designs with scripting is not a good idea: Flash Player doesn't automatically control things you program, like it does to pre-designed timeline entities.

    So, the possible solutions.

    1. Take full control of the things you do. Before going to another 'scene', unsubscribe all the events you subscribed to (that is important, because subscriptions can bind objects and funtions and prevent their auto-ending) and remove all the things you've manually added there. With removeChildren() method, for example.

    2. Wrap each 'scene' of yours into a container MovieClip. Whatever you do inside container, it will disappear once you gotoAndPlay to another frame, where that container just does not physically exist. The need to unsubscribe things is still in effect though.