2005/05/11 | [练习]as2.0-键盘控制物体缓冲移动
类别(flash学习) | 评论(8) | 阅读(752) | 发表于 16:50
//-----code by ycccc8202-------
//-----data 2005_5-------
//-----class SmoothMove----
class SmoothMove extends MovieClip {
    private var xspeed:Number = 0;
    private var yspeed:Number = 0;
    private var key:Object;
    private var x:Number;
    private var y:Number;
    private var left:Number;
    private var right:Number;
    private var up:Number;
    private var down:Number;
    private var press:Boolean;
    private var speed_up:Number = 1.1;
    private var speed_down:Number = .9;
    private var speed_add:Number;
    private var temp_test:MovieClip;
    public function SmoothMove(l, r, u, d, speed) {//参数分别是左右上下的位置及移动的初始速度
        super();
        if (speed == undefined or speed == null) {
            this.speed_add = 2;
        } else {
            this.speed_add = speed;
        }
        this.temp_test = this._parent.createEmptyMovieClip("temp", this._parent.getNextHighestDepth());
        this.left = l;
        this.right = r;
        this.up = u;
        this.down = d;
        this.ConTrol();
        this.KeyUp();
    }
    private function SpeedDown() {
        this.onEnterFrame = function() {
            this.xspeed *= this.speed_down;
            this.yspeed *= this.speed_down;
            this._x += this.xspeed;
            this._y += this.yspeed;
            //trace("-------"+this.xspeed+"-------"+this.yspeed);
            if (Math.abs(this.xspeed)<1 and Math.abs(this.yspeed)<1) {
                if (Key.getCode() == 37 or Key.getCode() == 39) {
                    trace("left_____right");
                    this.xspeed = speed_add;
                    this.yspeed = 0;
                } else if (Key.getCode() == 38 or Key.getCode() == 40) {
                    trace("up_____down");
                    this.xspeed = 0;
                    this.yspeed = speed_add;
                }
                delete this.onEnterFrame;
            }
            if (this._x<left) {
                this._x = left;
                delete this.onEnterFrame;
            }
            if (this._x>right) {
                this._x = right;
                delete this.onEnterFrame;
            }
            if (this._y<up) {
                this._y = up;
                delete this.onEnterFrame;
            }
            if (this._y>down) {
                this._y = down;
                delete this.onEnterFrame;
            }
        };
    }
    private function SpeedUp() {
        if (Key.isDown(Key.LEFT)) {
            if (!this.press) {
                //trace(this.press+"___________");
                delete this.onEnterFrame;
                this.press = true;
                this.xspeed = -speed_add;
                this.yspeed = 0;
            }
            this.x = this._x;
            if (this.x>left) {
                this.x += (this.xspeed *= speed_up);
                if (this.x<left) {
                    this._x = left;
                    this.press = false;
                } else {
                    this._x = this.x;
                }
            }
        } else if (Key.isDown(Key.RIGHT)) {
            if (!this.press) {
                delete this.onEnterFrame;
                this.press = true;
                this.xspeed = speed_add;
                this.yspeed = 0;
            }
            this.x = this._x;
            if (this.x<right) {
                this.x += (this.xspeed *= speed_up);
                if (this.x>right) {
                    this._x = right;
                    this.press = false;
                } else {
                    this._x = this.x;
                }
            }
        } else if (Key.isDown(Key.UP)) {
            if (!this.press) {
                delete this.onEnterFrame;
                this.press = true;
                this.yspeed = -speed_add;
                this.xspeed = 0;
            }
            this.y = this._y;
            if (this.y>up) {
                this.y += (this.yspeed *= speed_up);
                if (this.y<up) {
                    this._y = up;
                    this.press = false;
                } else {
                    this._y = this.y;
                }
            }
        } else if (Key.isDown(Key.DOWN)) {
            if (!this.press) {
                delete this.onEnterFrame;
                this.press = true;
                this.yspeed = speed_add;
                this.xspeed = 0;
            }
            this.y = this._y;
            if (this.y<down) {
                this.y += (this.yspeed *= speed_up);
                if (this.y>down) {
                    this._y = down;
                    this.press = false;
                } else {
                    this._y = this.y;
                }
            }
        }
    }
    private function ConTrol() {
        var This = this;
        temp_test.onEnterFrame = function() {
            This.SpeedUp();
        };
    }
    private function KeyUp() {
        var This = this;
        key = {};
        key.onKeyUp = function() {
            This.press = false;
            This.SpeedDown();
        };
        Key.addListener(key);
    }
}
//---------使用---------
Object.registerClass("mc", SmoothMove);
this.attachMovie("mc", "mc", getNextHighestDepth(), {_x:200, _y:200}).__constructor__(0, Stage.width, 0, Stage.height);
//库中新建个元件使其连接命为"mc"
//效果如下:


Flash 动画
0

评论Comments

日志分类
首页[38]
flash学习[35]
图片收藏[1]
Apollo_Flex[2]