PDA

View Full Version : keystroke input variations..


ard0x
02-18-2004, 10:31 PM
First of all I'm new to the forum, so hi everyone. I'm just delving into my very first actionscript game.
Anyway, I am not sure if this belongs in 'simple stuff', but I'm hoping that it does have a simple solution that I am just not seeing.

I am programming a ship for a game:

- When you press up, a power meter increases by one unit. When you press down, it reduces the power meter by one unit. (I have an LED graphic interface, so I want the levels to rise incrementally on every keystroke.)
- When you press spacebar, it releases the accumlated power, and fires the ship.

I have an algorithm all planned out on how to do this, except for the user input. If it reads Key.isDOWN on every frame, the levels rise WAY too fast, and if I rise by a number less than 1, say 0.3, and then update the display whenever it hits an integer, I fear the controls will feel too sloppy.

Is there a way to:

1. send a *single* signal upon keypress? If my variable "shippower" is suppose to increase by 1 every keystroke, how do I prevent it from adding 1 every single frame that the key is pressed on?
instead of Key.isDown(Key.UP) I guess I am looking for a Key.hasbeenpressed(Key.UP) :)

2. Or better yet, can I somehow use the way a typing keystroke works? (on press, one letter is typed. If you hold it down, there is a delay, and then it repeats at a set interval.)

Thanks in advance.

CyanBlue
02-18-2004, 10:50 PM
Howdy and Welcome... ;)

You can use KeyListener to take care of it... Try this on the new movie...keyListener = new Object();
keyListener.onKeyUp = function ()
{
trace(Key.getCode());
}
Key.addListener(keyListener);

ard0x
02-18-2004, 11:23 PM
Thanks for the quick reply and the welcome, Cyan.

The Listener still acts in the same way, in that it evaluates keyDown every frame. And while having it update only on release is a pretty good solution, I think it would make the controls feel a bit laggy.

However, I just had an idea. I'll use a setInterval delay inside of the function to slow down the variable updates. That way, the user can hold down the key and have a manageable and incremental increase. Now I just have to figure out how to use setInterval. hehe

Maybe I could even put some acceleration on the delay... And the project grows. :)

CyanBlue
02-19-2004, 06:35 AM
That sounds good... I am not sure how it will affect the performance of the project since you are asking for extra power with the setInterval() function... Flash help tells you all about the setInterval() function by the way... Shoot the question if it doesn't work for you... ;)

ard0x
02-19-2004, 02:00 PM
I think I may have found what I need.
It's a snippet of code from Ed Mack's site:
http://www.edmack.com/flash/run.swf

if(Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) && foot == "right"){
foot = "left";
_x += step;

I'm not entirely clear on the differences between assign (=) and equality (==) but this code looks like I could test for a "waitingforinput" variable, when the key is pressed, and then also when the key is pressed, I can switch the "waitingforinput" variable off, and the input will stop working. On keyRelease, the "waitingforinput" will be set back on.

If this does indeed work, it will be even more important when I use [spacebar] to fire the ship. Right now, if I hold down spacebar, I can continue to move around without ever stopping. (not the point of my game, which will be more of a turn-based game, like pool, where the ship would be like the cue ball.)