# Help with an Array used to create a Back-Button

**URL:** <https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532>\
**Category:** flash\
**Created:** [June 6, 2008, 6:34pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532 "2008-06-06T18:34:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradleykal](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bradleykal/32/1447_2.png) [@bradleykal](https://forum.kirupa.com/u/bradleykal)\
**Post date:** [June 6, 2008, 6:34pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532/1 "2008-06-06T18:34:28Z")

</div>

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

```auto
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;
    }

}

```

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 6, 2008, 6:41pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532/2 "2008-06-06T18:41:08Z")

</div>

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 ).

---

<div class="post-metadata">

**Author:** ![bradleykal](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bradleykal/32/1447_2.png) [@bradleykal](https://forum.kirupa.com/u/bradleykal)\
**Post date:** [June 6, 2008, 6:54pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532/3 "2008-06-06T18:54:08Z")

</div>

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:

```auto

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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 6, 2008, 6:55pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532/4 "2008-06-06T18:55:11Z")

</div>

Completely untested, but you could try this:

```auto
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;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 6, 2008, 6:58pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532/5 "2008-06-06T18:58:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bradleykal](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bradleykal/32/1447_2.png) [@bradleykal](https://forum.kirupa.com/u/bradleykal)\
**Post date:** [June 6, 2008, 7:07pm UTC](https://forum.kirupa.com/t/help-with-an-array-used-to-create-a-back-button/262532/6 "2008-06-06T19:07:45Z")

</div>

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 🙂

thanks again!
