練習作品1_自由落下

by imasashi forked from 円を動かす (diff: 42)
自由落下
 クリックで上へ跳ね上がる
♥0 | Line 53 | Modified 2010-02-15 01:02:26 | MIT License
play

ActionScript3 source code

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

// forked from 9re's 円を動かす
// 自由落下
// クリックで上へ跳ね上がる
package {
    import flash.display.*;
    import flash.events.Event;
    import flash.events.MouseEvent;
 
    [SWF(frameRate="50", width="465", height="465")]
  
    public class MyFirstAnimation extends Sprite { 
        private var _circle:Circle;
 
        public function MyFirstAnimation() {
            // クラスCircleのインスタンスを作る
            _circle = new Circle(5);
            _circle.vx = 4;
            _circle.vy = 0; //初期値
            _circle.ay = 1; //初期値            
            _circle.x = 0; //初期位置
            _circle.y = 0; //初期位置
            
            addChild(_circle);
 
            // 1フレーム毎に実行する処理にenterFrameHandlerを追加する
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.CLICK, onClick);
        }
 
        // フレーム毎に行われる処理
        private function enterFrameHandler(e:Event):void {
            // 1フレーム分動かす
            _circle.move();
            if(_circle.y > stage.stageHeight-2){
            		_circle.vy = -_circle.vy*0.9;
            		_circle.y = stage.stageHeight-2;
            	}
            	if(_circle.x > stage.stageWidth){
            		_circle.x = stage.stageWidth;
            		_circle.vx = -_circle.vx*0.9;
            	} else if (_circle.x < 0 ){
            		_circle.x = 0;
            		_circle.vx = -_circle.vx;
            	}
        }
        // クリックで行われる処理
        private function onClick(e:MouseEvent):void {
        		_circle.vy = -10;
        	}
    }
}
 
import flash.display.Sprite;

class Circle extends Sprite {
	public var vx:Number;
    public var vy:Number;
    public var ay:Number;
    // コンストラクタ
    public function Circle(_radius:Number, _fillColor:uint = 0x000000) {
        // 塗り_fillColor, 半径_radiusの円
        graphics.beginFill(_fillColor);
        graphics.drawCircle(0, 0, 2);
        graphics.endFill();
    }
    // 1フレーム分の動き
    public function move():void {
    		vy += ay;
    		y += vy;
    		x += vx;
    }
}