forked from: 車が動くアニメーション

by hig_an forked from 車が動くアニメーション (diff: 33)
♥0 | Line 78 | Modified 2010-01-02 00:19:58 | MIT License
play

ActionScript3 source code

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

// forked from 9re's 車が動くアニメーション
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.Event
    
    public class AnimationExample extends Sprite
    {
        // 車のクラス
        private var _car:Car;
        
        public function AnimationExample() 
        {
            // 車のクラスのインスタンスを作る
            // 色は赤にする
            _car = new Car(0xd00000);
            _car.y = stage.stageHeight / 2; 
            _car.x = stage.stageWidth / 2;
            _car.buttonMode = true;
            _car.putGas(60);
            
            
            _car.addEventListener(MouseEvent.MOUSE_DOWN,mDown);
            _car.addEventListener(MouseEvent.MOUSE_UP,mUp);
            _car.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
            
            
            addChild(_car);        // 表示リストに追加
        }
        private function mDown(e:MouseEvent):void
        {
        		e.currentTarget.startDrag();
        }
        
        private function mUp(e:MouseEvent):void 
        {
            e.currentTarget.stopDrag();
        }
        
        private function enterFrameHandler(e:Event):void{
        		var tmp:int = _car.x - mouseX;
        		_car.putGas(tmp);
        		if(_car._gas == 100){
        			e.currentTarget.stopDrag();
        			_car.move();
        		}
        }
    }
}

import flash.display.Sprite;

class Car extends  Sprite {
    // ガソリンの量最初は入っていない = 0
    public var _gas:int = 0;
    
    // コンストラクタ
    // 製造時に車の色は決まる
    public function Car(_fillColor:uint) {
        // 描画
        _draw(_fillColor);
    }
    
    // ガソリンを入れる。
    public function putGas(_amount:int):void {
        if (_amount < 0) {     // もしガソリンの量が0以下だったら
            return;            // 何もしないで、ここで終了
        }
        
        _gas += _amount;       // $amountだけ_gasの量を増やす
    }
    
    // 車を動かす
    public function move():void {
        if (_gas > 10) {        // ガスの残量が10より大きい場合
            x += 5;             // x座標を5だけ増やす
        } else {                // ガスの量が10以下の場合
            x += _gas / 2;      // 残りのガスの量によって動きが変わる
        }
        
        _gas--;                 // 車が動くとガスは減る
        if (_gas < 0) {         // もしガスの量が0より小さいなら
            _gas = 0;           // ガスの量を0にセットする
        }                       // ガスの量はマイナスにはならない
    }
    
    // 描画メソッド。privateなので外からは見えない
    // 車の形や色は製造される時に決まってしまうので外部からは
    // 呼べない
    private function _draw(_fillColor:uint):void
    {
        // 車輪を書く
        graphics.beginFill(0x333333);
        graphics.drawCircle(10, 20, 5);
        graphics.drawCircle(45, 20, 5);
        graphics.endFill();
        // 車体を書く
        graphics.beginFill(_fillColor);
        graphics.moveTo(0, 10);
        graphics.lineTo(15, 0);
        graphics.lineTo(30, 0);
        graphics.lineTo(40, 10);
        graphics.lineTo(50, 10);
        graphics.lineTo(55, 15);
        graphics.lineTo(55, 20);
        graphics.lineTo(0, 20);
        graphics.endFill();
    }
}

Forked