# Is there a better way to write this?

**URL:** <https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950>\
**Category:** programming\
**Created:** [April 9, 2006, 4:43pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950 "2006-04-09T16:43:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 9, 2006, 4:43pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/1 "2006-04-09T16:43:43Z")

</div>

I have this code:  
[AS]  
\_root.menu.abbut0.onPress = function() {  
\_root.btnnum = 0;  
\_root.selSpell(\_root.btnnum);  
}  
\_root.menu.abbut1.onPress = function() {  
\_root.btnnum = 1;  
\_root.selSpell(\_root.btnnum);  
}  
\_root.menu.abbut2.onPress = function() {  
\_root.btnnum = 2;  
\_root.selSpell(\_root.btnnum);  
}  
\_root.menu.abbut3.onPress = function() {  
\_root.btnnum = 3;  
\_root.selSpell(\_root.btnnum);  
}  
//etc.

```auto

Is there a way to write it so it's not so repetitive? Something like

```

\_root.menu[abbut+variable].onPress = function() {  
\_root.btnnum = variable;  
\_root.selSpell(\_root.btnnum);  
}

```auto

The idea being that since the buttons are all named abbut+num, I can use that to get the number of the button.
```

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 9, 2006, 7:49pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/2 "2006-04-09T19:49:55Z")

</div>

```auto

amount=3
for(i=0;i<amount;i++){
_root["menu.abbut"+i].onPress = function() {
                _root.btnnum = i;
                _root.selSpell(_root.btnnum);
            }
}

```

---

<div class="post-metadata">

**Author:** ![ricer](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@ricer](https://forum.kirupa.com/u/ricer)\
**Post date:** [April 9, 2006, 8:50pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/3 "2006-04-09T20:50:25Z")

</div>

bomb ur good in action scripting 😉

btw how do u get the actionscript code: thing

i only know

````auto
 and 
```php
 whats the actionscript thing? :)
````

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 9, 2006, 9:08pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/4 "2006-04-09T21:08:20Z")

</div>

[as] and [/as] will do it.

Bombsledder, wouldn’t that cause delays in the code?

---

<div class="post-metadata">

**Author:** ![ricer](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@ricer](https://forum.kirupa.com/u/ricer)\
**Post date:** [April 9, 2006, 9:16pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/5 "2006-04-09T21:16:10Z")

</div>

ai thnx nich

```auto
 onClipEvent(Load) { 
       _root.testingScreen.gotoAndPlay("itworks");
} 

```

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 9, 2006, 11:43pm UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/6 "2006-04-09T23:43:37Z")

</div>

no because this is a run-once script meaning that it will set it once, then it will be like you had written it all out, just as if you did before

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 10, 2006, 12:01am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/7 "2006-04-10T00:01:06Z")

</div>

ok, thanks 🙂

Edit: it doesn’t appear to be working. Re-thinking what you said about the code only being run once, I think it might be where I put the code. The code is in a function that is being called every frame. I think this might be causing my errors. The loop always outputs the maximum value

[FONT=Courier New][LEFT]

```auto

            x = 0;
            while(x < 9)
            {
                _root.menu["abbut"+x].onPress = function() {
                    _root.btnnum = x;
                    _root.selSpell(_root.btnnum);
                    trace(x); // always outputs 9
                    break; // thought it might help to break the loop
                }
                
                x++
            }// using a while loop out of preference

```

[/LEFT]  
[/FONT]

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 10, 2006, 1:05am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/8 "2006-04-10T01:05:37Z")

</div>

i would definately run it on an

```auto

onLoad=function(){
x = 0;
            while(x < 9)
            {
                _root.menu["abbut"+x].onPress = function() {
                    _root.btnnum = x;
                    _root.selSpell(_root.btnnum);
                    trace(x); // always outputs 9
                    break; // thought it might help to break the loop
                }
               
                x++
            }// using a while loop out of preference 
}

```

also im not sure how the scope should be since i never use \_root[] in a nested clip so maybe someone else could clear that up

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 10, 2006, 1:21am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/9 "2006-04-10T01:21:31Z")

</div>

Try this for that max value thing:

```auto
amount=3
for(i=0;i<amount;i++){
   var a_button:MovieClip = _root["menu.abbut"+i]
   a_button.onPress = function() { 
      _root.btnnum = i;
      _root.selSpell(_root.btnnum);
   }
}

```

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 10, 2006, 1:49am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/10 "2006-04-10T01:49:32Z")

</div>

Neither of those solutions seem to be working for me unfortunately. I might consider just taking an “if it ain’t broke, don’t fix it” approach to this, as much as I hate redundant code…

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 10, 2006, 5:06am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/11 "2006-04-10T05:06:45Z")

</div>

lol.  
using the target[eval] method you define the mnu before hand.

```auto

var amount:Number = 3;
for(var i:Number =0;i<amount;i++){
   _root.menu["abbut"+i].onPress = function() { 
      _root.btnnum = i;
      _root.selSpell(_root.btnnum);
   }
}

```

or:

```auto

var amount:Number = 3;
for(var i:Number =0;i<amount;i++){
   _root.menu["abbut"+i].id = i;
   _root.menu["abbut"+i].onPress = function() { 
      _root.btnnum = this.id;
      _root.selSpell(_root.btnnum);
   }
}

```

not sure which one you want

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 11, 2006, 1:32am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/12 "2006-04-11T01:32:28Z")

</div>

thats the part i ment good job nathan99 lol, @defective thats the same code i wrote except that you put a pointer too \_root.menu[“abbut”+i] instead of just writing , which makes no difference :pleased:

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 11, 2006, 11:19am UTC](https://forum.kirupa.com/t/is-there-a-better-way-to-write-this/184950/13 "2006-04-11T11:19:45Z")

</div>

😛
