Question

Sorry in advance. I am new in Actionscript and trying to implement a history back button, not a go to previous frame button; in a flash movie, not in a flash web page). My approach is to use an array and push the currentframe to it on any visited frame. My code is not working and it is giving back an error #1502.

Please help me to fix the code I have by now:

var back:Array = new Array(); //global variable


stop();
back.push(MovieClip(this.root).currentFrame); // in every visited frame 


back.addEventListener(MouseEvent.CLICK, backGoTo);
function backGoTo(event:MouseEvent):void
{
    switch (currentLabel)
    {

        case "framelabel":
            for (var i:int = 0; i < back.length; i++)
            {
            gotoAndStop(back[i]); // expected to go to the last visited frame.
            }
            break;
    }
}

thanks a lot in advance.

Was it helpful?

Solution

To get the last element of an array, use back[back.length-1], or if you want to remove the last element and get it back at the same time then back.pop();. So in your case, that for cycle has no use, it's all wrong.. nonsense to check all the elements of an array in this case.

function Softkey3GoTo(event:MouseEvent):void
{
    switch (currentLabel)
    {
        case "m_1":
        case "m_2":
        case "reply":
        case "messages":
            gotoAndStop(back[back.length-2]); // -2 because the current one is -1, the previous frame is -2.
            break;
    }
}

OTHER TIPS

After exploring the Array .pop method I could find the solution to my back button. here is my code for a global back history button. in every frame you must push it to the array. call the last element of the array and delete it at the same time:

var back:Array = new Array(); //global variable


stop();
back.push(MovieClip(this.root).currentFrame); // in every visited frame 


back.addEventListener(MouseEvent.CLICK, backGoTo);
function backGoTo(event:MouseEvent):void
{
    switch (currentLabel)
    {

        case "framelabel":
            back.pop();
    gotoAndStop(back.pop());
            break;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top