AS3 Disabling keyboard text input - actionscript-3

I'm making a virtual keyboard, and I want to find a good way to disable the actual keyboard.
For whatever reason my overall code doesnt work unless my text fields are input based.
I tried something simple with this, but it only works when its out of scope..
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
function onKeyEvent(e:KeyboardEvent):void
{
var character:String = String.fromCharCode(e.charCode);
if (e.keyCode == 65)
{
trace(character);
}
else if (e.keyCode == 66)
{
trace(character);
}
else if (e.keyCode == 67)
{
trace(character);
}
else if (e.keyCode == 68)
{
trace(character);
}
else if (e.keyCode == 69)
{
trace(character);
}
else if (e.keyCode == 70)
{
trace(character);
}
else if (e.keyCode == 71)
{
trace(character);
}
else if (e.keyCode == 72)
{
trace(character);
}
else if (e.keyCode == 66)
{
trace(character);
}
}

Try
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
e.preventDefault();
e.stopPropagation();
}

Related

ActionScript 3 use multiple onKeyDown/onKeyUP

Im trying to get a two plyer version of Snake running but i am having trouble getting the second snake to work, player 1 plays with w,a,s and d while player 2 uses the arrow keys. Player 1 with w,a,s and d is working player 2 with the arrows does not.
The code looks like this:
function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.A){
SnakeDirection = "left";
}else if (event.keyCode == Keyboard.D) {
SnakeDirection = "right";
}else if (event.keyCode == Keyboard.W) {
SnakeDirection = "up";
}else if (event.keyCode == Keyboard.S) {
SnakeDirection = "down";
}
}
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.A) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.D) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.W ) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.S){
SnakeDirection = "";
}
}
/* function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT){
Snake2Direction = "left";
}else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
}else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
}else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.LEFT) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.UP ) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.DOWN){
Snake2Direction = "";
}
}*/
The comments are there because it breaks the game, from my understanding the errors are because i can only use one onKeyUp/Down. If that is the case, is there another way?
Thanks!
Why do you want to use 2 functions for each event? Wouldn't it be much simpler if you just did the logic for the second player in the same function that also handles the logic of the first player?
Like this:
function onKeyDown(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A) {
SnakeDirection = "left";
} else if (event.keyCode == Keyboard.D) {
SnakeDirection = "right";
} else if (event.keyCode == Keyboard.W) {
SnakeDirection = "up";
} else if (event.keyCode == Keyboard.S) {
SnakeDirection = "down";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT) {
Snake2Direction = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
} else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
} else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D || event.keyCode == Keyboard.W || event.keyCode == Keyboard.S) {
SnakeDirection = "";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT ||event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
Snake2Direction = "";
}
}
Keep in mind, you want to keep the if-blocks for the players separated, so that you can get multiple inputs at the same time.
Also as a side note, the else-if blocks in your onKeyUp function are unnecessary. They all do the same thing anyway.

Single Action with Button Click

I am currently building a small version of a fighting game in flash, but I don't want the character to continuously hit etc. by just holding down a key, but rather to have the key be pressed everytime. This is what I currently have:
function moveChar(event:Event):void{
if(rightKeyDown && !hitting && !combo){
hitting = true;
gotoAndPlay("basic_punch");
kickbag.gotoAndPlay("hit1");
countHits++;
}
if(downKeyDown && !hitting && !combo){
hitting = true;
gotoAndPlay("basic_kick");
kickbag.gotoAndPlay("hit1");
countHits++;
}
if(downKeyDown && combo){
gotoAndPlay("kick_combo1");
kickbag.gotoAndPlay("hit2");
kickbag.stop();
}
if(rightKeyDown && combo){
gotoAndPlay("punch_combo");
kickbag.gotoAndPlay("hit2");
kickbag.stop();
}
}
function checkKeysDown(event:KeyboardEvent):void{
if(countHits == 2) bar.gotoAndStop("bar2");
if(countHits == 6) bar.gotoAndStop("bar3");
if(countHits == 10) {
bar.gotoAndStop("bar4");
combo = true;
gotoAndPlay("combo_stand");
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
}
}
function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = false;
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = false;
}
}
But as I explained this allows for the buttons to be held down.
Create onKeyDown and onKeyUp listeners. In the onKeyDown listener kill the onKeyDown listener, perform action, and init the onKeyUpListener. In the onKeyUp listener, kill the onKeyUp listener and re-init the onKeyDown listener.
I think #Ribs is on the right track, but given your current code the answer seems simple.
Remove the Event.ENTER_FRAME from moveChar() and change the function definition line so it looks like this:
function moveChar():void //e:Event removed from parameters
Then in your checkKeysDown function, call the moveChar() for the key pressed (your moveChar() evaluates the booleans anyway):
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
moveChar();
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
moveChar();
}
Now you can alter the if statements above to make it a bit more concrete:
if ( !downKeyDown && !rightKeyDown ) {
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
moveChar();
}
else if(!downKeyDown && !rightKeyDown && (event.keyCode == 40 || event.keyCode == 83)){
downKeyDown = true;
moveChar();
}
}
This is basically just handling the down or right key pressed one at a time, and ignoring everything else while those are running. It could also be assume that you might not want to do anything while down or right keys are pressed, therefore you can just do this:
function checkKeysDown(event:KeyboardEvent):void{
if ( downKeyDown || rightKeyDown ) return; //do nothing, we are pressing a key
if(countHits == 2) bar.gotoAndStop("bar2");
if(countHits == 6) bar.gotoAndStop("bar3");*/
if(countHits == 10) {
bar.gotoAndStop("bar4");
combo = true;
gotoAndPlay("combo_stand");
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
moveChar();
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
moveChar();
}
}
This will return in the checkKeyDown function if we are currently pressing the down or right key. I'm assuming this is probably the desired effect you are going for so your counters do not increment each KEY_DOWN registration. As you can see you have some options and there are multiple ways of doing this. Hope this provides some guidance.
I too have been working on a platformer flash game, and here's how I accomplished a clean attack code: Declare an "attack" Boolean to help decide whether or not the character can or cannot attack. You can prevent the character from continuously attacking by creating an enter frame event listener that checks what frame the character is in during his attack. If the character play-head is at the last frame of the animation than simply set the attack Boolean to false.

fluidly dealing with multiple keypresses in ActionScript3

I'm currently building an engine for a platformer game at the moment, but I've noticed that ActionScript3 is having difficulty in keeping fluid when multiple keypresses are in use. For example;
function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
if (isTouchingGround()) {
isJumping = true;
yv = -100;
}
} else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
if (xv == 0) {
player.gotoAndPlay(275);
}
} else if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
if (xv == 0) {
xv = -24;
} else if (xv != -120) {
xv-=2;
}
} else if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
if (xv == 0) {
xv = 24;
} else if (xv != 120) {
xv+=2;
}
}
}
So, as listed above, using the UP (or W, or Space) key triggers the player to jump (seperate onframe event handler handles gravity etc). Using the RIGHT (or D..) key triggers increases the player acceleration, which is again applied to the player in a seperate onframe event handler.
Everything works fine by itself - but the problem arises when multiple keystrokes are used. If a player starts to move to the right, and hits jump, he will cease accelerating. At the same time, he will not decelerate, as instructed in the Keyboard.UP method. Instead, he will maintain constant at his current rate, until the RIGHT key is hit again.
In short, it is as though Actionscript begins ignoring both the keyboard.down and keyboard.up methods for the RIGHT or LEFT movement keys, until they are no longer being pressed. This obviously causes for some very rigid gameplay - is there any solution anyone would be willing to share with me on this?
Your problem lies in the fact that your if conditionals are followed by if else conditionals. Drop the else and just have the if conditionals. Basically if the user holds down space then none of the other if conditionals are going to be tested since space is being held down and it's the first if statement. So just drop the else off of the if's. Just remove the if else conditionals that are testing keystrokes, not the conditionals inside of the if statements that deal with keystrokes.
Here is what your code should look like:
function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
if (isTouchingGround()) {
isJumping = true;
yv = -100;
}
}
if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
if (xv == 0) {
player.gotoAndPlay(275);
}
}
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
if (xv == 0) {
xv = -24;
} else if (xv != -120) {
xv-=2;
}
}
if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
if (xv == 0) {
xv = 24;
} else if (xv != 120) {
xv+=2;
}
}
}
Something else you may notice is that when the UP key and RIGHT key are both being held down, the computer seems to freeze keyboard input, however when the W key and D key are being held down you can still press other keys and the computer will register their input. The answer to that question is here.
Update:
For the fluid part, instead of triggering something when a keystroke takes place, it is better to have a boolean variable such as keyUP or UP that holds a true if the key is down or false when the key is up. Then have a function onEnterFrame(event:Event):void {} that performs an action when keyUP is true. Like so:
import flash.events.*;
public class keyEvents extends MovieClip {
private var keyRIGHT:Boolean = false;
public function keyEvents():void
{
this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT) {
this.keyRIGHT = true;
}
}
function onKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT) {
this.keyRIGHT = false;
}
}
function onEnterFrame(event:Event):void
{
if(this.keyRIGHT) {
// This code is executed while the RIGHT arrow key is down.
}
}
}
If the above code does not work I think that your problem lies with your keyboard, not that it's broken or anything but the way it was made might be messing things up.
Let me know if this didn't help and I'll continue trying.

Some Problems with my AS3 code

Some troubles to make my new flash game(i used AS3,CS5.5):
I just try to make my character(hatt) walk and jump, but I can't make it at the same time, well, idk how... Furthermore, I don't know how make my character recognize the ground.
And at last this thing here:
"Scene 1, Layer 'hatt', Frame 1, Line 6 Warning: 1090: Migration issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyDown', callback_handler)."
Plz help me and give some hints plz... Thanks.
Here's the code:
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
function update(evt:Event):void
{
if (jumping)
{
hatt.y += jumpPow;
jumpPow += grav;
if (hatt.y >= stage.stageHeight)
{
jumping = false;
hatt.y = stage.stageHeight;
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
function myFunction (event: KeyboardEvent){
if(event.keyCode == Keyboard.LEFT) {
hatt.x -= 5
}
if(event.keyCode == Keyboard.RIGHT) {
hatt.x += 5
}
}
remove this line:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
remove function onKeyDown:
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
replace myFunction with this:
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
hatt.x -= 5;
if(event.keyCode == Keyboard.RIGHT)
hatt.x += 5;
if(event.keyCode == Keyboard.UP && jumping != true)
{
jumpPow = -25;
jumping = true;
}
}

Sequentially-specific combination of keys held for function?

I'm trying to give my character in a platformer game a movement mechanic in which holding the left key then also the right will cause the character to still move left but at a slower pace (i.e. movementSpeed/2) as if moon-walking (and visa versa):
public var leftKey:Boolean = false;
public var rightKey:Boolean = false;
public var upKey:Boolean = false;
public var leftFlag:Boolean = false;
function ifKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT && rightKey == false)
{
leftKey = true;
if (event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT)
{
leftFlag = true;
trace("leftFlag true");
}
}
if (event.keyCode == Keyboard.RIGHT && leftKey == false)
{
rightKey = true;
}
}
function ifKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
leftKey = false;
leftFlag = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
rightKey = false;
}
}
public function ifEnterFrame(event:Event):void
{
if (leftKey == true && leftFlag == false)
{
player1_mc.x -= mainSpeed;
trace("L");
}
if (rightKey == true && leftFlag == false)
{
player1_mc.x += mainSpeed;
trace("R");
}
if (leftKey == true && rightKey == true)
{
if (leftFlag == true)
{
player1_mc.x -= mainSpeed/2;
trace("L + R");
}
else
{
player1_mc.x += mainSpeed/2;
trace("R + L");
}
}
My output would look like this:
I hold left key
L
L
L
L
I let go of left key. Then,
I hold right key
R
R
R
R
I let go of right key. Then,
I hold right then also hold left
L
R
R+L
L
R
R+L
I let go of both. Then,
I hold left then also right
L
R
R+L
L
R
R+L
Though I know by my traces that the leftFlag is not being run, I've spent hours trying to figure out why to no avail. :(
I think your problem is this expression:
event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT
Though I am not familiar with actionscript, if it is anything like Java, the keyboard events are called once for each key press. "event" corresponds to only one key, not two different keys, and thus your expression will always return false.
The solution to your problem will probably involve something like this in both the key pressed and released functions.
if (event.keyCode == Keyboard.LEFT)
{
if (leftFlag)
{
//code here
}
if (rightFlag)
{
//code here
}
}
if (event.keyCode == Keyboard.RIGHT)
{
if (leftFlag)
{
//code here
}
if (rightFlag)
{
//code here
}
}
Hope that helps!