Code in frame 1 layer 1
//orbit match
//orbit.fla
//ship must link up with orbiting sattelite
//By Andy Harris, Dummies Guide to Game Programming in Flash
init();
function init(){
//create the ship
myShip.dx = 3;
myShip.dy = 0;
myShip.speed = 0;
myShip.dir = 0;
myShip.gotoAndStop("still");
planet.gravity = 1000;
//set up drawing
_root.lineStyle(.5,0xffffff,100);
_root.moveTo(myShip._x, myShip._y);
sat.dx = 3;
sat.dy = 0;
sat.speed = 0;
sat.dir = 0;
} // end init
myShip.onEnterFrame = function(){
myShip.checkKeys();
myShip.turn();
myShip.move();
myShip.gravitate(planet)
} // end enter frame
sat.onEnterFrame = function(){
//myShip.turn();
sat.move();
sat.gravitate(planet)
} // end enter frame
myShip.checkKeys = function(){
//check for left and right arrows
if (Key.isDown(Key.LEFT)){
this.dir -= 10;
this.gotoAndStop("left");
if (this.dir < 0){
this.dir = 350;
} // end if
} else if (Key.isDown(Key.RIGHT)){
this.dir += 10;
this.gotoAndStop("right");
if (this.dir > 360){
this.dir = 10;
} // end if
} else if (Key.isDown(Key.UP)){
//thrust on up arrow
this.thrustSpeed = .1;
this.gotoAndPlay("thrust");
} else {
this.thrustSpeed = 0;
this.gotoAndStop("still");
} // end if
if (Key.isDown(Key.SPACE)){
//clear drawings
_root.clear();
//restart drawing
_root.moveTo(myShip._x, myShip._y);
_root.lineStyle(.5,0xffffff,100);
} // end if
} // end checkKeys
myShip.turn = function(){
this._rotation = this.dir;
//get new thrust vector
degrees = this.dir
degrees -= 90;
radians = degrees * Math.PI / 180;
thrustDX = this.thrustSpeed * Math.cos(radians);
thrustDY = this.thrustSpeed * Math.sin(radians);
//add thrust to dx and dy
this.dx += thrustDX;
this.dy += thrustDY;
} // end turn
myShip.move = function(){
this._x += this.dx;
myShip._y += this.dy;
_root.lineTo(this._x, this._y);
} // end move
myShip.gravitate = function(focus){
//pull this element to the focus object
//assumes focus object has a gravity property
//figure angle difference between ship and planet
tempDX = this._x - focus._x;
tempDY = this._y - focus._y;
//calculate distance between ship and planet
tempDistance = Math.sqrt(tempDX * tempDX + tempDY * tempDY);
//normalize vector (make it length of one)
tempDX /= tempDistance;
tempDY /= tempDistance;
//compensate for the planet’s gravitational pull
tempDX *= focus.gravity / (tempDistance * tempDistance);
tempDY *= focus.gravity / (tempDistance * tempDistance);
//invert the vector so it pulls ship to planet
tempDX *= -1;
tempDY *= -1;
//add vector to ship
this.dx += tempDX;
this.dy += tempDY;
} // end gravitate
sat.move = function(){
sat._x += sat.dx;
sat._y += sat.dy;
checkRendezvous();
} // end function
sat.gravitate = function(focus){
//pull this element to the focus object
//assumes focus object has a gravity property
//figure angle difference between ship and planet
tempDX = this._x - focus._x;
tempDY = this._y - focus._y;
//calculate distance between ship and planet
tempDistance = Math.sqrt(tempDX * tempDX + tempDY * tempDY);
//normalize vector (make it length of one)
tempDX /= tempDistance;
tempDY /= tempDistance;
//compensate for the planet’s gravitational pull
tempDX *= focus.gravity / (tempDistance * tempDistance);
tempDY *= focus.gravity / (tempDistance * tempDistance);
//invert the vector so it pulls ship to planet
tempDX *= -1;
tempDY *= -1;
//add vector to ship
this.dx += tempDX;
this.dy += tempDY;
} // end gravitate
function checkRendezvous(){
//checks to see if the ship and the sat have rendezvoused
//use distance formula
relX = myShip._x - sat._x;
relY = myShip._y - sat._y;
distance = Math.sqrt((relX * relX) + (relY * relY));
if (distance < 5){
trace("rendezvous!!");
} // end if
relDX = myShip.dx - sat.dx;
relDY = myShip.dy - sat.dy;
deltaV = Math.sqrt((relDX * relDX) + (relDY * relDY));
//report stats
txtDistance.text = distance;
txtDeltaV.text = deltaV;
} // end checkrendezvous