Forum Topic: AS3 triple key problem

(106 views • 12 replies)

This topic is 1 page long.

<< < > >>
Resigned

SenorLlama

Reply To Post Reply & Quote

Posted at: 6/16/08 10:45 PM

SenorLlama NEUTRAL LEVEL 10

Sign-Up: 03/11/08

Posts: 111

Fellow AS3 programmers, I have reached and impasse. I've had a bit of programming experience. I've made a few games (none worth posting) and done work for others so I'm ashamed to have to ask you this n00bish question.

I'm having a problem with moving a character (named "utrap" in this case) and firing a bullet at the same time. Not in all of the directions... just one. When I move up and to the left at the same time and try to fire a bullet I get movement but no bullet laden satisfaction.

Has anyone run into a situation like this? Is there a hierarchy of keyCode's that I am not aware of? Any hints would be appreciated. Code to follow

 // moves utrap in one of four directions.  Or two directions if two keys are pressed.
		private function moveUTRAP(){
			
			// Fires a bullet by calling the fireBullet function
			if(spaceBar) fireBullet();
			
			// Four diagonal directions
			if(upArrow && rightArrow){
				
				utrap.y -= 10;
				utrap.x += 15;
			}
			
			else if(upArrow && leftArrow){
				
				utrap.y -= 10;
				utrap.x -= 15;
			}
			
			else if(upArrow && leftArrow && spaceBar){
				
				fireBullet();
				utrap.y -= 10;
				utrap.x -= 15;
			}
			
			else if(downArrow && rightArrow){
				
				utrap.y += 10;
				utrap.x += 15;
			}
			
			else if(downArrow && leftArrow){
				
				utrap.y += 10;
				utrap.x -= 15;
			}
			
			// Four cardinal directions
			else if(upArrow){
				
				utrap.y -= 10;
			}
			
			else if(downArrow){
				
				utrap.y += 10;
			}
			
			else if(rightArrow){
				
				utrap.x += 15;
			}
			
			else if(leftArrow){
				
				utrap.x -= 15;
			}
		}

All movement and firing variables are boolean and are set to true using an event listener based off of a keyboard event. They are set back to false when the key is let go. As I said before, all the other motions and firing work perfectly in conjunction with each other except for up/right. I'd be glad to answer any questions concerning the program. The .fla itself solely consists of two symbols, both properly linked.

Thank you for your help ahead of time.

BBS Signature

None

ssjskipp

Reply To Post Reply & Quote

Posted at: 6/16/08 11:26 PM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 715

Just throwing this out there (it may help setting it up), I usually program keyboard input like so:

var keys:Object = new Object();
function key(event:KeyboardEvent){
if (event.type == "keyDown"){
keys[event.keyCode] = true;
} else {
keys[event.keyCode] = false;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, key);
stage.addEventListener(KeyboardEvent.KEY_UP, key);

Typed here, may have an error, but then all you need to do is:

if (keys[KEY CODE HERE]){
}

EX:

if (keys[65] && keys[66] && keys[67]){
}

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


Elated

SenorLlama

Reply To Post Reply & Quote

Posted at: 6/17/08 12:41 AM

SenorLlama NEUTRAL LEVEL 10

Sign-Up: 03/11/08

Posts: 111

Wow that's a really interesting idea. You effectively reduced the key input in the main class down to a few lines.

But let me get straight. Would I then have to create a new class file for "keys" and put all my keyCode instances in there? Because you have your keys variable listed as an object. Hmm... that's a bit confusing too. It's declared as an object but you treat it as an array, which I could totally understand because that would answer my previous question. Is it possible to use an object as an array as well?

But I really do like your keyboard set up. I think that is a much better idea then what I use.

BBS Signature

Elated

SenorLlama

Reply To Post Reply & Quote

Posted at: 6/17/08 06:57 PM

SenorLlama NEUTRAL LEVEL 10

Sign-Up: 03/11/08

Posts: 111

I tried the new method. I love how organized it is and how I only need to use one variable. Problem is, it still didn't solve my problem. I ended up figuring a way around it. For some reason the up and left arrow keys have some kind of beef with my space bar. So I merely switch my "fire" key from the space bar to the A key. Now it works just fine.

But thank you for helping me out with the new method, it works perfectly.

BBS Signature

None

Gutya

Reply To Post Reply & Quote

Posted at: 6/17/08 07:14 PM

Gutya FAB LEVEL 07

Sign-Up: 11/27/06

Posts: 777

Keyboards can only press certain keys down at the same time, a lot of different keyboard brands seem to have different combinations you are and are not allowed to press, but there's always some. It was probably a hardwar problem and not a problem with Flash itself.


None

Eibx

Reply To Post Reply & Quote

Posted at: 6/17/08 08:19 PM

Eibx LIGHT LEVEL 09

Sign-Up: 09/01/07

Posts: 101

I have the same problem on my Laptop (Dell), but not on my Desktop.

But I must thank you so much ssjskipp, you really reduced my code.

- Thanks.

BBS Signature

None

ssjskipp

Reply To Post Reply & Quote

Posted at: 6/17/08 10:23 PM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 715

Just FYI, I'm not treating it as an array, it's an object with properties that have numbers as their names. Objects work as such:
object = {property:value, property2:value2};

So, for a MovieClip, some base properties could be expressed as:
{x:50, y:24};

And how ActionScript uses objects, you can access the properties of an object in two ways:
Way 1 (common way):
object.property
EX: movieClip.x = 47;

Way 2 (dynamic, uncommon):
object['string equal to property name]
EX: movieClip['x'] = 33;

So, my script creates an object (keys) that stores key codes that are either true or false. True if down, false if not. Then all you need to do is access it as:
keys[keyCode]
If it doesn't exist, it'll evaluate to false. If it does exist, then it'll return the value (true or false) which is dependent on if they key is down.

nifty, eh?

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

SenorLlama

Reply To Post Reply & Quote

Posted at: 6/17/08 10:32 PM

SenorLlama NEUTRAL LEVEL 10

Sign-Up: 03/11/08

Posts: 111

At 6/17/08 10:23 PM, ssjskipp wrote: Just FYI, I'm not treating it as an array, it's an object with properties that have numbers as their names. Objects work as such:
object = {property:value, property2:value2};

So, for a MovieClip, some base properties could be expressed as:
{x:50, y:24};

And how ActionScript uses objects, you can access the properties of an object in two ways:
Way 1 (common way):
object.property
EX: movieClip.x = 47;

Way 2 (dynamic, uncommon):
object['string equal to property name]
EX: movieClip['x'] = 33;

So, my script creates an object (keys) that stores key codes that are either true or false. True if down, false if not. Then all you need to do is access it as:
keys[keyCode]
If it doesn't exist, it'll evaluate to false. If it does exist, then it'll return the value (true or false) which is dependent on if they key is down.

nifty, eh?

Well I'll be damned, that is nifty. I gotta say though, I went ahead and changed your object to an array and it still works perfectly. It never really occurred to me that an array might be an object itself.

BBS Signature

None

Rammer

Reply To Post Reply & Quote

Posted at: 6/17/08 10:41 PM

Rammer DARK LEVEL 31

Sign-Up: 06/08/03

Posts: 4,314

i know this isn't QUITE in AS3, but here's the principle i use for coding movement that can go diagonal.

i assign variables (left, right, up, down) to whether the key is down

var left:Boolean = Key.isDown(Key.LEFT);
var right:Boolean = Key.isDown(Key.RIGHT);
var up:Boolean = Key.isDown(Key.UP);
var down:Boolean = Key.isDown(Key.DOWN);

then, i assign movement simply using those variables.

vx = (right - left) * speed;
vy = (down - up) * speed;

because the isDown() method returns a boolean, it also basically returns a 0 or 1, which i use for the movement. if left is down vx will be (0 - 1) * speed, and if the down arrow key is pressed, vy will be (1 - 0) * speed.

get what i mean? i think that's pretty simple to incorporate into AS3 when you use the method ssjskipp described.

{ AMD Athlon 64 X2 5200+ 2.6 GHz - two 320 GB HDDs in RAID 0 - NVIDIA GeForce 8800 GT - Gyration mouse - Razer Tarantula keyboard - 2 GB Corsair RAM }

(:


None

SenorLlama

Reply To Post Reply & Quote

Posted at: 6/17/08 11:13 PM

SenorLlama NEUTRAL LEVEL 10

Sign-Up: 03/11/08

Posts: 111

At 6/17/08 10:41 PM, Rammer wrote: i know this isn't QUITE in AS3, but here's the principle i use for coding movement that can go diagonal.

i assign variables (left, right, up, down) to whether the key is down

var left:Boolean = Key.isDown(Key.LEFT);
var right:Boolean = Key.isDown(Key.RIGHT);
var up:Boolean = Key.isDown(Key.UP);
var down:Boolean = Key.isDown(Key.DOWN);

then, i assign movement simply using those variables.

vx = (right - left) * speed;
vy = (down - up) * speed;

because the isDown() method returns a boolean, it also basically returns a 0 or 1, which i use for the movement. if left is down vx will be (0 - 1) * speed, and if the down arrow key is pressed, vy will be (1 - 0) * speed.

get what i mean? i think that's pretty simple to incorporate into AS3 when you use the method ssjskipp described.

Ah! I see what you did there, yeah, that would reduce a few lines of code by preventing opposite keys from overriding each other and dealing with posit. That's pretty smart

BBS Signature

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 6/17/08 11:15 PM

Glaiel-Gamer NEUTRAL LEVEL 26

Sign-Up: 12/28/04

Posts: 6,966

Use A instead of Space, some keyboards can't register 2 arrow keys and the spacebar at the same time. It's not flash's fault.


Muted

SenorLlama

Reply To Post Reply & Quote

Posted at: 6/17/08 11:57 PM

SenorLlama NEUTRAL LEVEL 10

Sign-Up: 03/11/08

Posts: 111

At 6/17/08 06:57 PM, SenorLlama wrote: So I merely switch my "fire" key from the space bar to the A key. Now it works just fine.
from the space bar to the A key.
A key.

Way ahead of you, but thank you anyways.

BBS Signature

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 6/18/08 04:51 AM

GuyWithHisComp LIGHT LEVEL 23

Sign-Up: 11/10/05

Posts: 4,029

Yeah, keybaords limits how many keys can be pressed at the same time. This also applies to letters and numbers and not just arrowkeys but you can usually have more letters pressed down at trhe same time than arrowkeys+space. This sucks for some 2 player games.

At 6/16/08 11:26 PM, ssjskipp wrote: Just throwing this out there (it may help setting it up), I usually program keyboard input like so:

Don't forget about the DEACTIVATE event, which fires when the flash no longer has focus. Because when a flash file loses focus it won't register key-events anymore so if you hold the right key down and then press outside of the flash file to lose focus, the right key will still act as if it's pressed inside the flash even if you let go of it.

var keyIsDown:Object = new Object();

function key (event:KeyboardEvent)
{ keyIsDown[event.keyCode] = (event.type == "keyDown"); }
function dekey (event:Event)
{ keyIsDown = new Object(); }

stage.addEventListener("keyDown", key);
stage.addEventListener("keyUp", key);
stage.addEventListener("deactivate", dekey);
BBS Signature

All times are Eastern Daylight Time (GMT -4) | Current Time: 12:34 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!