Just need to run a test to see which button was pressed! how?

I have a menu with 6 buttons. I have set up a variable called button pressed that tracks which button was pressed through numbers (if button 1 pressed, value of buttonPressed becomes 1 etc…) I want to test for which button was pressed as little as possible. If I say onClipEvent(mouseDown) it tells me that this can only be used for movie clips. How can i run a simple test to see which button was pressed?

thanks
k

set the button’s name as a number and add the following code to your button’s AS;

on(press){
_root.buttonPressed = _name
}

okay. but then how would I create the test code that would be able to id which button was hit every time? Is there a general place that I can put the code like a standard code module or something and then every time a button is clicked, the program checks this test code to see what to do? for ex,

onClipEvent(mouseDown){ //any time the mouse is clicked check here

if 1…
if 2…
if 3…

}

but this gives me that error. Maybe I am putting the code in the wrong place? thanks

k

select a keyframe from your main timeline and than add the code below ,
[AS]_root.onMouseDown = function() {
switch (buttonPressed) {
case 1 :
trace(“button 1 pressed”);
case 2 :
trace(“button 2 pressed”);
//…
default :
trace(“no button pressed”);
}
};
[/AS]

just browsing through some posts

something I have wondered for a while…

why use cases? or perhaps I miss what their purpose is

i thought “switch” is better than “if” for this problem, beacuse it has a default property…

cases are used to reduce the amount of if statements, especially when you have to check for several different possibilites to check for on one variable. The default is just like an else, only it works after testing all other possibilities, unlike an “if/else” which only checks the one possibility.

so that code seems to be working however, now whenever I click on a button, I have to click twice for it to recognize what I am clicking. any ideas why thats happening?

k

cases are used to reduce the amount of if statements, especially when you have to check for several different possibilites to check for on one variable. The default is just like an else, only it works after testing all other possibilities, unlike an “if/else” which only checks the one possibility.

this is why i used “case” :wink: ,thanx a lot for the discription paradox244 :slight_smile:

second solution is;

add the following code to your buttons :
[AS]
on(press){
_root.clickWhat(_name);
}[/AS]
than add the following to your main timeline
[AS]
function clickWhat(buttonPressed) {
switch (buttonPressed) {
case 1 :
trace(“button 1 pressed”);
case 2 :
trace(“button 2 pressed”);
//…
default :
trace(“no button pressed”);
}
};[/AS]

is there a way that I can add the actionscript once and it will hold up throughout the entire timeline rather than just till the next keyframe?

thanks for all of your help by the way. this can just be so frustrating at times

k

make an new actions layer and let it has as many frames as you need. in the keyframe you have to put the actions you want to have the whole movie…

so can you use switch/cases with un-numbered (that is names etc) cases?

i.e.

switch(buttonPressed)
case smiley:
trace(“smiley button pressed”);
}

so can you use switch/cases with un-numbered (that is names etc) cases?

i.e.

switch(buttonPressed)
case smiley:
trace(“smiley button pressed”);
}

yes you can :wink:

so the second solution appears to be working. Basically this is what I have done:

for my buttons i have this:

  
on(press){
 _root.menu.clickWhat(1);
}

//This returns a 1 to the function clickWhat

For my main timeline I have this:


function clickWhat(buttonPressed) {
 switch (buttonPressed) {
 case 1:
  trace("tables pressed");
  break;
 case 2 :
  trace("accessories pressed");
  break;
 case 3 :
  trace("specs pressed");
  break;
 case 4:
  trace("Colors pressed");
  break;
 default :
  trace("no button pressed");
 }
};

The only problem that has been coming up is: When I click button 1, the trace box comes up and says tables pressed. It does the same for accessories. When I press specs and colors however, it traces the message twice. Any ideas why this is happening?
thanks

k