In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: ballistic projectile, range set by clicking

// forked from John_Blackburne's ballistic projectile, range set by clicking
// forked from John_Blackburne's PB's ballistic code 
package
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.text.TextField; 
       	[SWF(backgroundColor="#000000", frameRate="30")]
	public class Main extends Sprite 
	{
     
		private var ball : Shape = new Shape;
		private var target : Shape = new Shape;
		private var velocity : Number = 10.0;
		private var vx : Number;
		private var vy : Number;
                private var tx:Number, ty:Number;
		private var gravity : Number = 10 / stage.frameRate;
                private var bHigh:Boolean = true;
                private var txt: TextField;
                        
		public function Main():void 
		{
                        stage.addEventListener(MouseEvent.MOUSE_DOWN, click);
                        txt = new TextField();
                        txt.width = txt.height = 465;
                        txt.textColor = 0xffffff;
                        addChild(txt);
			init();
		}
 
		private function init(_x:Number = 100, _y:Number = 200):void 
		{
			// entry point
			addEventListener(Event.ENTER_FRAME, update);
			// create a ball to throw
			// create the target
                        graphics.beginFill(0);
                        graphics.drawRect(0, 0, 600, 600);
                        
			target.graphics.lineStyle(2, 0xffff00);
			target.graphics.drawCircle( -5, -5, 10);
			target.x = tx = _x;
			target.y = ty = _y;
			addChild(target);
			ball.graphics.beginFill(0xffffff);
			ball.graphics.drawCircle(-5, -5, 10);
			ball.graphics.endFill();
			ball.x = 400.0;
			ball.y = ty;
			addChild(ball);
			// work out the angle to throw it at
			var dx : Number = tx - ball.x;   // try to make the ball land at (tx, ty)
			var angle : Number = 0.5 * Math.asin(gravity * dx / (velocity * velocity));
                        if (bHigh) {angle = (-0.5 * Math.PI) - angle};
                        bHigh = !bHigh;
			txt.text = "launch angle: " + String( 180 / Math.PI * angle);
			// work out the velocity x,y components to start at that angle
			vx = -Math.cos(angle) * velocity;
			vy = Math.sin(angle) * velocity;
			trace("launch velocity: ", vx, vy);
		}

		private function click(e:MouseEvent):void {
                    graphics.clear();
                    
                    init(e.stageX, e.stageY);
		}
 
		private function update(e:Event = null):void
		{
			// apply gravity
			vy += gravity;
			// move the ball
			ball.x += vx;
			ball.y += vy;
                        graphics.beginFill(0x505050, 0.3);
                        graphics.drawCircle(ball.x - 5, ball.y - 5, 10);
			// stop when we land on the ground
			if (ball.y >= ty)
			{
			trace("ball landed at:", ball.x, ball.y);
			removeEventListener(Event.ENTER_FRAME, update);
			ball.y = ty;
			}
		}
	}
}