I’m starting a simple pong game, and I created a Paddle class that doesn’t do anything yet. However, I don’t think it is working.
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class Paddle extends MovieClip
{
private var paddleSpeed:int = 4;
public function Paddle()
{
trace("hello!")
addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
}
public function keyDown(e:KeyboardEvent):void
{
trace(e.keyCode);
}
}
}
In Main.as I have done this:
var player:Paddle = new Paddle;
addChild(player);
When I run the code, I get hello!, but when I press any key nothing happens. I’ve read a KeyboardEvent tutorial, and I’m doing what they are doing. Thanks for any help
Try adding the event listener to
stageinstead ofPaddle. Also, when you’re testing your project make sure you have keyboard shortcuts disabled.What I actually normally do is make a
Keyboardclass with adown()function that I can reference throughout the application. Its use would be something like this: