-
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..
-
.
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.
-
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.
-
.
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
-
.
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 (i = 0; i < buttonArray.length; i++) { var pageButton:Object = buttonArray[i]; pageButton.value = (i + 1); pageButton.numText.text = (i + 1); pageButton.addEventListener(MouseEvent.CLICK, doPage); } backButton.addEventListener(MouseEvent.CLICK, goBack); stage.addEventListener(Event.ENTER_FRAME, showHide); }
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.
-
Looks good, I will try this once I get home from work and will post my file then as well.
-
.
Hi,
I got carried away with playing with this, I'm sure you can get something from the plethora of code amongst those three.
-
Thanks, I appreciate your help!
-
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 (i = 0; i < buttonArray.length; i++)
{
var pageButton:Object = buttonArray[i];
pageButton.value = (i + 1);
pageButton.numText.text = (i + 1);
pageButton.addEventListener(MouseEvent.CLICK, doPage);
}
backButton.addEventListener(MouseEvent.CLICK, goBack);
stage.addEventListener(Event.ENTER_FRAME, showHide);
}
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.CLICK, volume);
function volume(event:MouseEvent):void
{
gotoAndStop(2);
}
/* Menu Selection*/
menubtn.addEventListener(MouseEvent.CLICK, menu);
function menu(event:MouseEvent):void
{
gotoAndStop(3);
}
-
Prid - Outing
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
-
-
Prid - Outing
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 = (i + 1);
pageButton.addEventListener(MouseEvent.CLICK, doPage);
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
-
Designer, Programmer, Musician
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
-
Prid - Outing
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|