ForumsProgramming ForumKeypress Events

9 4306
deserteddreams
offline
deserteddreams
20 posts
Nomad

Hey, I got this Keypress events. It works with arrow keys, Space, etc.
But i need it to register the X and Z events. Which wont work for some reason.
Here is all the code associated with the keypress event:
Please help me. Thank you =]

var X_PRESSED:Boolean = false;
var Z_PRESSED:Boolean = false;

function startGame()
{
Theres other code in here, im just sectioning out just the keyboard event. . .

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}

function keyPressed(event:KeyboardEvent)
{
switch (event.keyCode)
{
case Keyboard.Z :
Z_PRESSED = true;
break;
case Keyboard.X :
X_PRESSED = true;
break;
}
}

  • 9 Replies
deserteddreams
offline
deserteddreams
20 posts
Nomad

The error is: 1119: Access of possibly undefined property Z through a reference with static type Class

Rameares
offline
Rameares
3 posts
Nomad

I don't think you can use refrences to x and z keys like you can with arrow keys. you need to use the actual keyCode for x it is 120 and for z it is 122.

a list of flash key codes
http://people.uncw.edu/tompkinsj/112/flashactionscript/keycodes.htm

What you error msg is saying is that there is no Z variable or constant in the keyboard class.

Matrix159
offline
Matrix159
337 posts
Nomad

Here I'll give you the link of what Rameares said.

Flash Key Codes

deserteddreams
offline
deserteddreams
20 posts
Nomad

Thanks, But how would i use these codes?
Im kinda new to flash.

Darkroot
offline
Darkroot
2,763 posts
Peasant

if ( event.keyCode == 65 )
//left
if ( event.keyCode == 68 )
//right
if ( event.keyCode == 87 )
//up
if ( event.keyCode == 83 )
//down

That how you use keyCode just look up some tutorial like this and your done.

PixelSmash
offline
PixelSmash
566 posts
Nomad

If you want to use it more often, just create a static class once called KeyCodes, and add all the possible keycodes in it... that way, you can use KeyCode.Z, as opposed to using the numbers every time!

akunu
offline
akunu
35 posts
Nomad

If you want to use it more often, just create a static class once called KeyCodes, and add all the possible keycodes in it... that way, you can use KeyCode.Z, as opposed to using the numbers every time!

...or just use as3's built in Keyboard class. I think it's in flash.ui or something...
PixelSmash
offline
PixelSmash
566 posts
Nomad

Possibly, but if I remember correctly it's AIR only, so I don't know if it works in regular FP applications

GeraldGefter
offline
GeraldGefter
24 posts
Nomad

In flash you can just open actionscript library and find operators functions and classes you want

Showing 1-9 of 9