Orbit
@paulstamp
This example shows basic planetary orbit
/**
* Copyright paulstamp1 ( http://wonderfl.net/user/paulstamp1 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3HI7
*/
/**
_____ _____ _____ __ _____ _____ _____ _____ _____
| _ | _ | | | | | __|_ _| _ | | _ |
| __| | | | |__ |__ | | | | | | | | __|
|__| |__|__|_____|_____| |_____| |_| |__|__|_|_|_|__|
@paulstamp
This example shows basic planetary orbit
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var _planetA:Planet;
private var _planetB:Planet;
private var _clockwise:Boolean;
private var _orbitHeight:Number = 30;
private var _speed:Number = 2;
public function FlashTest() {
// write as3 code here..
_planetA = new Planet( 20 );
_planetA.x = 465>>1;
_planetA.y = 465>>1;
addChild( _planetA );
_planetB = new Planet( 10 );
addChild( _planetB );
addEventListener( Event.ENTER_FRAME, update );
}
private function update( event:Event ):void
{
if( _clockwise )
{
_planetB.rotation += _speed;
}
else
{
_planetB.rotation -= _speed;
}
var rad:Number = _planetB.rotation * Math.PI / 180;//degrees to rads
var radius:Number = 30 + _orbitHeight;
var difX:Number = _planetA.x - _planetB.x;
var difY:Number = _planetA.y - _planetB.y;
var velocityX:Number = difX + radius * Math.cos(rad);
var velocityY:Number = difY + radius * Math.sin(rad);
_planetB.x += velocityX;
_planetB.y += velocityY;
}
}
}
import flash.display.Sprite;
class Planet extends Sprite
{
public var radius:Number;
public function Planet( radius:Number )
{
this.radius = radius;
this.graphics.beginFill( 0x009900 );
this.graphics.drawCircle( 0,0, radius );
this.graphics.endFill();
}
}