# Please Help with with array an variable

**URL:** <https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864>\
**Category:** flash\
**Created:** [June 1, 2008, 12:28am UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864 "2008-06-01T00:28:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alchris](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alchris](https://forum.kirupa.com/u/alchris)\
**Post date:** [June 1, 2008, 12:28am UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864/1 "2008-06-01T00:28:31Z")

</div>

I am having trouble setting the variable for selectedBtn.

I want to have it so I can put selectedBtn = 3; So that when you first open the swf. It will have the last button already selected.

I include my code below. Any help is very much appreciated.

//code  
var nameArr:Array = new Array(“page 1”, “page 2”, “page 3”, “page 4”);  
var buttArr:Array = new Array (b1, b2, b3, b4);  
for (i=0;i\<buttArr.length;i++) {  
var tmpBtn = buttArr\*;  
tmpBtn.id = i;

tmpBtn.onRollOver = over;  
tmpBtn.onRollOut = out;  
tmpBtn.onPress = press;  
tmpBtn.onRelease = release;  
tmpBtn.buttText.buttonText.text = nameArr\*;  
}

function over() {  
if (\_global.selectedBtn != this) {  
this.gotoAndPlay(2);  
}  
}

function out() {  
if (\_global.selectedBtn != this) {  
this.gotoAndPlay(7);  
}  
}

function press() {  
if (\_global.selectedBtn != this) {  
this.gotoAndStop(17);  
this.\_parent.\_global.selectedBtn.gotoAndPlay(7);  
}  
}

function release() {  
if (\_global.selectedBtn != this)  
\_global.selectedBtn = this;  
this.gotoAndStop(18);

```
  switch (this.id) {
case 0:    
    getURL(yahoo.com, "_blank");
    break ;
case 1:
    getURL(yahoo.com, "_blank");
    break ;
case 2:
    getURL(yahoo.com, "_blank");
    break ;
case 3:
    getURL(yahoo.com, "_blank");
    break ;

```

}  
}

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [June 1, 2008, 5:09pm UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864/2 "2008-06-01T17:09:00Z")

</div>

test this, not sure how your site is set up but this should work:

```auto

_global.selectedBtn = null;
var nameArr:Array = new Array("page 1", "page 2", "page 3", "page 4");
var buttArr:Array = new Array (b1, b2, b3, b4);

for (i=0;i<buttArr.length;i++) {
    var tmpBtn = buttArr*;
    tmpBtn.id = i;
    tmpBtn.onRollOver = over;
    tmpBtn.onRollOut = out;
    tmpBtn.onPress = press;
    tmpBtn.onRelease = release;
    tmpBtn.buttText.buttonText.text = nameArr*;
}

function over() {
    if (_global.selectedBtn != this) {
        this.gotoAndPlay(2);
    }
}

function out() {
    if (_global.selectedBtn != this) {
        this.gotoAndPlay(7);
    }
}

function press() {
    if (_global.selectedBtn != this) {
        this.gotoAndStop(17);
        _global.selectedBtn.gotoAndPlay(7);
    }
}

function release() {
    if (_global.selectedBtn != this) {
        _global.selectedBtn.gotoAndPlay(7);
        _global.selectedBtn = this;
        this.gotoAndStop(18);

        switch (this.id) {
            case 0:
                getURL(yahoo.com, "_blank");
                break ;
            case 1:
                getURL(yahoo.com, "_blank");
                break ;
            case 2:
                getURL(yahoo.com, "_blank");
                break ;
            case 3:
                getURL(yahoo.com, "_blank");
                break ;
            default :
                break;

    }
}

```

---

<div class="post-metadata">

**Author:** ![alchris](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alchris](https://forum.kirupa.com/u/alchris)\
**Post date:** [June 1, 2008, 9:53pm UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864/3 "2008-06-01T21:53:00Z")

</div>

Thanks for your help. But It didn’t work. I attached the flash file so it will be easier to see how I built my buttons.

It has some thing to do with making selectedBtn = to a number but I am missing something in the code or just not writing the code properly.

---

<div class="post-metadata">

**Author:** ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)\
**Post date:** [June 1, 2008, 11:03pm UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864/4 "2008-06-01T23:03:53Z")

</div>

just put this in the last line of your code…

buttArr[3].onPress();

it wil select the last button automatically onload …

[quote=alchris;2334446]Thanks for your help. But It didn’t work. I attached the flash file so

it will be easier to see how I built my buttons.

It has some thing to do with making selectedBtn = to a number but I am missing something in the code or just not writing the code properly.[/quote]

---

<div class="post-metadata">

**Author:** ![alchris](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alchris](https://forum.kirupa.com/u/alchris)\
**Post date:** [June 2, 2008, 12:48am UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864/5 "2008-06-02T00:48:20Z")

</div>

[quote=sparkdemon;2334462]just put this in the last line of your code…

buttArr[3].onPress();

it wil select the last button automatically onload …[/quote]

Thank you so much. That works great!!! Can you also help create a variable for the “selectedBtn” Because I am using this on an html using SWFObject script and flashvars.

Example of how I am going to use:

Page1.html  
so.addVariable(“selectedBtn”, “1”);

Page2.html  
so.addVariable(“selectedBtn”, “2”);

---

<div class="post-metadata">

**Author:** ![alchris](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alchris](https://forum.kirupa.com/u/alchris)\
**Post date:** [June 4, 2008, 4:14am UTC](https://forum.kirupa.com/t/please-help-with-with-array-an-variable/261864/6 "2008-06-04T04:14:32Z")

</div>

Figured out the answer to my own question.

instead of buttArr[3].onPress();

buttArr[selectedBtn].onPress();

now  
selectedBtn =2; // input number 0-3 to select which button to select on start.
