7 Replies Latest reply on Jun 3, 2017 11:27 AM by jambiscotti

    how to code a button to go to previous frame?

    jambiscotti

      hi, does anyone know how to code a button to the previously viewed frame? that is, not the previous frame in the timeline, but the previous frame that the person was on. so for example, if someone is on frame 16 and they jump to frame 21, how can i code a button to take that person back to frame 16? (without specifying 16 exactly, because the person might have been on a different frame). sorry if it sounds a little confusing, basically i'm trying to make a pause menu but i want the person to go back to where they were once they close the pause menu.

       

      thank you for reading!

        • 1. Re: how to code a button to go to previous frame?
          Colin Holgate MVP & Adobe Community Professional

          Your UI will have some sort of button that the user used to get to the frame they are on at the moment. In very one of those scripts you would add the frame they are about to leave to an array, and if on the next screen they use a back button, you would go to the frame that was the last entry in the array.

           

          The logic gets harder if you want them to be able to go forwards again, but even then it's not too terrible. Here's some of the bits you would need (written here as AS3, but it would be much the same in JavaScript, only without the variable types):

           

          var visited:Array = [];

          var visitedpointer:int = 0;

           

          gosomewhereBtn.addEventListener(MouseEvent.CLICK,gosomewhere);

          gobackBtn.addEventListener(MouseEvent.CLICK,goback);

          goforwardBtn.addEventListener(MouseEvent.CLICK,goforward);

           

          function gosomewhere(e:MouseEvent){

            visited.push(currentFrame);

          // go to whereever that button would take the user, eg, frame 10:

          gotoAndStop(10);

            visitedpointer = visited.length-1;

          goforwardBtn.alpha = .5;

          gobackBtn.alpha = 1;

          }

           

          function goback(e:MouseEvent){

          if(gobackBtn.alpha==.5) return;

          visitedpointer--;

          gotoAndStop(visited[visitedpointer]);

          if(visitedpointer==0) gobackBtn.alpha=.5; 

          }

           

          function goforward(e:MouseEvent){

          if(goforwardBtn.alpha==.5) return;

          visitedpointer++;

          gotoAndStop(visited[visitedpointer]);

          if(visitedpointer==(visited.length-1)) goforwardBtn.alpha=.5;

          }

           

          I typed all that here, and didn't test it! You should get the general idea, and apply the approach to your code, rather than to copy and paste what I've shown.

           

          There are complications to think about, like if the user goes back three times, then goes to a new destination, you might well clear out the visited list, otherwise there could be unexpected behavior if they go back again.

          • 2. Re: how to code a button to go to previous frame?
            Colin Holgate MVP & Adobe Community Professional

            I answered the question you asked, then re-read what you wrote, and realized that you just want a pause menu that can be closed. The usual approach would not be to jump to the frame with the pause menu in it, but to have the pause menu be a hidden movieclip that sits on top of everywhere in the timeline. Then you only have to show or hide that movieclip, to reveal the screen that the user was on at the time.

             

            Another approach is to have a movieclip in the library that you add using code, and when the user has finished pausing you would remove the movieclip with code.

            • 3. Re: how to code a button to go to previous frame?
              jambiscotti Level 1

              hi, thank you so much for your detailed response, it was very helpful! your second suggestion seems a lot simpler, so i put all the pause menu objects into one movie clip and i have no trouble hiding it (using a button within the movie clip that hides 'this') but i can't seem to get it to show up again. i've named the movie clip instance as PAUSEMENU but when i try to get a button to do 'PAUSEMENU.play();' or 'PAUSEMENU.visible = true;' it doesn't work. do i need to somehow define the pause menu within the code before i refer to it? thank you again for your help (:

              • 4. Re: how to code a button to go to previous frame?
                Colin Holgate MVP & Adobe Community Professional

                It's unusual to name symbols in all caps, so maybe you've got a letter wrong? Also, so far it hasn't mattered whether you were doing AS3 or HTML5 Canvas, but as far as talking to symbols on the stage it can be different. In AS3 you can talk to a symbol as you enter the first frame it exists. With Canvas it's not so easy. The simple solution to that is to have the symbol on a frame before where your script is.

                 

                So, use lowercase, like 'pausemenu', then there is less to get wrong when typing. pausemenu.visible = true should work.

                • 5. Re: how to code a button to go to previous frame?
                  jambiscotti Level 1

                  thanks for your reply! hmm.. for some reason it still doesn't work, i get an error "#1009: Cannot access a property or method of a null object reference". i tried extending the movie clip to cover the whole timeline, because maybe it wasn't working because the movie clip wasn't present in the frame. but i then noticed that the close button only hides the button itself with the "this" command, and not the rest of the movie clip. i tried fiddling with that, but when i write pausemenu.visible instead of this.visible, it tells me i'm accessing an unidentified property. am i not allowed to refer to the movie clip within the movie clip itself..? (since the button is inside the movie clip) i've double-checked all the instance names and they seem to be fine..

                  • 6. Re: how to code a button to go to previous frame?
                    Colin Holgate MVP & Adobe Community Professional

                    If the button that shows the pause menu is at the same level as the pause menu movieclip, its click function would be like:

                     

                    function pauseclicked(e:MouseEvent){

                      pausemenu.visible = true;

                    }

                     

                    If the button that closes the pause menu is inside the pause menu movieclip, its click function would be:

                     

                    function closeclicked(e:MouseEvent){

                      e.currentTarget.parent.visible = false;

                    }

                     

                    Or you could have the show and hide functions on the main timeline, like:

                     

                    function showpause(){

                    pausemenu.visible = true;

                    }

                    function hidepause(){

                    pausemenu.visible = false;

                    }

                     

                    The show pause menu button function would be:

                     

                    function pauseclicked(e:MouseEvent){

                      showpause();

                    }

                     

                    and the close button inside the pause menu would have:

                     

                    function closeclicked(e:MouseEvent){

                      MovieClip(parent).hidepause();

                    }

                    • 7. Re: how to code a button to go to previous frame?
                      jambiscotti Level 1

                      excellent! i managed to get it to show up again! thank you so much! i've just got two more questions:
                      1. how can i get the animation inside the pause menu to play again when it becomes visible?
                      2. when i use the first code for the pause button inside the movie clip, it tells me "e" is unidentifiable. was that supposed to be something else? i tried movieclip(parent).visible = false; instead but then everything on the canvas disappears! if it continues not to work, i might just move the button outside the movie clip as that seems simpler.