user3484086
user3484086

Reputation: 1

if statement not being reconized

I'm new to Action Script 3 I have tried to make a movie where i can control a box on the screen move with the left and right arrow keys but I keep getting a message "Access of possible undefined property LEFT through a reference with static class". Certain statements don't turn blue, such as if and function? This is the code:

stage.addEventListener(KeyboardEvent.KEY_DOWN,myFunction);

function myFunction (event:KeyboardEvent){

    if (event.keyCode == KeyboardEvent.LEFT){
        blueBox.x -=5
    }
    if (event.keyCode == KeyboardEvent.RIGHT){
        blueBox.x +=5
    }
}

Upvotes: 0

Views: 51

Answers (3)

Moddl
Moddl

Reputation: 360

I don't think there is such a thing as KeyBoardEvent.LEFT/RIGHT. Try changing left to 37 and right to 39. they are the numerical value for those keys.

Upvotes: 0

moosefetcher
moosefetcher

Reputation: 1901

At the start of your code you need to import the KeyboardEvent and Keyboard Classes, ie:

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

You should also change KeyboardEvent.LEFT and KeyboardEvent.RIGHT to:

if (event.keyCode==Keyboard.LEFT)

and

if (event.keyCode==Keyboard.RIGHT)

The reason for that? ActionScript is trying to access the number contained in the Keyboard.LEFT and Keyboard.RIGHT static variables and it needs access to the Keyboard Class to do it.

Upvotes: 0

user2655904
user2655904

Reputation:

KeyboardEvent does not have properties like LEFT or RIGHT, what you're looking for is Keyboard.LEFT/RIGHT. Like:

if (event.keyCode == Keyboard.LEFT){
   blueBox.x -=5
}
if (event.keyCode == Keyboard.RIGHT){
    blueBox.x +=5
}

Documentation for Keyboard: Adobe Documentation

Upvotes: 1

Related Questions