Help with an Array used to create a Back-Button

Hi All,

I’m at my wits end (again) with AS3. Been slowly learning it, but then bump into a problem that I spend hours puzzling over…

var i:Number = bbArray.length;
bbArray* returns undefined - don’t know why?!

Overall issue is I’m trying to create an example that records into an array, and a backbutton that can navigate from that array. If I can figure this out, it has a larger application in a project a client dumped in my lap.

Speaking of, I could really use the help of someone VERY familiar w/ AS3 to complete this project - it would be a paying gig, PM me if your interested.

full code below, thanks in advance - b

stop();
var bbArray:Array = new Array();

btn1.addEventListener(MouseEvent.CLICK, btn1Action);
btn2.addEventListener(MouseEvent.CLICK, btn2Action);
btn3.addEventListener(MouseEvent.CLICK, btn3Action);
bButton.addEventListener(MouseEvent.CLICK, bbEvent);

function btn1Action(evt:MouseEvent):void {
    gotoAndStop("one");
    bbArray.push("three");
    //bbArray.push({functionType: "standard", functionTitle: "btn1Action()"});
    trace(bbArray);
}
function btn2Action(evt:MouseEvent):void {
    gotoAndStop("two");
    bbArray.push("two");
    trace(bbArray);
}
function btn3Action(evt:MouseEvent):void {
    bbArray.push("three");
    gotoAndStop("three");
    trace(bbArray);
}

function bbEvent(evt:MouseEvent):void  {
    bbArray.pop();
    var i:Number = bbArray.length;
    trace(i);
    trace(bbArray*);
    gotoAndStop(bbArray*);
    if (i == 0) {
        bButton.alpha=0;
    }

}

I’ll do the easy bit (-:

Don’t forget the index in an array starts at zero.

First item has index 0.
Second item has index 1 etc etc

The length of the array is how many items it contains all together.

Six items (say) means the last item will have index 5.

There is no item with an index of 6.

Which is why bbArray* returns undefined (if i = bbArray.length ).

thanks for the reply. Yes, I get that it needs to start w/ 0 and progresses as items are added to the array. but still getting Undefined whne I trace bbArray*. Feel like it’s gotta be something stupid I’m missing…

revised the code as following:


var bbArray:Array = new Array();
var i:Number = 0;
// buttons get coded here
function bbEvent(evt:MouseEvent):void {
    bbArray.pop();
    if (bbArray.length > 0) {
        var i:Number = bbArray.length;
        trace(i);
        trace(bbArray*);
        gotoAndStop(bbArray*);
    } else {
        bButton.alpha=0;
    }
}

Completely untested, but you could try this:

stop();
var bbArray:Array = new Array();

btn1.addEventListener(MouseEvent.CLICK, btn1Action);
btn2.addEventListener(MouseEvent.CLICK, btn2Action);
btn3.addEventListener(MouseEvent.CLICK, btn3Action);
bButton.addEventListener(MouseEvent.CLICK, bbEvent);

function btn1Action(evt:MouseEvent):void {
    gotoAndStop("one");
    bbArray.push("one"); // "one", not "three" ?
    trace(bbArray);
}
function btn2Action(evt:MouseEvent):void {
    gotoAndStop("two");
    bbArray.push("two");
    trace(bbArray);
}
function btn3Action(evt:MouseEvent):void {
    bbArray.push("three");
    gotoAndStop("three");
    trace(bbArray);
}

function bbEvent(evt:MouseEvent):void  {
    if(bbArray.length > 0)
    {
        var destination:String = bbArray.pop();
        gotoAndStop(destination);
        if(bbArray.length == 0)
        {
           bButton.alpha=0;
        }
    }
}

In that second piece of code you posted, you’re still referring to bbArray* where i is the number of items in the array.

The largest index you can use is i-1.

6 items, last one will have index 5. (Index goes from 0, 1, … 5)
30 items, last one will have index 29. (Index goes from 0, 1, … 29)

Hope that helps.

Brilliant! Thanks so much for giving it a second look! it was the “i-1” after all.

For posterity sake, I’ve posted the final FLA below… shoddy code and all :slight_smile:

thanks again!