Be a Supporter!

Keyboard codes not working [AS3]

  • 553 Views
  • 3 Replies
New Topic Respond to this Topic
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Keyboard codes not working [AS3] 2012-04-28 00:19:46 Reply

Okay, I decided to switch from using the arrow keys to move my character to using the "W,A,S,D" keys instead.

What should have been a smooth transition has become a very stupid nightmare. For some odd reason, "W" and "D" work correctly, yet "A" and "S" do not.

Here is some code:

function PressAKey(event:KeyboardEvent):void
{
	if(event.keyCode == 68)//d
	{
	 rightKey = true;
	}
	if(event.keyCode == 65)//a
	{
	 leftKey = true;
	}
	if(event.keyCode == 87)//w
	{
	 upKey = true;
	}
	if(event.keyCode == 83)//s
	{
	 downKey = true;
	}
}

It worked just fine when it used the arrow keys. Only now that I tried changing it is it not working completely.

(This code is from a tutorial on Youtube)

nameistaken1
nameistaken1
  • Member since: May. 21, 2007
  • Offline.
Forum Stats
Member
Level 16
Game Developer
Response to Keyboard codes not working [AS3] 2012-04-28 02:44:41 Reply

Did you set up your event right?
Something like this:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
function onKeyPressed(event:KeyboardEvent):void
{
	switch (event.keyCode)
{
	case 87:
	        upKey = true;
		break;
	case 65:
		leftKey = true;
		break;
case 83:
		upKey = true;
		break;
case 68:
	        downKey = true;
		break;
default:
		//wrong key was entered
		break;

If a structure like that isn't smooth, try declaring your used variables private. Another piece of of code right be affecting them. You can set gettings/setters if you need them to be public.


BBS Signature
4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Keyboard codes not working [AS3] 2012-04-28 06:37:16 Reply

Make sure you have keyboard shortcuts disabled.

From the swf window in flash. Control -> Disable keyboard shortcuts.

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to Keyboard codes not working [AS3] 2012-04-28 11:14:18 Reply

At 4/28/12 06:37 AM, 4urentertainment wrote: Make sure you have keyboard shortcuts disabled.

From the swf window in flash. Control -> Disable keyboard shortcuts.

That did it. Thanks!