AS3: Key Press clicks the tower - actionscript-3

I am almost done with an Engineering Internship where a group and I were assigned a project involving hardware and programming. Our project was an old-style arcade box Tower Defense Game with a theme of Stopping Global Warming.
After following a nicely made tutorial, we have completed our game except for one thing: in the tutorial's game you select a tower by clicking a button with the mouse, however for our purposes we need it so the user presses a keyboard button to select the tower (we already got it so when you press a button on the arcade box and it presses a key).
Below is the code section that I believe contains the code that needs to be changed. I have tried fiddling with KeyboardEvent.KEY_DOWN, however to no avail.
In case everything above made no sense, I need to make the event listener listen for the "w" key to be pressed, and then select the Fire Tower.
setupGame();
// Initialise the UI event listeners.
mcGameUI.btnBuildFireTower.addEventListener(KeyboardEvent.KEY_DOWN, clickTowerFire);
mcGameUI.btnBuildFireTower.addEventListener(MouseEvent.ROLL_OVER, showTowerFireHelp);
mcGameUI.btnBuildFireTower.addEventListener(MouseEvent.ROLL_OUT, clearHelp);
Thank you!!!!

Keyboard events need to be listened for on an object which has focus. Fortunately, the stage always has focus, so it's a good idea to attach your keyboard event listeners there, e.g.
stage.addEventListener(KeyboardEvent.KEY_DOWN, clickTowerFire);
If you want to add actions based on a specific key press, you can use the keyCode property that is attached to the KeyboardEvent:
function clickTowerFire(event:KeyboardEvent):void
{
if(event.keyCode === 87) // W
{
// Your existing code here.
}
}

Related

libgdx DragListener cancellation

Trying to use libgdx's drag listener, I realized that the #cancel method would only be called AFTER #dragStop even if ESC is pressed before the mouse button release, which is practically preventing me from cancelling the action meanwhile.
So how am I supposed to deal with my selection cancellation? With an additional keyboard input listener, perhaps?
So what is the functional point with the cancel method?
Thanks for any lights here.
For now, I'll just be checking up Gdx.input.isKeyPressed(Input.Keys.ESCAPE) at render time, not using #cancel anymore.

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

How to detect keydown up and down arrow in same time case Page Dance

hello I work at project to build dance game with AS 3.0 control by Page Dance.
I got problem when player stomp two button in same time.
How to detect it?
Thank you.
There is a lot of code in that pastebin, but the problem should not lie in your onKeys function.
What does this code do for you, when you press up and down at once?
function keyPressed(e:KeyboardEvent) {
trace("Key event: " + e.keyCode);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
It returns two keycodes instantly for me. If it doesn't for you, your keyboard might not support for those two buttons to be pressed at once (most keyboards have a certain number of keys that are cross-wired to save cost, making it impossible to press them at once).

ActionScript / AIR - One Button Limit (Exclusive Touch) For Mobile Devices?

Two years ago, when I was developing an application for the iPhone, I used the following built-in system method on all of my buttons:
[button setExclusiveTouch:YES];
Essentially, if you had many buttons on screen, this method insured that the application wouldn't be permitted do crazy things when several button events firing at the same time as any new button press would cancel all others.
problematic: ButtonA and ButtonB are available. Each button has a mouse up event which fire a specific animated (tweened) reorganization/layout of the UI. If both button's events are fired at the same time, their events will likely conflict, causing a strange new layout, perhaps a runtime error.
solution: Application buttons cancel any current pending mouse up events when said button enters mouse down.
private function mouseDownEventHandler(evt:MouseEvent):void
{
//if other buttons are currently in a mouse down state ready to fire
//a mouse up event, cancel them all here.
}
It's simple to manually handle this if there are only a few buttons on stage, but managing buttons becomes more and more complicated / bug-prone if there are several / many buttons available.
Is there a convenience method available in AIR specifically for this functionality?
I'm not aware of such thing.
I guess your best bet would be creating your own Button class where you handle mouse down, set a static flag and prevent reaction if that flag has been already set up by other instance of the same class.
In pseudo-code:
class MyButton
{
private static var pressed : Boolean = false;
function mouseDown(evt : MouseEvent)
{
if(!pressed)
{
pressed = true;
// Do your thing
}
}
}
Just remember to set pressed to false on mouse up and you should be good to go.
HTH,
J

Key press issue in ActionScript 3

I'm creating a DDR-like game for an assignment and my keyboard seems to respond and trace it, but only if I press on the screen with the mouse first.
How can I get rid of that and just have it respond right away?
right_mc is the arrow that's moving
ArrowRight_mc is the arrow at the top
perfect_mc should pop up briefly
and so should a glowing arrow where it hits.
Here is what I have so far:
if(rightDown){
trace("right arrow");
if(right_mc){
if(right_mc.y >= ArrowRight_mc.y){
perfect_mc.visible = true;
glowRight_mc.visible = true;
}
}
}
This has been a long standing issue for Flash developers. Flash needs keyboard focus before it can detect keyboard events.
The problem is that the browser does not give focus to the SWF until the user clicks somewhere inside the SWF. This does make sense though. I don't want the web page I am on to lose focus just because there is a flash movie embedded somewhere. This is a security feature, to stop things like Flash banner ads being silent key loggers. However there are some instances that it makes sense to force the focus e.g. a Flash game where its the only thing on the HTML page.
Usually the best thing to do is have start menu screen with a "play" button. This forces the user to click on the SWF without even knowing about this "focus issue".
There is more info at the Adobe Technote - Giving keyboard focus to an embedded Flash movie.
***EDIT****
Whether the Flash has focus or not, only affects keyboard events. It will not affect code from running, movieclips from playing, or sounds/video from playing.
The issue here is that the embedded SWF file does not have focus when the screen first loads. You can assign focus to it using JavaScript but in my experience, this does not always work 100% of the time because of variations in the way browsers interpret JS. What a lot of people do is have a big START button following the load of the game so that players have to click on the SWF to begin playing. Some sites, even use JS to detect when the game has lost focus and will pause the game and alert the user.
I suppose I haven't exactly answered your question because I'm not that great at JavaScript but I hope this points you in the right direction.
In response to your comment...
I'm unclear on something. If you have to click to start the song then you've already clicked on the SWF and you should be getting keyboard events, right? So if you're having to click to start then click again, maybe you need to make sure your mouse listener is at the root of your display list.
// in your document class / main AS file...
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Or perhaps you need to poll for input during an EnterFrame loop rather than listen for key events.
I know what you mean. You have a specific object on the stage that needs focus before keyboard events will trigger. I'm having the same issues with a game here. A sprite needs to move with the keyboard arrows but the container needs focus so it will respond. I'm just setting the object I want to move to be tabEnabled as my requirements are only for accessibility purposes so tabbing to the object first will give it keyboard control.