-
If statement for onPress of multiple buttons
Hey all,
I have a list of 8 buttons. I want each button to call a function, which has already been created and pass some arguments.
So I have something like this:
test = function(btnX, btnY){
myOtherFunction(btnX);
anotherFunction(btnY);
};
but1.onPress = function(){
test(1,2);
};
but2.onPress = function(){
test(2,2);
};
but3.onPress = function(){
test(3,2);
};
but4.onPress = function(){
test(4,2);
}
Is there a way to create a switch or something else that is cleaner than making repeated .onPress functions for each button?
Thanks.
-
Senior Member
Code:
test = function (btnX, btnY) {
myOtherFunction(btnX);
anotherFunction(btnY);
};
for (var z = 1; z<=4; z++) {
_root["but"+z].onPress = function() {
test(this._name.split("but").join(""), 2);
};
}
HTH,
zlatan
-
Code:
for(i=1;i<=8;i++){
this["but"+i].i = i;
this["but"+i].onPress = function(){
_root.test(this.i,2);
}
}
-
I appreciate both of your responses, but I accidentally misled you. The buttons are not but1 through but 4, I was just hoping for some sort of switch statement. The buttons actually have names, not sequenced numbers...my bad. Like the following...
test = function(btnX, btnY){
myOtherFunction(btnX);
anotherFunction(btnY);
};
btnEnter.onPress = function(){
test(1,2);
};
btnExit.onPress = function(){
test(2,2);
};
btnNextScene.onPress = function(){
test(3,2);
};
btnHelp.onPress = function(){
test(4,2);
}
Is there an easier/cleaner way than how I did the above?
Sorry for my confusion and thanks for any help.
-
My code is for 8 buttons
Code:
test = function (btnX, btnY) {
myOtherFunction(btnX);
anotherFunction(btnY);
};
for(i=1;i<=8;i++){
this["but"+i].i = i;
this["but"+i].onPress = function(){
_root.test(this.i,2);
}
}
-
do this
function functioncaller(btname){
switch(btname){
case btnEnter:
do this
case btnExit
do this
case btnNextScene
do this
case btnHelp
do this
}
and on evry bt u can call this function to check the name and do the corresponding thing
and even if u want to pass it all things dynamical through AS then use typeof and store the result in array and use it accordingly
-
Senior Member
lariat1997, if the buttons have different names then the you would just do it like you have above, but if you reeeeeally want the use a loop, then you can use an array.
you would push all your buttons into an array, then you would loop though the array in and asign a onPress handler to each button in the array.
if you want i can show you how, but it is not something that i would do..
zlatan
-
dudeqwerty,
So are you saying to keep what I have? There is no real good way to create a switch statement of something w/less code that is preferred? If not, that's fine, what I have works.
Thanks.
-
Senior Member
you can do it like psb's example, but it makes no difference.
The only reason you would use a loop to assign eventHandler's is when you have lots of repeating code but with a slight variation, like in your first example.
using something like psb's function will probably make more code than is required so it wouldn't be very efficient.
but if you really want to, and assuming that btnY will always be 2, then you can use the following code:
Code:
var myButtons = [btnEnter, btnExit, btnNextScene, btnHelp];
function test(btnX, btnY) {
myOtherFunction(btnX);
anotherFunction(btnY);
}
for (var z = 0; z<myButtons.length; z++) {
myButtons[z].index = z+1;
myButtons[z].onPress = function() {
test(this.index, 2);
};
}
HTH,
zlatan
-
Sounds good, thanks for the help to you all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|