刚刚写了一部分
import mx.transitions.easing.*;
import game.Hero;
class game.food.Food {
//指定路径
public var _path:MovieClip;
//移动速度,用时间来控制,在子类中指定
private var _time:Number;
//默认的运动方式
private var _func:Function = None.easeOut;
//游戏主角
private var _hero:MovieClip;
//场景的尺寸
private var _scenex:Number = Stage.width;
private var _sceney:Number = Stage.height;
public var _id:String;
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
//与主角保持联系
public function connectToHero(h:Hero) {
_hero = h.getHero();
}
//创建一个食物
public function create(id:String, name:String):Void {
var depth = _path.getNextHighestDepth();
var f:MovieClip = _path.attachMovie(id, name+depth, depth);
f._x = (depth%3 == 0) ? random(100)+_scenex : random(100)-100;
f._y = random(_sceney-100)+50;
move(f);
}
//运动方式,由子类实现
public function move(obj:MovieClip):Void {
}
}
import game.food.Food;
import mx.transitions.Tween;
import mx.events.EventDispatcher;
class game.food.FirFood extends Food {
//食物在库中的链接名
public var _id:String = "f_0_0";
//食物的新实例名
private var _name:String = "ok";
public function FirFood() {
EventDispatcher.initialize(this);
}
function create() {
super.create(_id, _name);
}
function move(obj) {
var _time:Number = random(20)+5;
var _end:Object = {x:random(_scenex), y:random(50)-50+obj._y};
obj._xscale = (_end.x>=obj._x) ? 100 : -100;
var tween1 = new Tween(obj, "_x", _func, obj._x, _end.x, _time, true);
var tween2 = new Tween(obj, "_y", _func, obj._y, _end.y, _time, true);
var ins = this;
tween1.onMotionFinished = function() {
ins.move(obj);
};
tween1.onMotionChanged = function() {
if (ins._hero.hitTest(obj)) {
obj.removeMovieClip();
ins.create(_id, _name);
ins.dispatchEvent({type:"onEat", target:obj});
}
};
}
}
import game.food.Food;
import mx.transitions.Tween;
import mx.events.EventDispatcher;
class game.food.SecFood extends Food {
//食物在库中的链接名
public var _id:String = "f_0_1";
//食物的新实例名
private var _name:String = "ok";
public function SecFood() {
EventDispatcher.initialize(this);
}
function create() {
super.create(_id, _name);
}
//本想用不同的动画效果,懒得写,从上面那个复制过来算了
function move(obj) {
//根据不同的情况设置不同的速度,以增加难度
var _time:Number = random(10)+5;
var _end:Object = {x:random(_scenex), y:random(50)-50+obj._y};
obj._xscale = (_end.x>=obj._x) ? 100 : -100;
var tween1 = new Tween(obj, "_x", _func, obj._x, _end.x, _time, true);
var tween2 = new Tween(obj, "_y", _func, obj._y, _end.y, _time, true);
var ins = this;
tween1.onMotionFinished = function() {
ins.move(obj);
};
tween1.onMotionChanged = function() {
if (ins._hero.hitTest(obj)) {
ins.dispatchEvent({type:"onHit", target:obj});
}
};
}
}
class game.Hero {
private var _hero:MovieClip;
private var _life:Number = 100;
private var _moveID:Number;
private var scene_width:Number = Stage.width;
private var scene_height:Number = Stage.height;
function Hero() {
}
function create(path:MovieClip, name:String,