00:00
00:00
Newgrounds Background Image Theme

eversnot just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As3 : Key Input (as2 Style)

11,772 Views | 19 Replies
New Topic Respond to this Topic

As3 : Key Input (as2 Style) 2007-05-02 10:32:39


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

Response to As3 : Key Input (as2 Style) 2007-05-02 10:59:53


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.

Response to As3 : Key Input (as2 Style) 2007-05-02 13:49:22


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


BBS Signature

Response to As3 : Key Input (as2 Style) 2007-05-02 18:40:50


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.

Response to As3 : Key Input (as2 Style) 2007-05-04 18:28:58


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.

Response to As3 : Key Input (as2 Style) 2007-05-05 14:37:38


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

Response to As3 : Key Input (as2 Style) 2007-05-28 15:05:16


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.

Response to As3 : Key Input (as2 Style) 2008-05-26 05:03:28


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

Could someone please explain.


Although practicality beats purity.

Errors should never pass silently.

In the face of ambiguity, refuse the temptation to guess.

Response to As3 : Key Input (as2 Style) 2008-05-26 06:05:24


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.


BBS Signature

Response to As3 : Key Input (as2 Style) 2008-05-26 06:44:04


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?


Although practicality beats purity.

Errors should never pass silently.

In the face of ambiguity, refuse the temptation to guess.

Response to As3 : Key Input (as2 Style) 2008-05-26 06:58:42


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.


BBS Signature

Response to As3 : Key Input (as2 Style) 2008-05-26 07:04:35


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

Although practicality beats purity.

Errors should never pass silently.

In the face of ambiguity, refuse the temptation to guess.

Response to As3 : Key Input (as2 Style) 2008-05-26 07:08:49


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;

Although practicality beats purity.

Errors should never pass silently.

In the face of ambiguity, refuse the temptation to guess.

Response to As3 : Key Input (as2 Style) 2008-05-26 07:22:33


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

Response to As3 : Key Input (as2 Style) 2008-05-26 07:29:53


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

Response to As3 : Key Input (as2 Style) 2008-05-26 07:47:55


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


Although practicality beats purity.

Errors should never pass silently.

In the face of ambiguity, refuse the temptation to guess.

Response to As3 : Key Input (as2 Style) 2008-05-26 09:32:09


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

or maybe thats just me =(

Response to As3 : Key Input (as2 Style) 2008-05-26 09:37:53


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

Response to As3 : Key Input (as2 Style) 2008-05-26 09:43:42


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

Response to As3 : Key Input (as2 Style) 2008-12-11 22:31:34


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


BBS Signature