Thursday, November 10, 2011

smooth keyboard movement





var moveRight:Boolean = false
move right false
var moveLeft:Boolean = false
move left false
var moveUp:Boolean = false
move up false
var moveDown:Boolean = false
move down false






import flash.events.Event;
import flash.events.KeyboardEvent;
insert an event called keyboard


stage.addEventListener(Event.ENTER_FRAME,moveShip);
listen for if the ship is moved


function moveShip(event:Event) {
if (moveRight==true)  {
ship.x+=3;
if right is true (pushed down) then move ship by 3
}
if (moveLeft==true)  {
ship.x-=3;
if left is true (pushed down) then move ship by 3
}
if (moveUp==true)  {
ship.y-=3;
if right is up (pushed down) then move ship by 3
}
if (moveDown==true)  {
ship.y+=3;
if right is down (pushed down) then move ship by 3
}

}



stage.addEventListener(KeyboardEvent.KEY_DOWN,pressKey);
stage.addEventListener(KeyboardEvent.KEY_UP,stopship);
listen to keyboard being pressed


function stopship(myevent:KeyboardEvent):void{
moveLeft=false;
moveRight=false;
moveUp=false
moveDown=false
if not pressed set move ship to false
}



function pressKey(myevent:KeyboardEvent):void{
if(myevent.keyCode==Keyboard.RIGHT){
moveRight=true;
}
if(myevent.keyCode==Keyboard.LEFT){
moveLeft=true;
}


    if(myevent.keyCode==Keyboard.UP){
moveUp=true;
}


if(myevent.keyCode==Keyboard.DOWN){
moveDown=true;
}

if pressed set move ship to true.
}

No comments:

Post a Comment