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

Throwable Bouncing Ball

Get Adobe Flash player
by AlejoAsd 07 Jan 2010
    Embed
/**
 * Copyright AlejoAsd ( http://wonderfl.net/user/AlejoAsd )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2J6i
 */

package 
{
	import flash.display.Sprite;
    import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class  Throw extends Sprite
	{
	    private var ball:Sprite;
        private var vx:Number;
        private var vy:Number;
        private var bounce:Number = -0.7;
        private var gravity:Number = 1;
		private var xSaver:Number = 0;
		private var ySaver:Number = 0;
		private var friction:Number = 0.1;
        
        public function Throw()
        {
            ball = new Sprite();
            ball.graphics.beginFill(0xFF0000);
            ball.graphics.drawCircle(0, 0, 40);
            ball.graphics.endFill();
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
            
            addChild(ball);
            
            vx = Math.random() * 100 - 50;
            vy = -50;
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
			ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
        }
        
        private function onEnterFrame(event:Event):void
        {
            vy += gravity;
            ball.x += vx;
            ball.y += vy;
			
			//Ball "Friction"
			if(ball.y>stage.stageHeight-60){
			if (vx < -0.1)
				vx += friction;
			else if (vx > 0.1)
				vx -= friction;
			else if (vx < 0.1 || vx < -0.1)
				vx = 0;
			}
            
			//Border Checks
            if(ball.x + 40 > stage.stageWidth)
            {
                ball.x = stage.stageWidth - 40;
                vx *= bounce;
            }
            else if(ball.x - 40 < 0)
            {
                ball.x = 40;
                vx *= bounce;
            }
            if(ball.y + 40 > stage.stageHeight)
            {
                ball.y = stage.stageHeight - 40;
                vy *= bounce;
            }
            else if(ball.y - 40 < 0)
            {
                ball.y = 40;
                vy *= bounce;
            }
        
		}
		
		private function onMouseDown(e:MouseEvent):void {
			xSaver = ball.x;
			ySaver = ball.y;
			
			addEventListener (MouseEvent.MOUSE_UP, onMouseUp, false, 0, true);
			
			ball.startDrag();
			
			removeEventListener (Event.ENTER_FRAME, onEnterFrame);
			addEventListener (Event.ENTER_FRAME, CheckVelocity, false, 0, true);
		}
		
		private function onMouseUp(e:MouseEvent):void {
			removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			ball.stopDrag();
						
			removeEventListener(Event.ENTER_FRAME, CheckVelocity);
			addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
		}
		
		private function CheckVelocity(e:Event):void {
			vx = ball.x - xSaver;
			vy = ball.y - ySaver;
			xSaver = ball.x;
			ySaver = ball.y
		}
	}
}