flash on 2009-9-29

by kenta
♥0 | Line 64 | Modified 2009-09-29 01:36:00 | MIT License
play

ActionScript3 source code

/**
 * Copyright kenta ( http://wonderfl.net/user/kenta )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kc34
 */

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	

	public class  main extends Sprite
	{
		private var ball:Sprite;
		private var oldX:Number;
		private var oldY:Number;
		private var vx:Number;
		private var vy:Number;
		private var flag:Boolean;
		
		
		public function main()
		{
			stage.frameRate = 60;
			
			ball = new Sprite;
			ball.graphics.beginFill(0x999999, 1);
			ball.graphics.drawCircle(0, 0, 10);
ball.graphics.endFill;
			this.addChild(ball);
			
			ball.addEventListener(Event.ENTER_FRAME, enterhandler);
			ball.addEventListener(MouseEvent.MOUSE_DOWN, downhandler);
			ball.addEventListener(MouseEvent.MOUSE_UP, uphandler );
						
			
		}
		
		private function downhandler(e:MouseEvent):void {
			oldX = this.mouseX;
			oldY = this.mouseY;
			
			ball.startDrag(true);
			flag = true;
		}
		
		private function uphandler(e:MouseEvent):void {
			
			vx = this.mouseX - oldX;
			vy = this.mouseY - oldY;
			
			ball.stopDrag();
			flag = false;
			
		}
		
		
		private function enterhandler(e:Event):void {
			
			if (flag) {
				return;
			}
			
			
			ball.x += vx;
			ball.y += vy;
			
			vx *= 0.95;
			vy *= 0.95;
			
			if (ball.x + ball.width/2 > stage.stageWidth) {
				
				ball.x = stage.stageWidth - ball.width/2;
				vx *= -1;
				
			}
			else if(ball.x - ball.width/2 < 0) {
				ball.x = 0 + ball.width / 2;
				vx *= -1;
				
			}
			
			if (ball.y + ball.height/2 > stage.stageHeight) {
				
				ball.y = stage.stageHeight - ball.height/2;
				vy *= -1;
				
			}
			else if(ball.y - ball.width/2 < 0) {
				ball.y = 0 + ball.height/2;
				vy *= -1;
				
			}			
			
			
			
			
			
		}		
		
		
		
		
		
		
		
		
		
		
		
	}
	
}