|
-
[RESOLVED] Key.isDown equivient in AS3?
I used the Search function to try and solve this problem, then posted in Mr Malee's old thread. But people seem to be ignoring the new activity in that old one, so I'm starting my own thread after all.
Basically, I just want to run some code while a specific key is held down.
The problem is, they removed the isDown method "for security reasons," and failed to provide a replacement method, despite the fact that every realtime Flash game in the known universe relies upon it. Thanks, Adobe.
Anyway, Fall_X's solution was to implement his own keydown class, as follows:
Code:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.KeyboardEvent;
class MyKey
{
private static var downArray:Array=[];
private static var pressedArray:Array=[];
private static var stg:Stage;
public static function set stage(value:Stage) {
if(stg) {
stg.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stg.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
stg=value;
stg.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler,false,0,true);
stg.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler,false,0,true);
}
public static function isDown(keyCode:int):Boolean {
// can't just return downArray[int] because it can be undefined or null,
// and we want to return a boolean
if(downArray[keyCode]) {
return true;
}
return false;
}
public static function isPressed(keyCode:int):Boolean {
// this one will only return true once per key
// which is handy in some cases, when you don't want to repeat an action
// if the key stays pressed
if(pressedArray[keyCode]) {
delete pressedArray[keyCode];
return true;
}
return false;
}
private static function keyDownHandler(event:KeyboardEvent):void {
downArray[event.keyCode as int]=true;
pressedArray[event.keyCode as int]=true;
}
private static function keyUpHandler(event:KeyboardEvent):void {
delete downArray[event.keyCode];
delete pressedArray[event.keyCode];
}
}
}
Fall_X's later comments suggest that he has tested and updated his class to make it work, and he offers the following presumably as part of a test-case:
I updated the code above to something that actually works, lol. You'll have to set the static variable "stage" to the current stage in order to make it work - ie :
Code:
MyKey.stage=this.stage; // needed only once
if(MyKey.isDown(38)) {
trace("up");
}
Unfortunately, I can't figure out how to even create an instance of this new class. Every time I try, I get various flavors of runtime errors. Also, he's inexplicably named his new class MyKey, when typically flash developers use my* to designate an instance name. Then he (apparently) used the word MyKey as an instance name in his main code.
Someone please show me hot to set up an instance of Fall_X's myKey object. If his class is wrong, make it right. If the class is right, show me how to use it properly. I'm not an OOP whiz yet, as I'm still learning to use AS3 as a functional language. Implementing it in the timeline of a FLA would be preferable, since that's my comfort zone, but I'll settle for a generic game class intended to become a Document Class, if that's the only way you know how to help me.
But please, I need to see a working example before I'll ever understand how to use this thing. Every attempt I've made (both as a Document Class and as functional code in my document's timeline) has resulted in one or more runtime errors.
I've tried everything. I've tried importing the class AS file, I've tried including the class AS file, I've tried using the name MyKey as the instance name, and I've tried making up my own unique instance name. It all fails. I clearly don't know what I'm doing. Need help. Please send brains.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|