Here it is --->
import flash.events.KeyboardEvent;
import flash.events.Event;
stop();
stage.focus = this;
var isRightKeyPressed:Boolean = false;
var isLeftKeyPressed:Boolean = false;
const TROLLEY_SPEED:Number = 8;
stage.addEventListener(KeyboardEvent.KEY_DOWN, isKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, isKeyReleased);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function gameLoop(event:Event):void
{
moveTrolley();
}
function isKeyPressed(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
isRightKeyPressed = true;
}
if(event.keyCode == Keyboard.LEFT)
{
isLeftKeyPressed = true;
}
}
function isKeyReleased(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
isRightKeyPressed = false;
}
if(event.keyCode == Keyboard.LEFT)
{
isLeftKeyPressed = false;
}
}
function moveTrolley():void
{
if(isRightKeyPressed == true)
{
trolley_mc.x += TROLLEY_SPEED;
}
if(isLeftKeyPressed == true)
{
trolley_mc.x -= TROLLEY_SPEED;
}
}
I have also used the following code to make my Home Screen compatible, with 2 buttons (the Play button and the Instructions button)
Heres that code -->
import flash.events.MouseEvent;
stop ();
start_btn.addEventListener(MouseEvent.CLICK, startBtnClicked);
instructions_btn.addEventListener(MouseEvent.CLICK, instructionsBtnClicked);
function startBtnClicked(event:MouseEvent):void{
gotoAndStop(4);}
function instructionsBtnClicked(event:MouseEvent):void
{
gotoAndStop(3);
}
No comments:
Post a Comment