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

**URL:** <https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786>\
**Category:** Uncategorized\
**Created:** [July 26, 2004, 7:10am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786 "2004-07-26T07:10:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_davis13](https://avatars.discourse-cdn.com/v4/letter/k/c5a1d2/32.png) [@kyle\_davis13](https://forum.kirupa.com/u/kyle_davis13)\
**Post date:** [July 26, 2004, 7:10am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/1 "2004-07-26T07:10:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 7:39am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/2 "2004-07-26T07:39:09Z")

</div>

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

on(press){  
\_root.buttonPressed = \_name  
}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 7:59am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/3 "2004-07-26T07:59:33Z")

</div>

> [@e.s.x.s](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 9:16am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/4 "2004-07-26T09:16:37Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 9:44am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/5 "2004-07-26T09:44:59Z")

</div>

just browsing through some posts

something I have wondered for a while…

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 12:25pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/6 "2004-07-26T12:25:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 1:37pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/7 "2004-07-26T13:37:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 5:50pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/8 "2004-07-26T17:50:24Z")

</div>

> [@paradox244](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 5:50pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/9 "2004-07-26T17:50:41Z")

</div>

> 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” 😉 ,thanx a lot for the discription paradox244 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 6:03pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/10 "2004-07-26T18:03:08Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 6:05pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/11 "2004-07-26T18:05:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 26, 2004, 9:13pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/12 "2004-07-26T21:13:54Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 27, 2004, 2:19am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/13 "2004-07-27T02:19:50Z")

</div>

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

i.e.

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 27, 2004, 6:22am UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/14 "2004-07-27T06:22:48Z")

</div>

> 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 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 27, 2004, 4:57pm UTC](https://forum.kirupa.com/t/just-need-to-run-a-test-to-see-which-button-was-pressed-how/58786/15 "2004-07-27T16:57:06Z")

</div>

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

for my buttons i have this:

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

```

//This returns a 1 to the function clickWhat

For my main timeline I have this:

```auto

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
