Reputation: 1
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
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
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
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