fluidly dealing with multiple keypresses in ActionScript3 - actionscript-3

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.

Related

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.

AS3 Disabling keyboard text input

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();
}

A conflict exists with definition leftIdle1 in namespace internal

I'm scripting a game with Actionscript 3.0 in Flash Professional CS5.5, but I get an error.
Scene 1, Layer 'as2', Frame 153, Line 4 1151: A conflict exists with definition leftIdle1 in namespace internal.
(I get these with the other Variables too.)
Now it's a platform game, and I am going to put cutscenes throughout in the game and I need to switch from frames and put the code in another frame. But it gives that error, I turned off the 'Automatic Declare stage instances' function, now I checked this website and Googled it, people get it with their Movieclips, I get it with my variables.
This is my script:
var leftKeyDown1:Boolean = false;
var rightKeyDown1:Boolean = false;
var spaceKeyDown1:Boolean = false;
var leftIdle1:Boolean = false;
var rightIdle1:Boolean = true;
var mainSpeed1:Number = 4;
player.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event)
{
if (leftKeyDown1)
{
player.x -= mainSpeed1;
leftIdle1 = true;
rightIdle1 = false;
player.gotoAndStop("walk_left");
}
if (rightKeyDown1)
{
player.x += mainSpeed1;
rightIdle1 = true;
leftIdle1 = false;
player.gotoAndStop("walk_right");
}
if (rightIdle1 && !rightKeyDown1 && !leftKeyDown1)
{
player.gotoAndStop("idle_right");
}
else if (leftIdle1 && !rightKeyDown1 && !leftKeyDown1)
{
player.gotoAndStop("idle_left");
}
if (collide.hitTestObject(player))
{
player.x = player.x + mainSpeed1;
}
if (trigger1.hitTestObject(player))
{
son1.gotoAndStop("walkRight");
trigger1.gotoAndStop(2);
son1.x += 2;
}
if (trigger2.hitTestObject(player))
{
gotoAndPlay(4);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent)
{
if (event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown1 = true;
}
if (event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown1 = true;
}
if (event.keyCode == 32)
{
spaceKeyDown1 = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysU1);
function checkKeysUp(event:KeyboardEvent)
{
if (event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown1 = false;
}
if (event.keyCode == 32)
{
spaceKeyDown1 = false;
}
if (event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown1 = false;
}
}
It's probably coded in a weird way, but whatever.
I have no idea what to do at this point.
Help would be really appreciated.
EDIT:
Oh I got another error too. It's with Duplicate function, and I can't seem to fix it but to rename those, and it will take a long time to rename them every-time. So if someone has something for that, thanks!
you have duplicated definitions.
For example:
var leftIdle1:Boolean
exists multiple times - remove the duplicates

AS3.0 if/else if evaluation behaviour for velocity control

Sorry if this is obvious to others, but I can't get my head around something in ActionScript 3.0 (huge n00b btw)
I have this code for controlling velocity:
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
When run, if I hold both LEFT and UP the Sprite moves Diagonally, but the fact that the two last conditionals (Keyboard.UP & Keyboard.DOWN) are elseifs should prevent them from being evaluated at all shouldnt it?
Is anyone able to shed some light on the behaviour?
When you press both buttons flash fires two independent events for each button. If you want to skip this case, you can make state flags (leftPressed, rightPressed, etc) for each button, change state in key handler and call the check motion method according to current states of each button.
If you don't want diagonal movement, set the velocity components to zero first like this:
public function keyDownHandler(event:KeyboardEvent):void
{
vx = vy = 0; ////
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}

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!