In flash we made a two scripts. the first one is to control an object with out keyboard control and later with it.
Here is the code:
import flash.events.KeyboardEvent;
// set variables
var moveRight:Boolean = false
var moveLeft:Boolean = false
var moveUp:Boolean = false
var moveDown:Boolean = false
// tells the object where to go if true
stage.addEventListener(Event.ENTER_FRAME,moveship);
// allows the object to move without keyboard input
function moveship(event:Event) {
if (moveRight==true) {
ship.x+=3;
}
if (moveLeft==true) {
ship.x-=3;
}
if (moveUp==true) {
ship.y-=3;
}
if (moveDown==true) {
ship.y+=3;
}
}
// listen to keyboard being pressed
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
stage.addEventListener(KeyboardEvent.KEY_UP, stopship);
// if not pressed set move ship to false
function stopship(myevent:KeyboardEvent):void{
moveLeft=false;
moveRight=false;
moveUp=false;
moveDown=false;
}
// if pressed set move ship to true
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;
}
}
The second script is to pull an object from the library and place it on screen.
here is the code:
import flash.display.MovieClip;
var ship:MovieClip = new Ship();
ship.x=275
ship.y=200
addChild(ship);


No comments:
Post a Comment