How would I got about doing this? I’ve got 3 classes, the document class(Main.as), the Main Menu Class (TitleScreen.as) and the gameplay class(GameScreen.as). I’m trying to add movement to the GameScreen.as class using eventListeners, but it’s just not working. I can’t for the life of me figure it out.
Here’s the GameScreen Class
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
public class GameScreen extends MovieClip
{
public static const PAUSE:String = "pause";
public static const WIN:String = "win";
public static const LOSE:String = "lose";
public var rightKey:Boolean = false;
public var leftKey:Boolean = false;
public var escKey:Boolean = false;
public var movementX:int = 25;
public var shipMC:Ship;
public function GameScreen()
{
this.addEventListener( KeyboardEvent.KEY_DOWN, keyPressed );
this.addEventListener( KeyboardEvent.KEY_UP, keyReleased );
trace("GameScreen")
shipMC = new Ship();
shipMC.x = 316;
shipMC.y = 487;
addChild( shipMC );
this.addEventListener( Event.ENTER_FRAME, animate);
this.addEventListener( MouseEvent.MOUSE_MOVE, crosshair );
}
public function keyPressed (e:KeyboardEvent):void
{
trace("testKey")
switch (e.keyCode)
{
case Keyboard.LEFT:
leftKey = true;
break;
case Keyboard.RIGHT:
rightKey = true;
break;
case Keyboard.ESCAPE:
escKey = true;
break;
}
}
public function keyReleased (e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
leftKey = false;
break;
case Keyboard.RIGHT:
rightKey = false;
break;
case Keyboard.ESCAPE:
escKey = false;
break;
}
}
public function animate (e:Event)
{
if ( leftKey )
{
shipMC.x += -movementX;
trace("left pressed")
}
if ( rightKey )
{
shipMC.x += movementX;
trace("right pressed")
}
if (escKey)
{
trace("Game Pause")
}
}
public function crosshair (e:Event):void
{
//mcCrosshair.gotoAndPlay(1);
Mouse.hide();
mcCrosshair.x = mouseX;
mcCrosshair.y = mouseY;
}
}
}
Sorry if I’m not providing enough information. I’m fairly new to Actionscript. Any help is appreciated.
Thanks