Is there a better way to write this?

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.



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


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

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

bomb ur good in action scripting :wink:

btw how do u get the actionscript code: thing

i only know

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

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

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

ai thnx nich

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

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

ok, thanks :slight_smile:

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]


            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]

i would definately run it on an


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

Try this for that max value thing:

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

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…

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


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:


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

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:

:stuck_out_tongue: