keyboard events

  • grindy
  • Newbie
  • Newbie
  • User avatar
  • Posts: 8

Post 3+ Months Ago

I have a drum with 6 buttons on it that play a sound when the mouse is clicked on each button.

I want to make it so the sounds play when i hit keys 1 to 6 on the top of my keyboard.

this is the code I am trying to use to do this

public function initListeners() {
stage.addEventListener(KeyboardEvent.KEY_UP, keyDownHandler);
}

private function keyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case(49):
// activate button 1
break;
case(50):
// activate button 2
break;
case(51):
// activate button 3
break;
case(52):
// activate button 4
break;
case(53):
// activate button 5
break;
case(54):
// activate button 6
break;
default:
// do nothing.
}
}

what I am not sure of is what to put where // activate button 1 to 6 is

I am a noob to as3 and everyone i ask about this tells me to look it up. which i have but i still havent been able to figure it out. my buttons are labeled key1, key2, key3, key4, key5, key6.

I tried to put "initListeners(key1)" in place of //activate button 1. That didnt work and I just dont know enough about the as3 code. Is there anyone out there who could help me or give me a hint or something.

I have been working on this for a couple of weeks and nothing has worked.
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

grindy wrote:
.........................

I tried to put "initListeners(key1)" in place of //activate button 1. That didnt work and I just dont know enough about the as3 code. Is there anyone out there who could help me or give me a hint or something.

First you need to capture the KeyDownEvent. I Purpose to capture event on Outer Element or the wrapper of the all drum interaction Code. Your switch statement is perfect, It will call the initListeners(key1) function.
Just first make sure proper capturing of event.
  • grindy
  • Newbie
  • Newbie
  • User avatar
  • Posts: 8

Post 3+ Months Ago

now im totally confused.

"I Purpose to capture event on Outer Element or the wrapper of the all drum interaction Code."

I dont know what you mean by this. Like i said I am a noob at action script 3.
  • grindy
  • Newbie
  • Newbie
  • User avatar
  • Posts: 8

Post 3+ Months Ago

public function initListeners() {
stage.addEventListener(KeyboardEvent.KEY_UP, keyDownHandler);
}

private function keyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case(49):
"initListeners(key1)"
break;
case(50):
"initListeners(key2)"
break;
case(51):
"initListeners(key3)"
break;
case(52):
"initListeners(key4)"
break;
case(53):
"initListeners(key5)"
break;
case(54):
"initListeners(key6)"
break;
default:
// do nothing.
}
}

This is what i tried to do but it still doesnt work. I get these errors

Scene 1, Layer 'Actions', Frame 1, Line 1 1114: The public attribute can only be used inside a package.

Scene 1, Layer 'Actions', Frame 1, Line 5 1013: The private attribute may be used only on class property definitions.

I am not sure what they mean
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

grindy wrote:
now im totally confused.

"I Purpose to capture event on Outer Element or the wrapper of the all drum interaction Code."

I dont know what you mean by this. Like i said I am a noob at action script 3.


If you are using flash for this then it is lot simpler

Just select your first drum
In the instance panel name it 'Drum1'.
Put this code in the actions window for the Druming
Code: [ Select ]
    onClipEvent(enterFrame){
    if(Key.isDown(Key.DOWN)){
        // Put Here Your Desired Action
        }
    }
  1.     onClipEvent(enterFrame){
  2.     if(Key.isDown(Key.DOWN)){
  3.         // Put Here Your Desired Action
  4.         }
  5.     }


You need to make this for each of drum .. :)
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

grindy wrote:
public function initListeners() {
stage.addEventListener(KeyboardEvent.KEY_UP, keyDownHandler);
}

private function keyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case(49):
"initListeners(key1)"
break;
case(50):
"initListeners(key2)"
break;
case(51):
"initListeners(key3)"
break;
case(52):
"initListeners(key4)"
break;
case(53):
"initListeners(key5)"
break;
case(54):
"initListeners(key6)"
break;
default:
// do nothing.
}
}

This is what i tried to do but it still doesnt work. I get these errors

Scene 1, Layer 'Actions', Frame 1, Line 1 1114: The public attribute can only be used inside a package.

Scene 1, Layer 'Actions', Frame 1, Line 5 1013: The private attribute may be used only on class property definitions.

I am not sure what they mean


Your code is fine... now i am confused too... :|
  • grindy
  • Newbie
  • Newbie
  • User avatar
  • Posts: 8

Post 3+ Months Ago

do you mean something like this

onClipEvent(enterFrame){
if(Key.isDown(Key.DOWN)){
initListeners(); gotoAndPlay("key1");
}
}
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Yes
but without this initListeners(); because this 'onClipEvent' is already run a event for particular single drum.
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Satwant wrote:
Yes
but without this initListeners(); because this 'onClipEvent' is already itself a event for particular single drum.
  • grindy
  • Newbie
  • Newbie
  • User avatar
  • Posts: 8

Post 3+ Months Ago

well that makes two of us then. LOL

heres the actionscript i used

private function initListeners() {
stage.addEventListener(KeyboardEvent.KEY_UP, keyDownHandler);
}

private function keyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case(49):
gotoAndPlay("key1");
break;
case(50):
gotoAndPlay("key2");
break;
case(51):
gotoAndPlay("key3");
break;
case(52):
gotoAndPlay("key4");
break;
case(53):
gotoAndPlay("key5");
break;
case(54):
gotoAndPlay("key6");
break;
default:
// do nothing.
}
}


you said that was right but i still get these errors

Scene 1, Layer 'Actions', Frame 1, Line 1 1013: The private attribute may be used only on class property definitions.

Scene 1, Layer 'Actions', Frame 1, Line 5 1013: The private attribute may be used only on class property definitions.
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Ok My Last wild attempt :)
Quote:
This ActionScript Error justs means that you have used the access control modifier 'private' in the wrong spot.


So I can say that
Your Private function initListeners() is outside class scope.

Sorry if it can't help u :(

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 3 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.