grebs
12-19-2008, 04:37 PM
Hi, i am building a lunar lander game. I have set up the controls for the spaceship so that the user has to rotate the spaceship in order to use the thrust to fly in different directions. the rotation works fine and the thurst up works fine. The only problem is the spaceship is flying the wrong way, for example if you rotate the spaceship so the thruster is facing left then press up the spaceship goes left instead of right. i have written this code -
code:
--------------------------------------------------------------------------------
var xVel:Number = 0;
var yVel:Number = 0;
var rVel:Number = 0;
var thrust:Number = 0;
var gravity:Number = 1
stage.addEventListener(KeyboardEvent.KEY_DOWN, right);
function right(evt:KeyboardEvent):void{
if (evt.keyCode == Keyboard.RIGHT)
{
rVel = 10;
}
else if(evt.keyCode == Keyboard.LEFT)
{
rVel = -10;
}
else if (evt.keyCode == Keyboard.UP)
{
thrust = -0.2;
}
}
this.addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void{
spaceship.rotation += rVel;
var angle:Number = spaceship.rotation * Math.PI/180;
var xAcc:Number = Math.cos(angle) * thrust;
var yAcc:Number = Math.sin(angle) * thrust;
xVel += xAcc;
yVel += yAcc;
spaceship.x += yVel;
spaceship.y += xVel;
spaceship.y += gravity;
}
i have also uploaded the fla file if anyone wants to have a look. the code is a bit messy at the moment, sorry.
code:
--------------------------------------------------------------------------------
var xVel:Number = 0;
var yVel:Number = 0;
var rVel:Number = 0;
var thrust:Number = 0;
var gravity:Number = 1
stage.addEventListener(KeyboardEvent.KEY_DOWN, right);
function right(evt:KeyboardEvent):void{
if (evt.keyCode == Keyboard.RIGHT)
{
rVel = 10;
}
else if(evt.keyCode == Keyboard.LEFT)
{
rVel = -10;
}
else if (evt.keyCode == Keyboard.UP)
{
thrust = -0.2;
}
}
this.addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void{
spaceship.rotation += rVel;
var angle:Number = spaceship.rotation * Math.PI/180;
var xAcc:Number = Math.cos(angle) * thrust;
var yAcc:Number = Math.sin(angle) * thrust;
xVel += xAcc;
yVel += yAcc;
spaceship.x += yVel;
spaceship.y += xVel;
spaceship.y += gravity;
}
i have also uploaded the fla file if anyone wants to have a look. the code is a bit messy at the moment, sorry.