Triggering button event (or atleast appear to) via keyboard press AS3 - actionscript-3

Attempting to make a piano in AS3 and so far I have 2 methods of triggering a note, 1 via keyboard and 1 via mouse-click. When I click the key, it changes colour to indicate that the key has been pressed. Unfortunately , I can't find a way for the keyboard event to trigger the button press, is it possible and how would I do it?
It doesn't need to be an actual button press, it can instead just appear to be.
EDIT
The button is a button object, using the GotoAndStop causes the following error code:
"project.as, Line 13, Column 7 1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:SimpleButton."
Thanks in advance.

The easiest way is to extract the piano key press logic into its own function, and then call that function from two events.
function pressedMyPianoKey(){
// Do something like pianoKey.gotoAndStop(2) to show a new graphic
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
function keyDown(event:KeyboardEvent):void
{
pressedMyPianoKey();
}
pianoKey.addEventListener(MouseEvent.CLICK, click);
function click(e:MouseEvent){
pressedMyPianoKey();
}

Related

adobe flash show/hide different movieclips in different frames with one button Error #1009

I created a button to a layer and I am trying to show movieclip com7 in frame 1 when I click the button named quest. Then, I would like to show a different movieclip com9 in frame 2. I put the movieclips in another layer each one in frames1 and 2.
In frame1 the code is:
quest.visible=true;
com7.visible=false;
quest.addEventListener(MouseEvent.CLICK, q7_clicked);
function q7_clicked(event:MouseEvent):void
{
if (com7.visible==false)
{com7.visible=true
}
else
{
com7.visible=false;
}
}
in frame 2:
quest.visible=true;
com9.visible=false;
quest.addEventListener(MouseEvent.CLICK, q9_clicked);
function q9_clicked(event:MouseEvent):void
{
if (com9.visible==false)
{com9.visible=true
}
else
{
com9.visible=false;
}
}
Flash creates the swf without errors but when I click the button in frame2 there is a TypeError:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at meli_fla::MainTimeline/q7_clicked()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at meli_fla::MainTimeline/q9_clicked()
The issue is that when you add event listeners on the timeline, those listeners do not automatically go away when you change frames (no code does).
So on frame 1, you just have the one listener and it probably works fine.
On frame 2, you create a new listener, but the one from before on frame 1 is also still hanging around, so when you click the quest button, it actually calls q7_clicked and q9_clicked. No matter what frame you are one, at this point clicking the button you added the listeners to will always call both functions.
Your error, is because the objects you're referencing (com9, com7) are likely not around on both frames you are visiting (confirmed from you comments on the question).
To remedy the problem, you need to remove the appropriate event listener when you move to a new frame.
So, wherever in your code you do nextFrame(); or gotoAndStop(2); or however you move the user to another frame, at that time remove the listener on the button:
quest.removeEventListener(MouseEvent.CLICK, q7_clicked);
gotoAndStop(2);
Or, if returning to frame 1:
quest.removeEventListener(MouseEvent.CLICK, q9_clicked);
gotoAndStop(1);

Flash CS6 Button Not Working Despite No Compiler/Output Errors

I'm using Flash CS6, AS3 to create buttons for my project. Below is my code:
Intro_btn.addEventListener(MouseEvent.MOUSE_DOWN, Intro_func);
function Intro_func(event:MouseEvent):void {
gotoAndStop("Intro");
}
No errors appear when I run it on the output and compiler panel, and the same happens when I run it through the debugger. Also, I have used the exact same code for five other buttons, and they have no problems working. Please can someone tell me what is wrong with my code???
I would need to know more info of your code and the type of buttons you're using, however I would try this steps:
1.- add a Button component from the component inspector, set its instance name, and add that same function to the Click event:
newButton.addEventListener(MouseEvent.CLICK, Intro_func);
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- run your app and test if the action is executed with this new button. If this test pass, then your problem is your button.
check this on your button:
check the base class of the button (be sure its a MovieClip or a Button class)
check if your button is enabled/disabled.
check if your button or its children has mouseEnabled and mouseChildren set as
true.
check there is no other object on top of the button either in your artboard or added by code at runtime.
I finally would suggest you to set useHandCursor = true; this will replace your mouse pointer arrow to a hand when you move over your button (this is just to test that your button is actually interacting with mouse).
hope this helps.
sorry for the delay, But I've found your solution, It's easier than you thought: your frame is delayed.
your Intro_btn script is on frame 10, however in frame one you're telling it to go to frame 9 (the "Erhu" frame label) so the actions to setup the event listener are never being called.
Also keep in mind the following:
objects exists only in their frames life time, if you have an object from frame 5 to 10, it will only exist in there, so moving to a previous or later frame (e.g. frames 4 and 11) will internally delete the object in memory along with their asociated actions, in english:
you placed the button in frame 10 and added its MOUSE_DOWN listener however, if you go back to frame 0, since the button doesn't exists in this frame it will be deleted from memory along with its listeners, so if you go from frame 0 to any other frame different than 10, your button will never have its listeners associated.
So my suggestion:
1.- add your function into frame 0:
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- create a new layer. In this layer add a keyframe at the same position where your Intro_btn is(frame 10), and fill the rest of the timeline of this layer with empty frames (no keyframes), finally in the same layer add in frame 10 your listener Intro_btn.addEventListener. this way, the action is available for every subsecuent frame from frame 10.
hope this solves your problem.

Navigation from frame 1 to frame 2 renders event listeners non-functional in flash/actionscript3

I am new to actionscript and flash, and I have been unable to find a good explanation for why I cannot get this pretty basic code to work. I am using flash cc with actionscript 3. I have two frames on my timeline, one labeled "startFrame" and another labeled "newFrame". In "startFrame", I have a button with the instance name "btnStart". There is an event listener that listens for a mouse click on btnStart and then calls a function that goes to the next frame. Here is the code for "startFrame":
stop();
//When "btnStart" button is clicked, call the beginSession function.
btnStart.addEventListener(MouseEvent.CLICK, beginSession);
function beginSession(event:MouseEvent):void
{
//Remove event listener??
//btnStart.removeEventListener(MouseEvent.CLICK, beginSession);
//Go to newFrame.
gotoAndStop("newFrame");
}
I thought initially I should remove the event listener in the function after it is called, but it doesn't seem to work whether I remove the event listener or not. Here I have it commented out. In the "newFrame" frame, I have an event listener that listens for a key press and then calls a function that returns the key and character codes for that given key. That code is here:
stop();
//show that newFrame has been reached.
trace("newFrame has been reached");
//Add event listener for a key press to the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
//function that returns the key pressed and the character code.
function reportKeyDown(event:KeyboardEvent):void
{
trace("Key Pressed: " + String.fromCharCode(event.charCode) + " (character code: " + event.keyCode + ")");
}
When I test the movie, the button press works and "newFrame" is reached (the "newFrame has been reached" trace is found in the output). But it does not seem that the event listener for the key press is working. However, if I create a project that only has "newFrame" by itself (no other frames in the timeline), it works just fine when I test the movie. Only after I add in the startFrame and the button navigation do the event listeners seem to no longer hear the key press event in the second frame. Also, I am not getting any compile errors. What am I missing here?
Disable keyboard shortcuts under view while testing the application.

How to capture all keyboard input to a custom element?

Here is my problem: I've got a swf loaed inside my loader an in that swf I have a keylistener:
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.__onKeyDown, false, int.MAX_VALUE);
Now I'm adding a TextInput to this stage and I would like this input to catch all keyboard events while I'm focusing it. Is it possible to do so that native __onKeyDown will not fire until my TextInput has lost focus?
Thank you for your answers and sorry for my bad english.
You could give your listener higher priority (which you are), and stopAllPropogation in your handler. I've never tried this with an embedded swf so if it doesn't work right away, you could also try listening to the event in the capture phase (third parameter in addEventListener).
function __onKeyDown(e:KeyboardEvent):void {
e.stopImmediatePropagation();
//rest of you handler code here
}

How to detect if the delete key was pressed in Actionscript 3?

How do I determine if the delete key was pressed using actionscript?
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
...
function onKeyUp(event:KeyboardEvent):void
{
trace(event.keyCode);
}
The above code yields no value when delete, backspace, enter, and other command keys are pressed. However, arrow keys do yield values.
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
....
function onKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.DELETE) {
.....
}
}
it's workin nice...
But if you test movie from Flash, it will not work, so export to swf and test....
Just guessing you are using the TEXT_INPUT event, this doesn't work for delete and backspace. To catch those ones you can add an eventListener on the stage and listen to a KeyboardEvent.
Code will work fine if the display object that you attached the listener is in focus. For global listening, as Theo said, you have to attach the listener to the stage. Accessing stage from an object that's not yet added to the display list will result in null error. Do it in the ADDED_TO_STAGE event handler to be safe.
Old thread, but if anyone gets this far: in the Flash Player inside the IDE, these keys are associated with shortcuts. When you test your movie, choose Control>disable keyboard shortcuts in the player and you'll get the events back.