I made a simple little program to move a movieclip around, but it doesn’t work, and for the life of me I can’t seem to figure out why. It’s so simple! The program runs with no errors but the movieclip won’t move. I put in a trace statement that doesn’t return anything, so it’s gotta be something the matter with my event listener.
package {
import flash.display.MovieClip;
import flash.display.Stage;
//our Engine class it extends MovieClip
public class Engine extends MovieClip
{
//our constructor function. This runs when an object of
//the class is created
public function Engine()
{
//create an object of our ship from the Ship class
var ourShip:Ship = new Ship();
//add it to the display list
stage.addChild(ourShip);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
}
}
}
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.*;
public class Ship extends MovieClip
{
public function Ship()
{
addEventListener(KeyboardEvent.KEY_DOWN, loop);
}
private function loop(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
this.x -= 2;
}
else if (event.keyCode == Keyboard.RIGHT)
{
this.x += 2;
}
else if (event.keyCode == Keyboard.UP)
{
this.y -= 2;
}
else if (event.keyCode == Keyboard.DOWN)
{
this.y += 2;
}
trace (event.keyCode);
}
}
}