A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Click button to play movie in reverse?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    4

    Click button to play movie in reverse?

    Actionscript 3 ----

    Looking to have a movie clip and 2 buttons. Click the right button to make the movie clip go to the right, left button to make it go left (play backwards in the timeline)

    Currently the movie clip is set up as a 90 frame animation that moves across the stage on a motion tween. The right button just makes the movie clip play, so I'm looking to make my left button play that timeline in reverse.

    I've found lots of examples for AS2 online, but none for AS3......

  2. #2
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    It should be fairly similar to the AS2 method of accomplishing this effect.

    More or less:

    Every X seconds change the currentFrame of the movieclip in question. To play normally nextFrame(), to go backwards prevFrame();

    The cool thing about doing this for a movieclip is you can listen for the ENTER_FRAME event, so that if you movie is lagging your animation playing in reverse will lag too!



    Hope this helps, and please see attached for an example.

    -GK

  3. #3
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    Quote Originally Posted by guardiankitty View Post
    Hope this helps, and please see attached for an example.

    -GK
    Wow... I thought I could attach a file on this forum,... i guess I cant... well here is a link to it:

    http://rapidshare.com/files/37945480...wards.fla.html






    and ill post the code here so that if someone else needs it in the future, they can find it.



    for the record, I have 4 objects on the stage,

    Three buttons: fore, back, sto
    and a 90 frame animation: anim


    Hope this helps!

    -gk


    PHP Code:

    //Written by GuardianKitty >'.'<



    //Main Methods
    function forwards(MC:MovieClip):void{
        
    clearListeners(MC);
        
        
    //This next bit does all the real work...
        //It sets up a listener which listens for the flash
        
    MC.addEventListener(Event.ENTER_FRAMEf);
    }

    function 
    backwards(MC:MovieClip):void{
        
    clearListeners(MC);
        
    MC.addEventListener(Event.ENTER_FRAMEb);
    }

    function 
    stops(MC:MovieClip):void{
        
    clearListeners(MC);
    }


    //Used so you dont hit 'forward' then 'forward' and make it go twice as fast
    function clearListeners(MC:MovieClip):void{
        
    MC.removeEventListener(Event.ENTER_FRAMEf);
        
    MC.removeEventListener(Event.ENTER_FRAMEb);
    }


    //Here is a trick that I have learned over the years:
    //Small function names save in processing power!
    //and with enterframes, you need to skimp as much as you can
    function b(e:Event):void{
        
    //if it is not at the beginning of the animation:
        
    if(e.currentTarget.currentFrame!=1){    
            
    //Go Back one Frame
            
    e.currentTarget.prevFrame();
        }
        
    //Otherwise, if it is then:
        
    else{
            
    //Go to the last frame of the animation
            
    e.currentTarget.gotoAndStop(e.currentTarget.totalFrames);
        }
            
            
    }
    function 
    f(e:Event):void{
        
        if(
    e.currentTarget.currentFrame != e.currentTarget.totalFrames){
            
    e.currentTarget.nextFrame();
        }else{
            
    e.currentTarget.gotoAndStop(1);
        }
    }








    //You can use the following like this:
    fore.addEventListener(MouseEvent.CLICK, function(){forwards(anim)});
    back.addEventListener(MouseEvent.CLICK, function(){backwards(anim)});
    sto.addEventListener(MouseEvent.CLICK, function(){stops(anim)});

    //We will stop the animation at the beginning
    anim.stop(); 

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    6
    This code has helped a great deal, but i am trying to work out how the movie clip can stop at certain intervals,

    for example, when you press forward the movie will stop at frame 7, then play forwards or backwards from that point.

    I have a movie clip with 5 images rotating around like a carousel, I need the user to use the back or forward button to rotate the images around and to stop when the image is at the front.
    without the use of a stop button.

    I have looked high and low for tutorials as i would really like to learn this but the code above is the closest i have found so far after a week of searching.

    Any help would be greatly appreciated.

    kind regards.

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    6
    I have attached an swf file as my flash file is to big to upload

    I have changed "guardiankitty" script highlighted in green.
    so that the movie clip stops at frame 6 of the movie and then plays back to frame one.

    see below

    //Written by GuardianKitty >'.'<


    //Main Methods
    function forwards(MC:MovieClip):void{
    clearListeners(MC);


    MC.addEventListener(Event.ENTER_FRAME, f);
    }

    function backwards(MC:MovieClip):void{
    clearListeners(MC);
    MC.addEventListener(Event.ENTER_FRAME, b);
    }

    function stops(MC:MovieClip):void{
    clearListeners(MC);

    }

    function clearListeners(MC:MovieClip):void{
    MC.removeEventListener(Event.ENTER_FRAME, f);
    MC.removeEventListener(Event.ENTER_FRAME, b);

    }

    function b(e:Event):void{

    if(e.currentTarget.currentFrame!==1){
    //Go Back one Frame
    e.currentTarget.prevFrame();
    }
    else{
    e.currentTarget.gotoAndStop (1)
    }


    }
    function f(e:Event):void{

    if(e.currentTarget.currentFrame != 6){
    e.currentTarget.nextFrame();
    }else{
    e.currentTarget.gotoAndStop(6);
    }
    }



    I would like this to happen on each image but do not have a clue on how i can change this.

    AS3 is proving very hard to learn.

    I hope from looking at my file somebody could shed some light.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center