How to click buttons that has sound using keyboard - actionscript-3

I've made a simple piano in adobe animate, its made of white and black rectangular that are buttons ,each button has a musical note when you click on it, it plays the(the audio file is set on down keyframe) note, i would like to be able to play these notes by pressing on certain keys on the keyboard, any code for that?
Thanks very much!

You can achieve this with the help of Keyboard events.
Each key has a key code associated with it. Example setup is below:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
function keyDownHandler(event:KeyboardEvent) :void
{
trace("keyDownHandler: " + event.keyCode); //# is key-code of key pressed down
if (event.keyCode == XXX) //# where XXX is the key-code of pressed key
{
//# put your code here to play the sound for that key
}
}
function keyUpHandler(event:KeyboardEvent) :void
{
trace("keyUpHandler: " + event.keyCode); //# is key-code of key released
}

Related

Linking Enter key to button in Flash game

I'm building a topo-trainer in Adobe Flash with actionscript 3.0.
I'm almost done now. I made this "quit" button at the end of the game.
You can click it to quit the game now, but I would like the Enter key to react to the quit-button as well. I really tried looking it up on the internet and on stackoverflow, but either my searching skills are not advanced enough or there hasn't been a person with the same problem yet.
I hope somebody knows how to couple the Enter button to a button in Flash. There is no EditText involved.
Thanks for your help,
Justin
Let's say you have some code that runs when your games ends:
myQuitBtn.addEventListener(MouseEvent.CLICK, quit, false, 0, true);
function quit(e:MouseEvent):void {
//do something, we quit the game
}
You could easily listen for a key event by changing it to the following:
myQuitBtn.addEventListener(MouseEvent.CLICK, quit, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);
function keyUpHandler(e:KeyboardEvent):void {
//Check if the key pressed was the enter key
if(e.keyCode === 13){ //could also do `e.keyCode === Keyboard.ENTER`
quit();
}
}
//make the event argument optional so you can call this method directly and with a mouse listener
function quit(e:Event = null):void {
//remove the key listener
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false);
//do something, we quit the game
}

AS3 keyboard event not updating when going back to frame

I am making an application about the atmosphere, and am trying to make a movie clip animate (move) up and down to different section when the arrow keys are pressed. It's almost working fine. However, if for instance I was on the section 'troposphere', pressing the up arrow key would take me up to 'stratosphere' and then the down arrow key back to 'troposphere'. The up arrow key would then take me back up to 'stratosphere', but if I press the up arrow key to then go up to 'mesosphere' it repeats the animation from 'troposphere' to 'stratosphere'.
I explained that the best I can, sorry if it's unclear. I uploaded a screencap to youtube that I think would help make it clear.
Here's my code for troposphere (the start) on frame 40. The animation to stratosphere starts on 41.
stop();
stage.addEventListener(KeyboardEvent.KEY_UP, goupStrat);
function goupStrat(e:KeyboardEvent):void {
if(e.keyCode==Keyboard.UP) {
gotoAndPlay (41)
}
}
And here's my code for stratosphere
stop();
stage.addEventListener(KeyboardEvent.KEY_UP, goupMes);
stage.addEventListener(KeyboardEvent.KEY_UP, godownTropos);
function goupMes(e:KeyboardEvent):void {
if(e.keyCode==Keyboard.UP) {
gotoAndPlay (64);
}
}
function godownTropos(e:KeyboardEvent):void {
if(e.keyCode==Keyboard.DOWN) {
gotoAndPlay (211);
}
}
and so on.
Here's what my timeline looks like
I have tried searching for a solution but I'm not sure exactly what it is I should be searching for.

prevent next scene frame on movieclip mouseclick

I have a scene with a few objects as movieclips which can be clicked one at a time.
What happens is that I'm able to click every object and on click the scene switches to the next frame.
How do I change that?
Basically I have a key and a door, both movieclips.
You can collect the key, it disappears and after that you are able to click the door to open it.
What actually happens is you are both able to click the key and the door.
When you click the key, it's working as intended, but when you click the door, the key still disappears. This is much more annoying with more than 2 objects.
code for the key:
addEventListener(MouseEvent.CLICK, CollectKey);
function CollectKey(event: MouseEvent): void
{
this.visible = false;
// door
MovieClip(root).door.addEventListener(MouseEvent.CLICK, MovieClip(root).FinishGame);
}
code for the door:
stop();
function FinishGame(event: MouseEvent): void
{
if(MovieClip(root).currentFrame == 4)
{
nextFrame();
}
}
http://www.wuala.com/sollniss/stuff/Untitled-2.swf/
http://www.wuala.com/sollniss/stuff/Untitled-2.fla/
EDIT
After looking at your .fla, I can see your issue:
On your first frame, you have the following script:
stop();
addEventListener(MouseEvent.CLICK, StartGame);
function StartGame(event: MouseEvent): void
{
nextFrame();
}
You likely aren't aware that the mouse click listener you add there, doesn't go away until you tell it to (even if the frame changes). That's why every click calls next frame.
To remedy this, simply remove the listening before you move on to the next frame:
function StartGame(event: MouseEvent): void
{
removeEventListener(MouseEvent.CLICK, StartGame);
nextFrame();
}
And, may be that only visible false is not enough, you also need to set enabled = false and mouseEnabled = false for the key element, because without it, it will keep hearing the click event.

Flash AS3 making it so whenever a button is clicked on screen is like pressing a specific keyboard key

Hello I am working on a game in flash, in the mobile version of this app I am trying to make it so in a mousedown event on a button it will be equal to the Left, Right keycodes etc. being active?
Is there a way in AS3 for a keyboard key to be activated by other means such as mouse clicks?
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, leftKeyPress)
function leftKeyPress(e:MouseEvent){
// Left Key is pressed
Keyboard.LEFT;
trace("trace statement");
}
This code doesent seem to work
EDIT: also this is important this class and the class that uses the keyboard(The player) are in two seperate classes, so Im trying to make it so that when this is click it is recognized as a key press on a keyboard
Sorry but It is all wrong. You can't trigger keyboard events like that. That is the way of capturing events. And your function is also a MouseEvent. Should be keyboard event. I Opened Flash after one year. Anyway. AFAI understand you are trying to disable keyboard until mouse click somewhere.
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, leftKeyPress);
var keyEnabled:Boolean = false;
function leftKeyPress(e:MouseEvent){
keyEnabled = true;
}
key.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(e:KeyboardEvent):void{
if(keyPressed){
//do whatever here
}
}
You can manually dispatch a keyboard event; however, is not the best design pattern:
dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN,
true,
false,
Keyboard.LEFT,
Keyboard.LEFT));
It would be better to establish two handlers:
addEventListener(KeyboardEvent.KEY_DOWN, moveKeyHandler);
addEventListener(MouseEvent.CLICK, moveMouseHandler);
From each handler, call a function that abstracts movement function from the handlers:
protected function moveKeyHandler(event:KeyboardEvent):void
{
/* switch statement for key */
moveLeft();
}
protected function moveMouseHandler(event:MouseEvent):void
{
/* switch statement for button target */
moveLeft();
}
protected function moveLeft():void
{
}

FLASH actionscript 3.0 problems

I want to ask about this problem. I have a movieclip (instance name : char). Inside it I have 2 frames. The first frame contains a movieclip (it does nothing I forgot why I even bother to make it into movieclip). This first frame has frame label "still".
The second frame also contains a movieclip, inside it contains 12 frame. This second first frame has frame label "run"
This is my code
char.gotoAndStop(char.still);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keysUp);
function keysDown(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT)
{
char.gotoAndStop("run");
this. char.scaleX = 1;
}
}
function keysUp(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT)
{
char.gotoAndStop("still");
}
}
The problem is, when I press RIGHT ARROW button, It moves, but the movieclip (with frame name "run") can't loop or even play complete from frame 1-12, it only plays from frame 1-9 then stop (not go to frame 10 or even loop)
is there something wrong with my code?
Have a look at how often the KEY_DOWN-Event is actually fired. For example by just tracing.
function keysDown(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT)
{
trace("pressed");
char.gotoAndStop("run");
this. char.scaleX = 1;
}
}
You will realize that the event is thrown WHILE the Key is pressed, not only once. You actually call the gotoAndStop("run") repeatedly, which makes your animation mc restart all the time.