flash on 2009-12-14

by pasodania
♥0 | Line 158 | Modified 2009-12-14 17:46:42 | MIT License
play

ActionScript3 source code

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

package {
	import flash.events.*;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
    		private var ball:Sprite;
    	
    		public var vx:Number = 3;
    		public var vy:Number = 2;
    		
    		public var ax:Number;
    		public var ay:Number;
    		
    		public var speed:Number;
    		public var angle:Number;
    		public var radians:Number;
    		public var gravity:Number;
    		public var bounce:Number;
    		public var bottom:Number;
    		public var friction:Number;
    		
    		public var left:Number;
		public var top:Number;
		public var right:Number;
		public var oldx:Number;
		public var oldy:Number;
		public var k:Number;
    		
    		private var _initFlag:Boolean = false;
    		private var _dragging:Boolean = false;
        public function FlashTest() {
            // write as3 code here..
            ball = new Sprite();
            ball.graphics.beginFill(0xFF0000);
            ball.graphics.drawCircle(0, 0, 10);
            ball.graphics.endFill();
            addChild(ball);
            ball.x = 100;
            ball.y = 100;
           
            addEventListener(Event.ENTER_FRAME, onEnterFrame7);
        }
        
        public function onEnterFrame1(e:Event):void{
        		// 簡単なアニメーション
        		ball.x += vx;
        		ball.y += vy;
        }
        
        public function onEnterFrame2(e:Event):void{
        		// 角(運動)速度からX,Y速度へ
        		speed = 1;
        		angle = 45;
        		
        		radians = angle * Math.PI / 180;
        		vx += Math.cos(radians) * speed;
        		vy += Math.sin(radians) * speed;
        		
        		ball.x += vx;
        		ball.y += vy;
        }
        
        public function onEnterFrame3(e:Event):void{
        		// 加速
        		ax = .1;
        		ay = .2;
        		vx += ax;
        		vy += ay;
        		ball.x += vx;
        		ball.y += vy;
        }
        
        public function onEnterFrame4(e:Event):void{
        		// Gravity
        		vx = 2;
        		gravity = .95;
        		
        		vy += gravity;
        		ball.x += vx;
        		ball.y += vy;
        }
        
        public function onEnterFrame5(e:Event):void{
        		// 弾性と摩擦
        		if(!_initFlag){
        			vx = 3;
        			vy = -5;
        			gravity = .6;
        			bounce = -.7;
        			bottom = 400;
        			friction = .95;
        			_initFlag = true;
        		}
        		vy += gravity;
        		vy *= friction;
        		vx *= friction;
        		ball.x += vx;
        		ball.y += vy;
        		if( ball.y + ball.height/2 > bottom ){
        			ball.y = bottom - ball.height/2;
        			vy *= bounce;
        		}
        }
        
        public function onEnterFrame6(e:Event):void{
        	    // ボールを投げる
        		if(!_initFlag){
        			vx = 3;
				vy = -5;
				gravity = .6;
				bounce = -.7;
				friction = .99;
		
				left = 0;
				top = 0;
				right = 400;
				bottom = 400;
				_dragging = false;
				oldx = 0;
				oldy = 0;
				
				var onDrag:Function = function(e:Event):void{
					_dragging = true;
					ball.startDrag();
					stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
				}
				var onDrop:Function = function(e:Event):void{
					stage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
					_dragging = false;
					ball.stopDrag();
				}
				ball.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
				_initFlag = true;
        		}
        		
     		if(!_dragging){
  				vy += gravity;
    				vy *= friction;
			    vx *= friction;
    				ball.x += vx;
    				ball.y += vy;
    				if(ball.x + ball.width/2 > right){
      				ball.x = right - ball.width/2;
      				vx *= bounce;
    				}else if(ball.x - ball.width/2 < left){
     	 			ball.x = left + ball.width/2;
      				vx *= bounce;
    				}else if(ball.y - ball.height/2 < top){
      				ball.y = top + ball.height/2;
      				vy *= bounce;
    				}else if(ball.y+ball.height/2 > bottom){
      				ball.y = bottom - ball.height/2;
      				vy *= bounce;
    				}
     		} else {
       			vx = ball.x - oldx;
    				vy = ball.y - oldy;
    				oldx = ball.x;
    				oldy = ball.y;
     		}
        }
        
        public function onEnterFrame7(e:Event):void{
        		// バネ
        		if(!_initFlag){
				vx = 3;
				vy = -5;
				k = .2;

				gravity = 6;
				friction = .9;
        			_initFlag = true;
        		}
			var dx:Number = mouseX - ball.x;
			var dy:Number = mouseY - ball.y;
			var ax:Number = k * dx; 
			var ay:Number = k * dy;
  
			vx += ax;
			vy += ay;
			vy += gravity
  
			vx *= friction;
			vy *= friction;
  
			ball.x += vx;
			ball.y += vy;
        }
    }
    
    
}