Linking Enter key to button in Flash game - actionscript-3

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
}

Related

Rich Editable Text not dispatching Enter key event

I'm trying to catch when a user presses a "enter" key in a rich editable text field. It wasn't dispatching KeyboardEvent.KEY_DOWN so I found that it dispatches an "enter" key event. That seemed more appropriate but that is not getting dispatched either.
Here is my code:
editableRichTextField.addEventListener(FlexEvent.ENTER, commitTextEditorValues, false, 0, true);
Am I missing something?
NB: This is with Apache Flex 4.14.
I knocked up a sample application for this and it seemed that the RichEditableText does dispatch KeyboardEvent.KEY_DOWN. How are you catching it? The following code:
private function CCH():void
{
richEditableText.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
}
private function KeyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ENTER)
{
Alert.show("Enter Key Pressed");
}
}
should trigger an alert (where the CCH method is the creation complete handler for the component/application.
You have to set multiline to false for the event to be dispatched. This is not helpful if you need to support multiple lines so you have to use #DaJobat's answer and listen on key down.
editableRichTextField.multiline = false;

AS3 Key down listener not working after returning to frame

I have two frames, one labeled "mainFrame" and another labeled "secondFrame". When the program starts, the user clicks a button called start and that sends the user to "secondFrame". Once the user is on second frame, the KeyCode of whatever key they have pressed is traced and if the key pressed has the KeyCode of 68, the program returns to "mainFrame". This works fine, the problem is then when the user clicks the button again and returns to the "secondFrame" at which point the KeyCode does not trace.
Here is the code on the mainFrame:
stop();
start.addEventListener(MouseEvent.CLICK, startGame);
function startGame(e:MouseEvent):void
{
gotoAndStop("secondFrame");
}
And the code on the secondFrame:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
function keyDownCheck(event:KeyboardEvent):void
{
trace(event.keyCode);
if(event.keyCode == 68)
{
gotoAndStop("mainFrame");
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
}
}
I'm sure I'm doing something stupidly obvious that is preventing this from working, but I just can't figure out what.
Try to add this.setFocus() to the second frame's code.
I've found the answer! Well, Zavr and Vesper actually said it xD The focus was being set out of the application, so stage.focus() = this worked. I don't know what I did that caused the errors when I tried it the first time but it works now xD Thanks all!
Change second frame code to:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
function keyDownCheck(event:KeyboardEvent):void
{
trace(event.keyCode);
if(event.keyCode == 68)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
gotoAndStop("mainFrame");
}
}

as3 how to perfom different actions if button is clicked or not

I am a complete n00b.
I'm working on a game. Basically, I have to perform no action if a button is pressed on a certain keyframe, but if it's not clicked, I need the scene to proceed until a certain point and then go to another frame.
In my stupid head I thought: if I can create an empty global variable I could input "something" when the button is clicked on my frame and check on a latter frame "if globalvar == "something": nothing happens, else: gotoAndPlay(where I need)"
Explanatory image:
Apparently that's not how AS3 works. So what can I do?
import flash.events.MouseEvent;
var pressed = "no";
function work(event:MouseEvent):void
{
pedal.visible = false;
cursor_mc.visible = false;
pressed = "yes"
}
pedal.addEventListener(MouseEvent.CLICK, work);
This is what I've got when the action needs to be performed.
Then I would put (I've never written an if statement in as3)
if (pressed == "no") {
gotoAndPlay(some other frame);
}
This would be put on a complete different frame AND LAYER from the previous so that the part in between is still played.
You got to add a new event, and ENTER_FRAME event to which will fire a function on every frame. There you could check whether the button is pressed. For example:
stage.addEventListener(Event.ENTER_FRAME, updateFunction);
function updateFunction(e:Event){
if(pressed == "no"){
if(currentFrame == frame number when you want to check){
gotoAndPlay(some other frame);
}
}
}

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.

Not catching on key up event

I have a flash component TextInput within a movieclip, added to stage, through as3.
The moment this mc is added to stage, it assigns keyboard event (key down and key up events), to the main stage (main app stage), and set main stage focus to main stage.
So far so good.
The problem is, when I type ENTER, I catch KEY_DOWN, but when I release key ENTER, my KEY_UP event is uncatched by my function assigned to that event.
Any body knows why?
It only works if I click the flash player.
(Code provided in comment)
private function initAdded(e:Event){
_main.stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
_main.stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
MovieClip(_main).setStageFocus()
}
private function checkKeysDown(e:KeyboardEvent):void {
if (e.keyCode == 13) {
trace('enter down')
}
}
private function checkKeysUp(e:KeyboardEvent):void {
if(e.keyCode == 13){
trace('enter up')
}
}
I'm going to guess that the KEY_UP event is being handled by the text area you are typing in, and it doesn't want to share, so it kills the event. Try:
_main.stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp, true);
This causes your event listener to catch the event in the capture phase, before the other event listener cancels propagation of the event.