AS3 Detecting Multiple Keyboard Keys Pressed - actionscript-3

Is there is a way for me to detect if say 'a' and 'b' are pressed together using AS3.
If they are I can then make a movieclip visible.
Seems simple but I can't seem to find this out.

Just use Senocular KeyObject class to detect if a key is down or not. Listen for the KeyboardEvent.KEY_DOWN event on the stage, and see if both your keys are down.
That class has a small example usage in the header comments.

Related

What is capture and normal phase in event handeling in Scene2D ?

I read the wiki but it's very confusing for me. Can someone explain it in easy language? There are few doubts I have
1. Is capture listener and normal listener are also one of the many listeners provided by scene2D like InputListener, ClickListener etc
What is the difference between target and listener actor?
No, these terms describe whether you've assigned one of the types of listeners as a capture listener or not. Capture listeners allow an actor a chance to reject an event on one of its descendents. For example, some kind of group widget can decide whether to reject presses on a button that is in it based on some criteria. I think the built-in ScrollPane class uses this to prevent buttons and sliders from being manipulated while the view is being scrolled. Most people will not have a reason to use this, as it is for custom widgets that have very particular behavior.
Target and listener actors are usually the same. The listener actor is the actor you attached the listener to, and by default it will also be the target. But you can change the target actor to something else. This is just a feature to allow you to create listeners with custom behavior. Most people will not have a use for this. I don't even think it is used by any of the built-in classes.
The complexities of the listener system were designed to get button and widgets to react to input like a traditional UI. Unless you are designing widgets with customized input behavior, you only need
ChangeListener for the UI widgets (buttons and sliders).
If you're using Scene2D for stuff other than UI (the game scene) you'll subclass InputListener to react to touches. Or maybe GestureListener.
I built a small game using Scene2D and found that it is a bit clumsy for non-UI stuff. I wouldn't do it again, personally. So I would say 99% of people making simple games should only ever use ChangeListener.
The action system is nice for tweening stuff, but you can just add actions to the stage root and react to them externally with your own non-actor classes.

Why does one of my AS3 event handlers only work once?

I am building an SWF panel with support for English and Japanese, and a button to toggle between them. The English is on frame 1, the Japanese on frame 2. On the first frame there is this relevant AS3:
btnLangToggle.addEventListener(MouseEvent.CLICK, onLangToggle);
...
this.stop();
...
function onLangToggle(e:MouseEvent):void {
if (MovieClip(root).currentFrame == 1) {
MovieClip(root).gotoAndStop(2);
} else {
MovieClip(root).gotoAndStop(1);
}
trace(MovieClip(root).currentFrameLabel);
}
I click the button and the event handler function runs fine, once. If I click it again, nothing happens. Why?
Edit: Here is what my two frames and timeline look like.
If I click it again, nothing happens. Why?
Because It's a MovieClip and they are designed for animation, not for application states. Objects on the scene in first frame isn't accessible in the second key frame.
I assume in your case you have button for toggling language with different instances of MovieClip(Different languages). In second frame apply again event listener for the Japanese version of the language button:
btnLangToggle.addEventListener(MouseEvent.CLICK, onLangToggle);
Also, please read about Document Class, It's pretty simple create application with only 2 states, like you have (with 2 frames...)
I assume you are using key frames for the button, right?
You must be sure that the button is the same on both frames. Which means you must not use key frames - instead use separate layer for the button with a single keyframe and two normal frames. This way your code will work as it's the same button.

Actionscript 3: How to have a keyboard Keystroke "Button" go to specific Frame FROM a specific Frame?

I'm Super New to Programming in general and very New to Actionscript 3. It's been a rough but fun ride.
Anyway For the work I am doing I have my animation Play up to a certain point and it asks the viewer a question, The viewer has 3 choices, and each of those choices are connected to a button on the keyboard. A B C or D.
After searching for someone with a similar problem, My solution so far is this:
stage.addEventListener(KeyboardEvent.KEY_DOWN, cNote);
function cNote(event:KeyboardEvent):void {
trace(event.keyCode);
if (event.keyCode==67) {
gotoAndPlay(21);
}
I have this repeated 4 times each for the different Keys.
This works and allows me to move the viewer to the Frame with their response, however Any time during the animation they can press the button and it will move them to the Frame of the corresponding button (in this case Frame 21). How do I set it so that the keypress will only respond and move them to Frame 21 when they are on Frame 20 for example? (I suspect it has something to do with the "stage" part in the code but I cant get it to work without it.)
I also have the audio for the animation on a separate Layer and even though the visuals change as it goes to the correct frame, the audio continues as if the key was not pressed. Is there also a way to connect the key to make it stop the current audio and then play the new audio? Or is there a better way to do this?
Sorry if this is confusing. Thank you for your help :)
It has been quite awhile since I touch flash. From what I remembered, you can click on the frame at your movieclip, insert a keyframe by pressing f6, and press f9 to insert the listener at that frame and that frame only, after an input has been pressed, you remove it. for example:
if (event.keyCode==67) {
gotoAndPlay(21);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, cNote);
}
For the audios, I usually plays them using code, but I'm quite sure there's a way to drag and drop the audio from library to the movieclip. so I can do sound1.play(), when a button is pressed, I can do sound1.close() and sound2.play()
For more reference:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html

keyboard input to movieClip actionScript 3.0

I have two alternatives to move a space ship movieclip.
First one is adding eventListener(keybordEvent) check which button is pressed and act according to it.
The second one is again,adding an eventListener(keyboardEvent) and using 4 boolean varaibles,leftArrow,rightArrow,upArrow and downArrow. keep track of pushed buttons and use eventListener(Event.ENTER_FRAME) and move movieClip in everyframe using these bool variables)
Which one is better or at least does any of them have an advantages ?
I suggest the second option because you want to handle movement in the game loop and not in the key event.
I would use the Boolean method as the other method (depending how you implement it) could react to the key repeat that happens when you press a key down - it triggers once, pauses, then continues to fire.
EDIT
..and, I agree, also for the best practices reason Nambew mentions!

How to make an object be ignored and letting mouse evnets pass through?

Hallo, I been having this problem for a while and I have no idea how to solve it.
I have a flash game (very much like a normal memory game) that has a lot of Movieclips in it that has MouseEvents attached to them. But, when I add a bitmap over the stage (used for covering lots of unwanted things and has to be there) that is the full size of the screen non of my events are fired anymore. The reason is that the overlay bitmap is stealing all of the events.
How can I stop this behavior? Is there a way of letting the events pass through the overlay object? Or for the overlay object to be ignored when it comes to events?
Thanks.
Assuming your overlay is stored in a variable m_overlay, then
m_overlay.mouseEnabled = false;
However you said it is used for "covering lots of unwanted things" so perhaps we need more information on what you are trying to achieve?
I've solved this in the past by creating "proxy" object to capture mouse clicks. The MCs under the bitmap aren't going to receive events.