as1.0(贪食蛇)

      Flash 2005-6-14 15:30

Flash 动画

sinit = function () {
    timeline = _root;
    Xp = 100;
    Yp = 100;
    speed = 9;
    bodynum = 0;
    KeyEvent = this;
    i = 1;
    duplicateMovieClip(body, "body0", 0);
};
sinit.prototype.headPosition = function() {
    var obj = timeline.head;
    obj._x = Xp;
    obj._y = Yp;
    KeyEvent.onKeyDown = function() {
        if (Key.isDown(Key.LEFT) && obj._rotation<>90) {
            obj._rotation = -90;
        }
        if (Key.isDown(Key.UP) && obj._rotation<>180) {
            obj._rotation = 0;
        }
        if (Key.isDown(Key.RIGHT) && obj._rotation<>-90) {
            obj._rotation = 90;
        }
        if (Key.isDown(Key.DOWN) && obj._rotation<>0) {
            obj._rotation = 180;
        }
    };
    Key.addListener(KeyEvent);
    timeline.head.onEnterFrame = function() {
        if ((this._x+this._width)>=550 or this._x-this._width<=0 or (this._y+this._width)>=400 or (this._y-this._width)<=0) {
            xs = 0;
            ys = 0;
            delete this.onEnterFrame;
        }
        P = int(this._rotation/90);
        if (P == 2) {
            xs = 0;
            ys = speed;
        }
        if (P == 1) {
            xs = speed;
            ys = 0;
        }
        if (P == -1) {
            xs = -speed;
            ys = 0;
        }
        if (P == 0) {
            xs = 0;
            ys = -speed;
        }
        KeyEvent.bodyPosition(this._x, this._y);
        this._x += xs;
        this._y += ys;
        if (timeline.food.hitTest(this._x, this._y, true)) {
            timeline.food._x = random(500)+20;
            timeline.food._y = random(350)+20;
            addbody();
        }
        for (var i = 1; i<=bodynum; i++) {
            if (head.hitTest(timeline["body"+i]._x, timeline["body"+i]._y, true)) {
                xs = 0;
                ys = 0;
                delete this.onEnterFrame;
            }
        }
    };
};
sinit.prototype.bodyPosition = function(x, y) {
    this.x = x;
    this.y = y;
    timeline["body0"]._x = this.x;
    timeline["body0"]._y = this.y;
    for (var i = bodynum; i>=1; i--) {
        timeline["body"+i]._x = timeline["body"+(i-1)]._x;
        timeline["body"+i]._y = timeline["body"+(i-1)]._y;
    }
};
MovieClip.prototype.addbody = function() {
    duplicateMovieClip(body, "body"+i, i);
    i++;
    bodynum = i;
};
var myob = new sinit();
myob.headPosition();

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}