A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Creating a back button AS3

  1. #1
    Junior Member
    Join Date
    Feb 2015
    Posts
    6

    Creating a back button AS3

    Was looking at a few posts that had some similar ideas but none of them seem to be exactly what I want/need for this program.

    Basically I am creating a simple simulator program to use at work. What I have right now is 5 frames each with different images of a cell phone. What I want to do is create a back button that doesn't just take you back to the previous frame..it takes you back to the previous frame you were on..that way when I am hitting the back button I am not having to go through three other frames I don't need to see..I am going back to the last two that I were on. I hope this makes sense and if not I can provide further details upon request.

    I am working in actionscript 3, I used the code snippet provided by going to previous frames but again that isn't exactly what I want. If I am on frame 1 and go to frame 5 and then hit the previous button it takes me through frames 4 3 and 2 when I just want to hit the back button to go back to frame 1..

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    I think it should be pretty simple, but before I help you I need to know how you go forward with your frames.
    If you have 5 buttons taking you to each of the 5 frames then you could simply use the 5 buttons, if you follow, so the buttons are always on the screen.

  3. #3
    Junior Member
    Join Date
    Feb 2015
    Posts
    6
    Thanks for the quick reply.

    I have multiple buttons on a layer labeled buttons however, I have the back button on its own layer that expands across all 5 frames. This button however currently just takes you back to the previous frame before it and not back to the previous frame I was on prior.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    if you can attch your fla (save having to try and mimick your file), that would be great, but I was thinking more along the lines of simply choosing the page you wanted , like so.

    so you just want to go bakc to the last page only, not further back and continually

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Here is one that remembers all pages visited and stores them in an array, then removes it from the array as you go back, perhaps this is more of what you desire, play around with it.

    actually I changed the code on that file, here it is, if you wish to swap it for this code.
    PHP Code:
    stop();

    import flash.events.MouseEvent;

    var 
    i:Number;
    var 
    firstRun:Boolean;

    var 
    currentPage:Number;
    var 
    pageArray:Array;
    var 
    buttonArray:Array;

    if (! 
    firstRun)
    {
        
    currentPage 1;
        
    firstRun true;
        
    pageArray = new Array();
        
    pageArray.push(currentPage);
        
    buttonArray = new Array(button1,button2,button3,button4,button5);
        for (
    0buttonArray.lengthi++)
        {
            var 
    pageButton:Object buttonArray[i];
            
    pageButton.value = (1);
            
    pageButton.numText.text = (1);
            
    pageButton.addEventListener(MouseEvent.CLICKdoPage);
        }
        
    backButton.addEventListener(MouseEvent.CLICKgoBack);
        
    stage.addEventListener(Event.ENTER_FRAMEshowHide);
    }

    function 
    showHide(e:Event):void
    {
        if (
    pageArray.length 1)
        {
            
    backButton.visible true;
        }
        else
        {
            
    backButton.visible false;
        }
    }

    function 
    doPage(e:MouseEvent):void
    {
        
    currentPage e.currentTarget.value;
        
    pageArray.push(currentPage);
        
    gotoAndStop(currentPage);
    }

    function 
    goBack(e:MouseEvent):void
    {
        if (
    pageArray.length 1)
        {
            
    pageArray.pop();
            
    gotoAndStop(pageArray[pageArray.length 1]);
        }

    Last edited by fruitbeard; 02-04-2015 at 11:49 AM.

  6. #6
    Junior Member
    Join Date
    Feb 2015
    Posts
    6
    Looks good, I will try this once I get home from work and will post my file then as well.

  7. #7
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    I got carried away with playing with this, I'm sure you can get something from the plethora of code amongst those three.

  8. #8
    Junior Member
    Join Date
    Feb 2015
    Posts
    6
    Thanks, I appreciate your help!

  9. #9
    Junior Member
    Join Date
    Feb 2015
    Posts
    6
    Alright finally home and added this code to my file. I had to adjust the names of the array buttons to match the instances to the buttons I already have in place. My problem though is that the code is preventing me from clicking on anything. It recognizes there is a button on the screen but when clicking on it nothing happens.

    PHP Code:
    import flash.events.MouseEvent;

    var 
    i:Number;
    var 
    firstRun:Boolean;

    var 
    currentPage:Number;
    var 
    pageArray:Array;
    var 
    buttonArray:Array;

    if (! 
    firstRun)
    {
        
    currentPage 1;
        
    firstRun true;
        
    pageArray = new Array();
        
    pageArray.push(currentPage);
        
    buttonArray = new Array(volbtn,menubtn,setbtn,dvcbtn,callmanbtn);
        for (
    0buttonArray.lengthi++)
        {
            var 
    pageButton:Object buttonArray[i];
            
    pageButton.value = (1);
            
    pageButton.numText.text = (1);
            
    pageButton.addEventListener(MouseEvent.CLICKdoPage);
        }
        
    backButton.addEventListener(MouseEvent.CLICKgoBack);
        
    stage.addEventListener(Event.ENTER_FRAMEshowHide);
    }

    function 
    showHide(e:Event):void
    {
        if (
    pageArray.length 1)
        {
            
    backButton.visible true;
        }
        else
        {
            
    backButton.visible false;
        }
    }

    function 
    doPage(e:MouseEvent):void
    {
        
    currentPage e.currentTarget.value;
        
    pageArray.push(currentPage);
        
    gotoAndStop(currentPage);
    }

    function 
    goBack(e:MouseEvent):void
    {
        if (
    pageArray.length 1)
        {
            
    pageArray.pop();
            
    gotoAndStop(pageArray[pageArray.length 1]);
        }
    }  

    /* Volume Menu*/
    volbtn.addEventListener(MouseEvent.CLICKvolume);
    function 
    volume(event:MouseEvent):void
    {
        
    gotoAndStop(2);
    }

    /* Menu Selection*/
    menubtn.addEventListener(MouseEvent.CLICKmenu);
    function 
    menu(event:MouseEvent):void
    {
        
    gotoAndStop(3);


  10. #10
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    can you post your FLA file? TinyUpload
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  11. #11

  12. #12
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Thanks,

    the problem is that fruitbeard used MovieClips in his file, while you are using the same code with Buttons. Buttons don't have the ability to store variables, so you'll either have to modify the code quite a bit, OR you can just turn all your buttons into movieclips and the code will work like a charm! However, you may notice that the cursor won't change to a hand when moving your mouse over the buttons/movieclips -- this is because they're movieclips, but this can be easily fixed by finding this code:

    PHP Code:
            var pageButton:MovieClip buttonArray[i];
            
    pageButton.value = (1);
            
    pageButton.addEventListener(MouseEvent.CLICKdoPage); 
    and adding this line:

    Code:
            var pageButton:MovieClip = buttonArray[i];
    	pageButton.buttonMode = true;
            pageButton.value = (i + 1);
            pageButton.addEventListener(MouseEvent.CLICK, doPage);
    However, this still won't work for you, because in the buttonArray, you've put ALL the buttons from ALL the frames, but the code runs on Frame 1 -- which means that the buttons that don't exist in Frame 1, but are in buttonArray, will return an error when the code tries to execute commands on them. You can either have all your buttons on Frame 1, and disabling some of them, and enabling them on their respective frames -- OR -- you could use the same code for each frame, with the buttonArray varying according to which frame you're in, however this is prone to failure as the code is most likely suited for one frame only.

    If you need furhter assistance with this, then just say so, and we'll try to come up with a different solution which takes different frames into consideration
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  13. #13
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Welcome back Nig , i turned 29 today, im old XD
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  14. #14
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Thanks, angelhdz Oh right, didn't realize you were that much older than me xD Hope you had a great birthday
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

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