Forum Topic: As3 : Key Input (as2 Style)

(4,157 views • 19 replies)

This topic is 1 page long.

<< < > >>
None

dELtaluca

Reply To Post Reply & Quote

Posted at: 5/2/07 10:32 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

Key input (AS2 style)

AS3 : Main

as you'll no doubt have found out, the beloved isDown and isUp static functions are no longer existent, however, due to improvements with the Key event broadcasts, it is possible to make your own.

in AS2, tbh, the key event broadcasts were pathetic, example, hold down A, release, a KeyUp event is broadcasted, ... but, hold down A, then B, then release A, and no event is broadcasted, this is no longer the case in AS3.

basicly, i use the same technique i do in my C++ programming for simulating an isDown style function, hold a list of all key codes, with a boolean value for whether it is down or up, and access the list for the isDown isUp functions. in otherwords: below.

Feel free to use this class for you're own purposes, there is nothing majorly advanced about this, you can work out what is going on easily enough.


package delta.ui
{
import flash.events.KeyboardEvent;
public class Key
{
private var keys:Array;
private function downhandle(obj:KeyboardEvent):void
{
keys[obj.keyCode] = true;
}
private function uphandle(obj:KeyboardEvent):void
{
keys[obj.keyCode] = false;
}
public function isDown(keyCode:uint, ...keyCodes):Boolean
{
if(keyCodes.length==0)
return keys[keyCode];
else
{
if(!keys[keyCode]) return false;
for(var i:uint = 0; i<keyCodes.length; ++i)
{
if(!keys[keyCodes[i]]) return false;
}
return true;
}
}
public function isUp(keyCode:uint, ...keyCodes):Boolean
{
if(keyCodes.length==0)
return !keys[keyCode];
else
{
if(keys[keyCode]) return false;
for(var i:uint = 0; i<keyCodes.length; ++i)
{
if(keys[keyCodes[i]]) return false;
}
return true;
}
}
public function Key(stage:Object):void
{
keys = new Array(222);
stage.addEventListener(KeyboardEvent.KEY_DOWN ,downhandle,false,0,true);
stage.addEventListener(KeyboardEvent.KEY_UP,u phandle,false,0,true);
}
}
}

basicly, i assign a listener for the keyboard event KeyDown and KeyUp events, and retrieve the key that was broadcasted for, modify the array, and then access the array to determine if a key is down or not.

Usage is like this:

import delta.ui.Key;
import flash.ui.Keyboard;

var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR

...

if(keyui.isDown(Keyboard.DOWN)){}
if(keyui.isUp(Keyboard.UP)){}

Also note, that the isDown, isUp take multiple parameters:

in other words:

keyui.isDown(Keyboard.DOWN,Keyboard.UP,Keyboa rd.LEFT);

This function call will only return true, if DOWN, UP AND LEFT are all currently being pressed, i think this is a nice little improvement upon AS2's function calls in my opinion

My social worker says im special!

BBS Signature

None

trig1

Reply To Post Reply & Quote

Posted at: 5/2/07 10:59 AM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,107

Very nice, I'll definatly use this.

If I ever get round to learning AS3, I'm deadly scared. I'm so glad AS3: Mains finally out.

BBS Signature

Happy

Depredation

Reply To Post Reply & Quote

Posted at: 5/2/07 01:49 PM

Depredation LIGHT LEVEL 17

Sign-Up: 09/05/05

Posts: 4,781

Great tut delta, although it kind of defeats the point of learning AS3 :P.

BBS Signature

None

Newsdee

Reply To Post Reply & Quote

Posted at: 5/2/07 06:40 PM

Newsdee LIGHT LEVEL 18

Sign-Up: 01/21/05

Posts: 650

There's one thing missing, that I've learned from Senocular's tutorial (link in AS3: main).

If Flash loses focus, it will not register the status of the keys correctly. So you need to add an event making all keyCodes to be Up if Flash loses focus. Not a big change... if you want the code, just check Senocular's version, or I can dig it up for you and post if you want.


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 5/4/07 06:28 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

At 5/2/07 06:40 PM, Newsdee wrote: If Flash loses focus, it will not register the status of the keys correctly.

didn't know about that, thanks.

My social worker says im special!

BBS Signature

None

RyanPridgeon

Reply To Post Reply & Quote

Posted at: 5/5/07 02:37 PM

RyanPridgeon LIGHT LEVEL 11

Sign-Up: 12/07/05

Posts: 1,978

It says 5000: The class 'delta.ui' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

I make flashes because I can.
PM me for anything flash or web related or visit my blog here on NG!
Also, here's my DICK

BBS Signature

None

Skeik-Sprite

Reply To Post Reply & Quote

Posted at: 5/28/07 03:05 PM

Skeik-Sprite LIGHT LEVEL 15

Sign-Up: 06/19/05

Posts: 1,399

How am I supposed to use this?

I've been looking at AS3 for a few weeks now and I still don't understand why I get all these errors. I get about a total of 7 errors when I try to use this class, all of which I have no idea how to fix. When I do try to fix them, I only get another exponentially increased amount of errors.

I've been trying to get it so I could make a function like the AS2 key.isDown function for ages now, and this morning I came to AS3 main and find someone already made a class for it, but it doesn't work for me. I get a syntax error even from the package line and the class defining itself. It's aggravating. I would think he added the errors on purpose to stop copy and pasters, maybe, but even if Delta did it's still way over my head.

It's not like I didn't try and work on the key listeners myself, and build it from scratch, but I get errors from doing that too, even if I get the code straight from the Flash help file. It's driving me crazy.


None

Montycarlo

Reply To Post Reply & Quote

Posted at: 5/26/08 05:03 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 719

Usage is like this:
var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR

Could someone please explain.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/26/08 06:05 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

At 5/26/08 05:03 AM, Montycarlo wrote: Could someone please explain.

Select that line of code, rightclick, select copy, then go to Flash, open Actions, rightclick and select paste. Now you can use keyui.isDown to detect keypresses.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Montycarlo

Reply To Post Reply & Quote

Posted at: 5/26/08 06:44 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 719

At 5/26/08 06:05 AM, GustTheASGuy wrote: Select that line of code, rightclick, select copy, then go to Flash, open Actions, rightclick and select paste. Now you can use keyui.isDown to detect keypresses.

Thanks for being so direct to the solution, i've started fresh with AS3 and just trying to get a grip with the basics. I've put DELta's code in a file, and imported it with my 'main.as' with:

import delta.ui.Key;
import flash.ui.Keyboard;

Along with the keyboard import line.
Then I have

var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR

A couple of lines under the import's. I save, and export and get the following error:

1046: Type was not found or was not a compile-time constant: Key.

If I have my EnterFrame function with the 'if' statement inside it, I still get the same error.

Any clues?


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/26/08 06:58 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

You don't need to import the classes used in Luca's code. You need to put the AS file in a delta/ui folder. Packages are basically folders and (importing is looking them up). Usually you put general usage classes like this in a global classpath.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Montycarlo

Reply To Post Reply & Quote

Posted at: 5/26/08 07:04 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 719

At 5/26/08 06:58 AM, GustTheASGuy wrote: You don't need to import the classes used in Luca's code. You need to put the AS file in a delta/ui folder. Packages are basically folders and (importing is looking them up). Usually you put general usage classes like this in a global classpath.

So my Keys.as file should be somewhere like this:
root/main/main.as
/Delta/ui/Keys.as
If thats what you mean, I had already figured that out and is already done.
As I said, I am getting the following errors:

1046: Type was not found or was not a compile-time constant: Key.
Source: var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR
1180: Call to a possibly undefined method Key.
Source: var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR
1120: Access of undefined property stage.
Source: var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR

None

Montycarlo

Reply To Post Reply & Quote

Posted at: 5/26/08 07:08 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 719

At 5/26/08 07:04 AM, Montycarlo wrote: root/main/main.as
/Delta/ui/Keys.as

Ellaborate on my directory:
My .fla source file is in a folder call 'root', with two folders; one called 'Delta' and another called 'main'. In the 'main' folder there are the main .as files, which are all fully functional. The 'main.as' file imports Delta's file, named Keys.as, which is located in the folder 'ui' which is in the folder 'Delta' with this code:

import Delta.ui.Keys;

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 5/26/08 07:22 AM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

Simplify it by doing it yourself. It's easier to just do something like this:

package 
{
	import flash.display.Stage;
	public class Key
	{
		private static var keys:Object = new Object();
		public static function init(s:Stage):void
		{
			s.addEventListener("keyDown", okd);
			s.addEventListener("keyUp", oku);
		}
		private static function okd(e):void
		{ keys[e.keyCode] = true; }
		private static function oku(e):void
		{ delete keys[e.keyCode]; }
		public static function isDown(n:uint):Boolean
		{ return (n in keys); }
	}
}

To use it, just do:

Key.init(stage);

addEventListener("enterFrame",oef);
function oef(e):void
{
	trace(Key.isDown(65));
}

And be sure stage is accessible (ie the class where you use it must've been added to something's displaylist).

And no need to import anything.

BBS Signature

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 5/26/08 07:29 AM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

Actually, even better would be:

package 
{
	import flash.display.Stage;
	public class Key
	{
		private static var keys:Object = new Object();
		public static function init(s:Stage):void
		{
			s.addEventListener("keyDown", okd);
			s.addEventListener("keyUp", oku);
			s.addEventListener("deactivate", oda);
		}
		private static function okd(e):void
		{ keys[e.keyCode] = true; }
		private static function oku(e):void
		{ delete keys[e.keyCode]; }
		private static function oda(e):void
		{ keys = new Object(); }
		public static function isDown(n:uint):Boolean
		{ return (n in keys); }
	}
}
BBS Signature

None

Montycarlo

Reply To Post Reply & Quote

Posted at: 5/26/08 07:47 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 719

I think i'm going to have to buy essential actionscript 3.


None

Blackfang

Reply To Post Reply & Quote

Posted at: 5/26/08 09:32 AM

Blackfang LIGHT LEVEL 21

Sign-Up: 08/27/05

Posts: 3,322

Good job Luca, once again you make other coders want to kill themselves wishing they could code like you

or maybe thats just me =(

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 5/26/08 09:37 AM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

At 5/26/08 09:32 AM, Blackfang wrote: Good job Luca, once again you make other coders want to kill themselves wishing they could code like you

or maybe thats just me =(

lmao. It's not even complicated...

BBS Signature

None

Blackfang

Reply To Post Reply & Quote

Posted at: 5/26/08 09:43 AM

Blackfang LIGHT LEVEL 21

Sign-Up: 08/27/05

Posts: 3,322

Im an artist tho, ive tried learning AS. im too lazy >.<


None

jaco208

Reply To Post Reply & Quote

Posted at: 12/11/08 10:31 PM

jaco208 EVIL LEVEL 08

Sign-Up: 04/18/06

Posts: 68

I'm incredibly fluent in AS 2.0. I want to learn AS 3.0 though so i can use things like Box 2d. I decided to try and make a character with the instance player move with arrow keys. Since i'm a total newbie to AS 3.0. How would i implement this. I'm using flash 10 (cs4). Please help me

STOP LEECHING, START SEEDING!
1337 GAMEZ

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 07:28 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!