Today we looked at a smoother way to move the 'Ship' around the stage.
var moveRight:Boolean = false
var moveLeft:Boolean = false
var moveUp:Boolean = false
var moveDown:Boolean = false
//First we made a variable for each key we will use for movement, then we set the boolean (a statement that can only be true or false) to false
stage.addEventListener(Event.ENTER_FRAME,moveShip);
//We then listen to the frame and set up a function called moveShip
function moveShip(event:Event) {
if (moveRight==true) {
ship.x+=3;
}
//We then tell it that if move right becomes true, move right, this is repeated with the other 3 directions.
if (moveLeft==true) {
ship.x-=3;
}
if (moveUp==true) {
ship.y-=3;
}
if (moveDown==true) {
ship.y+=3;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,pressKey);
//A new function for when a key is pressed down
stage.addEventListener(KeyboardEvent.KEY_UP,stopShip);
//A new function for when a key is not being pressed
function stopShip(myevent:KeyboardEvent):void{
moveLeft=false;
moveRight=false;
moveUp=false;
moveDown=false;
}
//When stopShip is happening (no keys being pressed) all the move values in function moveShip are false, therefore there is no movement
function pressKey(myevent:KeyboardEvent):void{
if(myevent.keyCode==Keyboard.RIGHT){
moveRight=true;
}
//If the right key is pressed then the value of moveRight in moveShip becomes true and it will move. This is repeated for the other keys.
if(myevent.keyCode==Keyboard.LEFT){
moveLeft=true;
}
if(myevent.keyCode==Keyboard.UP){
moveUp=true;
}
if(myevent.keyCode==Keyboard.DOWN){
moveDown=true;
}
}
To add a file to the stage from the library is quite simple but first when your making the movie clip you have to go advanced settings we clicked 'export for ActionScript' and called it 'Ship' and set the class to 'Ship', we then deleted it from the stage.
var ship:MovieClip=new Ship();
//This basically pulls the thing in the library called Ship and puts it on the stage, renaming it ship
ship.x=275
ship.y=200
//we then positioned the ship in the middle of the stage
addChild(ship);
//then we added the final bit of code.
No comments:
Post a Comment