Wednesday, 28 September 2011

Basic navigation coding

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveship);

function moveship(myevent:KeyboardEvent):void {
if (myevent.keyCode==Keyboard.RIGHT){
ship.x+=5;
ship.rotation+=5
if (ship.rotation>=90){
ship.rotation=90;
//This is an example of coding that allowed me to rotate the ship alot more smoothly. The coding above is done so the ship will turn until it reaches 90 degrees and then stops turning so the ship can carry on moving right without rotating.
}

}
// These coding's below are variations of the coding above, but now for the other arrow keys and their rotation. These were part of experimentation. After the first coding of the RIGHT key was shown to us I decided to experiment with what I learnt to finish the rest of the directional keys. I attempted to have all directional rotations to rotate to their correct position and then stop but I could only get the RIGHT KEY to do this.

if (myevent.keyCode==Keyboard.LEFT) {
ship.x-=5;
ship.rotation-=5
if (ship.rotation>=270){
ship.rotation=270;

}

}

if (myevent.keyCode==Keyboard.DOWN) {
ship.y+=5;
ship.rotation-=5
if (ship.rotation>=180){
ship.rotation=180;
}
}

if (myevent.keyCode==Keyboard.UP) {
ship.y-=5;
ship.rotation=0;
}

}

stage.addEventListener(Event.ENTER_FRAME,bounds);

function bounds(event:Event) {
if (ship.x>=550) {
ship.x=0;
// This allowed me to set a boundry for the stage, so when the ship would pass outside the stage boundary's it would appear out the opposite side of the stage. 
}



}

No comments:

Post a Comment