てすと

by kenta forked from forked from: flash on 2009-9-22 (diff: 16)
♥0 | Line 131 | Modified 2009-09-23 02:44:54 | 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/6K1z
 */

// forked from kenta's forked from: flash on 2009-9-22
// forked from andrew_croce's flash on 2009-9-22
package {
    import flash.display.Sprite;
    import flash.events.*;
    
    public class FlashTest extends Sprite {
        private var ball:Sprite = new Sprite();
        private var size:Number = 30;
	private var speed:Number = 10;
	private var accel:Number = .97;
	private var max:Number = 50;
	private var min:Number = -50;
		
	//distance between point clicked and center of ball
        private var xoffset:Number = new Number();
	private var yoffset:Number = new Number();
		
	private var xvel:Number = new Number();
	private var yvel:Number = new Number();
	private var xaccel:Number = accel;
	private var yaccel:Number = accel;
		
	//to record dragged ball positions
	private var xpath:Array = new Array();
	private var ypath:Array = new Array();
		
        
        public function FlashTest() {
            // write as3 code here..
            drawBall();
	    ball.addEventListener(MouseEvent.MOUSE_DOWN,_startDrag);
	    ball.addEventListener(MouseEvent.MOUSE_UP,_release);
            
        }
        private function drawBall()
		{
			ball.graphics.beginFill(0x990077,1);
			ball.graphics.drawCircle((stage.stageWidth/2),(stage.stageHeight/2),size/2);
			ball.graphics.endFill();
			this.stage.addChild(ball);

			graphics.beginFill(0x990077,1);
			graphics.drawCircle(0,0,size/2);
			graphics.endFill();

			graphics.beginFill(0x990077,1);
			graphics.drawCircle(stage.stageWidth,0,size/2);
			graphics.endFill();



		}
		
		private function _startDrag(e:MouseEvent = void)
		{
			
			xoffset = e.stageX - ball.x;
			yoffset = e.stageY - ball.y;
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,_drag);
		}
		
		private function _drag(e:MouseEvent = void)
		{
			//set ball position to event(mouse) position
			ball.x = e.stageX - xoffset;
			ball.y = e.stageY - yoffset;
			e.updateAfterEvent();
			ball.addEventListener(Event.ENTER_FRAME,_dragUpdate);
		}
		
		private function _release(e:MouseEvent = void)
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, _drag);
			throwBall();
		}
		
		private function _dragUpdate(e:Event = void)
		{
			//add new ball position to end of array, and drop the first position
			xpath.push(ball.x);
			if (xpath.length >= 5)
			{
				xpath = xpath.slice(1,5);
			}
			ypath.push(ball.y);
			if (ypath.length >= 5)
			{
				ypath = ypath.slice(1,5);
			}
			//trace("X "+ xpath);
			//trace("Y "+ ypath);
			
		}
		
		private function throwBall()
		{
			//calculate velocity based on last two values in path arrays
			xvel = xpath[3] - xpath[2];
			yvel = ypath[3] - ypath[2];
			
			if (xvel > max)
			{
				xvel = max;
			} else
			{
				if (xvel < min)
				{
					xvel = min;
				}
			}
			
			if (yvel > max)
			{
				yvel = max;
			} else
			{
				if (yvel < min)
				{
					yvel = min;
				}
			}
			
			this.stage.addEventListener(Event.ENTER_FRAME,_throwUpdate);
		}
		
		private function _throwUpdate(e:Event = void)
		{
			if ((ball.x) >= stage.stageWidth)
			{
				xvel *= -1;
			} else
			{
				if (ball.x   <= 0)
				{
					xvel *= -1;
				}
			}
			
			if ((ball.y)  >= stage.stageHeight)
			{
				yvel *= -1;
			} else
			{
				if ((ball.y )  <= 0)
				{
					yvel *= -1;
				}
			}
			
			if (((xvel + yvel) < .4) && ((xvel + yvel) > -.4))
			{
				xvel = 0;
				yvel = 0;
			}
		
			xvel *= accel;
			yvel *= accel;
			ball.x += xvel;
			ball.y += yvel;
			
			if (xvel == 0)
			{
				this.stage.removeEventListener(Event.ENTER_FRAME,_throwUpdate);
			}
		}

    }
}