PDA

View Full Version : [AS3] Key checking Code


Gridden
03-03-2009, 05:22 PM
Here is a piece of code from my program:

function characterMovement(yourEvent:KeyboardEvent):void{

//Function for attacking and combos.
if (yourEvent.keyCode==Keyboard.A) //Attack 1
{
if (char1.currentLabel != "attack1")
{
char1.gotoAndStop ("attack1");
}
}

//Function for when the key is pressed (To move the character and play the
//corresponding animation.

if (yourEvent.keyCode==Keyboard.RIGHT) //Move right
{
if (char1.currentLabel != "walkRight")
{
char1.gotoAndStop ("walkRight");
}
wasLeft = 0;
wasRight = 1;
char1.x+=speed;
}
.
.
.
.
.
.
.
.
.
.
.

stage.addEventListener(KeyboardEvent.KEY_DOWN,char acterMovement);

And here is the error:

"1119: Access of possibly undefined property A through a reference with static type Class."

Does anyone have any idea why all of the key checking code works with the left, right, up, and down key, but not with any other key?

Strange...

CyanBlue
03-03-2009, 05:29 PM
Howdy and Welcome... :)

Keyboard.A is not defined in the Keyboard class, yet you are trying to use it... That's why you are getting that error message... Flash provides you code for Keyboard.LEFT or Keyboard.RIGHT or Keyboard.NUMPAD_1, but you have to use key codes for other keys that are not defined in that table... Check out the 'Keyboard Keys and Key Code Values' section in the Flash manual for those keys...

Gridden
03-03-2009, 05:52 PM
you mean something like:

if (yourEvent.charCode==Keyboard.charCodeAt (65)) ?

Because that also doesnt work. The reason i simply typed "A" was because it showed in the drop down menu, so i have no idea why they actually put it there.

And sorry if the questions are stupid, its my first piece of code :).

CyanBlue
03-03-2009, 05:56 PM
No, no question is stupid... Only there is a stupid answer... ;)

I don't get A in the drop down menu... Weird... What's the version of Flash IDE you are using???

Gridden
03-03-2009, 06:03 PM
Whats that? The library version for AC3?

Here, have a look:

http://img26.imageshack.us/img26/2954/aaaaaaaaaaaaaas.jpg (http://img26.imageshack.us/my.php?image=aaaaaaaaaaaaaas.jpg)
http://img26.imageshack.us/img26/aaaaaaaaaaaaaas.jpg/1/w341.png (http://g.imageshack.us/img26/aaaaaaaaaaaaaas.jpg/1/)

CyanBlue
03-03-2009, 06:35 PM
Um... I've got Flash CS3, and I don't get A, B or C... Weird...

Gridden
03-03-2009, 08:38 PM
So about that code i asked about...

Edit: mine is cs4 btw

CyanBlue
03-03-2009, 08:43 PM
I still have my copy of CS4 in the drawer, so I cannot tell you on that, but this code should work...
function characterMovement(yourEvent:KeyboardEvent):void
{
trace("yourEvent.keyCode = " + yourEvent.keyCode);
if (yourEvent.keyCode == 65)
{
trace("You pressed A key...");
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, characterMovement);

Gridden
03-03-2009, 09:29 PM
Thanks for the input mate, il try it out tomorrow. Could you explain how it works thou? I have no idea what trace does.

CyanBlue
03-03-2009, 10:31 PM
The KeyboardEvent.keyCode contains the ASCII value of the key you have just pressed... and the trace() function displays whatever you have inside the parenthesis whether it is literal or variable... Check out the Flash manual for more information on those... ;)

Gridden
03-04-2009, 12:20 PM
It complied good now, but it still doesnt work...

Here is the code from the attack mechanism:

//Function for attacking and combos.
if (yourEvent.keyCode==65) //Attack 1
{
if (char1.currentLabel == "attack2") { //Combo attack 3
{
if(wasRight == 1) {
if (currentLabel != "rightAttack3") {
char1.gotoAndStop ("rightAttack3");
}
}
if(wasLeft == 1) {
if (currentLabel != "leftAttack3") {
char1.gotoAndStop ("leftAttack3");
}
}
}
if (char1.currentLabel == "attack1") { //Combo attack 2
{
if(wasRight == 1) {
if (currentLabel != "rightAttack2") {
char1.gotoAndStop ("rightAttack2");
}
}
if(wasLeft == 1) {
if (currentLabel != "leftAttack2") {
char1.gotoAndStop ("leftAttack2");
}
}
}
}
}
if (char1.currentLabel != "attack1") //Attack 1
{
if(wasRight == 1) {
if (currentLabel != "rightAttack1") {
char1.gotoAndStop ("rightAttack1");
}
}
if(wasLeft == 1) {
if (currentLabel != "leftAttack1") {
char1.gotoAndStop ("leftAttack1");
}
}
}
}

Edit: oh, and how would i go about limiting what area the character can move in? The game will be played in a room where the wall can also be seen (not just the floor) so i need to make it so that the character cant actually walk onto the wall. I tried this...

if (char1.y <= 185) {
char1.y = 185;
}

... where 185 is the y value for where the wall starts, but it wouldnt work.

CyanBlue
03-11-2009, 10:25 PM
Ah... I do see Keyboard.A or Keyboard.B... It's AIR specific constants... Not sure why they are AIR specific though...
http://www.actionscript.org/forums/showthread.php3?t=199304