Search code examples
actionscript-3flashpositionadobeplayback

AS3 for transfering pauseposition of MC1 to MC2


Hi I'm quite a new on flash and need some code. I have 3 MCs say MC1, MC2 and MC3. I will have 3 dedicated buttons that will pause the music of one MC and transfer the frame position to another MC and start playing. so say for example if MC2 is playing and I press the MC3 button I would like it to take the pause position of MC2 (not MC1) and continue playing from that frame on MC3 as well as switching the visuals from MC2 to MC3. it's a Multilingual app and all 3 MC's have the same frame length. in other words I would like to switch between languages. Thanks in advance, any help would be great.

EDIT: up till now I have

mtlyrvult.stop();
itlyrvult.stop();

engvult.addEventListener(MouseEvent.CLICK, playMC1); 

function playMC1(e:MouseEvent):void {
    itlyrvult.stop();
    enlyrvult.gotoAndPlay(itlyrvult.currentFrame);
    itlyrvult.gotoAndStop(1);  //frame one is empty
    engvult.mouseEnabled = false;
    itvult.mouseEnabled = true;
}

itvult.addEventListener(MouseEvent.CLICK, playMC2); 

function playMC2(e:MouseEvent):void {
    enlyrvult.stop();
    itlyrvult.gotoAndPlay(enlyrvult.currentFrame);
    enlyrvult.gotoAndStop(1);    //frame one is empty
    itvult.mouseEnabled = false;
    engvult.mouseEnabled = true;
} 

This switches from one language to another. Now my client gave me another language. mtlyrvult. And I don't know how AS3 will recognise which mc is playing to take the the pauseposition/currentframe from it.


Solution

  • I stick to your code (no class, method, members, addChild, ...). I didn't try the final draft with Flash or SDK.

    I assume that you have:

    • 3 MovieClip objects : enlyrvult, itlyrvult, mtlyrvult;
    • 3 InteractiveObject (SimpleButton, MovieClip, ...) objects : engvult, itvult, mtvult;
    • and that enlyrvult is playing when we begin here (if not: enlyrvult.play(); or enlyrvult.stop();).

    itlyrvult.stop();  // or itlyrvult.gotoAndStop(1); 
    mtlyrvult.stop();  // or mtlyrvult.gotoAndStop(1);
    
    engvult.addEventListener(MouseEvent.CLICK, playMC1); 
    itvult.addEventListener(MouseEvent.CLICK, playMC2); 
    mtvult.addEventListener(MouseEvent.CLICK, playMC3); 
    
    // play enlyrvult
    function playMC1(e:MouseEvent):void {
        // stop them
        itlyrvult.stop();
        mtlyrvult.stop();
    
        // play me
        enlyrvult.gotoAndPlay(mtlyrvult.currentFrame);
    
        // hide them
        itlyrvult.gotoAndStop(1);    //frame one is empty
        mtlyrvult.gotoAndStop(1);    //frame one is empty
    
        // My trigger is out, theirs are fine
        engvult.mouseEnabled = false;
        itvult.mouseEnabled = true;
        mtvult.mouseEnabled = true;
    }
    
    // play itlyrvult
    function playMC2(e:MouseEvent):void {
        // stop them
        enlyrvult.stop();
        mtlyrvult.stop();
    
        // play me
        itlyrvult.gotoAndPlay(enlyrvult.currentFrame);
    
        // hide them
        enlyrvult.gotoAndStop(1);    //frame one is empty
        mtlyrvult.gotoAndStop(1);    //frame one is empty
    
        // My trigger is out, theirs are fine
        itvult.mouseEnabled = false;
        engvult.mouseEnabled = true;
        mtvult.mouseEnabled = true;
    } 
    
    // play mtlyrvult
    function playMC3(e:MouseEvent):void {
        // stop them
        enlyrvult.stop();
        itlyrvult.stop();
    
        // play me
        mtlyrvult.gotoAndPlay(itlyrvult.currentFrame);
    
        // hide them
        enlyrvult.gotoAndStop(1);    //frame one is empty
        itlyrvult.gotoAndStop(1);    //frame one is empty
    
        // My trigger is out, theirs are fine
        mtvult.mouseEnabled = false;
        engvult.mouseEnabled = true;
        itvult.mouseEnabled = true;
    }
    

    OR, for as much (300?) as you want...

    // add to your import:
    import flash.utils.Dictionary;
    
    // in your const/var section
    const STARTING_FRAME:int = 1;
    var dict = new Dictionary();  // mapping and memory
    var currentTrack:MovieClip;   // we will know who's last
    
    initAll();
    playTrack(enlyrvult, STARTING_FRAME, engvult);
    
    function clickHandler(e:MouseEvent):void {
        var playheadFrame:int = currentTrack.currentFrame;                      // we remember position
        var trigger:InteractiveObject = (e.currentTarget as InteractiveObject); // who shot me ?
        var nextTrack:MovieClip = (dict[trigger] as MovieClip);                 // who's next ?
    
        resetAll();  // and again.. (http://en.wikipedia.org/wiki/Sisyphus)
        playTrack(nextTrack, playheadFrame, trigger);
    }
    
    function playTrack(mc:MovieClip, fram:int, iObj:InteractiveObject):void {
        currentTrack = mc;
        currentTrack.gotoAndPlay(fram);
        iObj.mouseEnabled = false;
    }
    
    function resetAll():void {
       for (var key:InteractiveObject in dict) { key.mouseEnabled = true; }
       for each (var value:MovieClip in dict) { value.gotoAndStop(1); } // diff-> each
    } 
    
    function initAll():void {
       dict[engvult] = enlyrvult;
       dict[itvult] = itlyrvult;
       dict[mtvult] = mtlyrvult;
       //dict[avult] = alyrvult; //<- new one like this: dict[trigger]=lyrMC; add as much as you can!
    
       for (var key:InteractiveObject in dict) {
           key.addEventListener(MouseEvent.CLICK, clickHandler);
       }
    
       resetAll();
    }