刚刚写了一部分
importmx.transitions.easing.*;
importgame.Hero;
classgame.food.Food{
//指定路径
publicvar_path:MovieClip;
//移动速度,用时间来控制,在子类中指定
privatevar_time:Number;
//默认的运动方式
privatevar_func:Function=None.easeOut;
//游戏主角
privatevar_hero:MovieClip;
//场景的尺寸
privatevar_scenex:Number=Stage.width;
privatevar_sceney:Number=Stage.height;
publicvar_id:String;
publicvaraddEventListener:Function;
publicvarremoveEventListener:Function;
publicvardispatchEvent:Function;
//与主角保持联系
publicfunctionconnectToHero(h:Hero){
_hero=h.getHero();
}
//创建一个食物
publicfunctioncreate(id:String,name:String):Void{
vardepth=_path.getNextHighestDepth();
varf: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);
}
//运动方式,由子类实现
publicfunctionmove(obj:MovieClip):Void{
}
}
importgame.food.Food;
importmx.transitions.Tween;
importmx.events.EventDispatcher;
classgame.food.FirFoodextendsFood{
//食物在库中的链接名
publicvar_id:String="f_0_0";
//食物的新实例名
privatevar_name:String="ok";
publicfunctionFirFood(){
EventDispatcher.initialize(this);
}
functioncreate(){
super.create(_id,_name);
}
functionmove(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;
vartween1=newTween(obj,"_x",_func,obj._x,_end.x,_time,true);
vartween2=newTween(obj,"_y",_func,obj._y,_end.y,_time,true);
varins=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});
}
};
}
}
importgame.food.Food;
importmx.transitions.Tween;
importmx.events.EventDispatcher;
classgame.food.SecFoodextendsFood{
//食物在库中的链接名
publicvar_id:String="f_0_1";
//食物的新实例名
privatevar_name:String="ok";
publicfunctionSecFood(){
EventDispatcher.initialize(this);
}
functioncreate(){
super.create(_id,_name);
}
//本想用不同的动画效果,懒得写,从上面那个复制过来算了
functionmove(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;
vartween1=newTween(obj,"_x",_func,obj._x,_end.x,_time,true);
vartween2=newTween(obj,"_y",_func,obj._y,_end.y,_time,true);
varins=this;
tween1.onMotionFinished=function(){
ins.move(obj);
};
tween1.onMotionChanged=function(){
if(ins._hero.hitTest(obj)){
ins.dispatchEvent({type:"onHit",target:obj});
}
};
}
}
classgame.Hero{
privatevar_hero:MovieClip;
privatevar_life:Number=100;
privatevar_moveID:Number;
privatevarscene_width:Number=Stage.width;
privatevarscene_height:Number=Stage.height;
functionHero(){
}
functioncreate(path:MovieClip,name:String,depth:Number):Void{
_hero=path.attachMovie(name,"hero",depth);
_moveID=setInterval(this,"move",30);
}
functionmove(){
varspeed=4
if(Key.isDown(Key.LEFT)){
_hero._x-=speed;
_hero._xscale=-100;
}
if(Key.isDown(Key.RIGHT)){
_hero._x+=speed;
_hero._xscale=100;
}
if(Key.isDown(Key.UP)){
_hero._y-=speed;
}
if(Key.isDown(Key.DOWN)){
_hero._y+=speed;
}
if(_hero._x>scene_width-_hero._width/2){
_hero._x=scene_width-_hero._width/2;
}
if(_hero._x<0+_hero._width/2){
_hero._x=_hero._width/2;
}
if(_hero._y>scene_height-_hero._height/2){
_hero._y=scene_height-_hero._height/2;
}
if(_hero._y<0+_hero._height/2){
_hero._y=_hero._height/2;
}
}
functiongetHero():MovieClip{
return_hero;
}
}
importgame.Hero;
importgame.food.*;
importmx.utils.Delegate;
classgame.Player{
//放置游戏的路径
privatevar_target:MovieClip;
//游戏难度
privatevar_level:Number;
//游戏分数
privatevar_score:Number=0;
//游戏主角
privatevar_hero:Hero;
privatevar_life=1000;
//食物
privatevar_food:FirFood;
privatevar_food_test:SecFood;
functionPlayer(tar){
_target=tar;
_food=newFirFood();
_food_test=newSecFood();
initHero();
initFood();
initFood_test();
}
//创建主角
functioninitHero(){
_hero=newHero();
//对数:路径,链接名,深度
_hero.create(_target,"hero",1000);
}
functioninitFood(){
_food._path=_target;
_food.connectToHero(_hero);
for(vari=0;i<10;i++){
_food.create();
}
_food.addEventListener("onEat",Delegate.create(this,setScore));
}
functionsetScore(){
_score+=100;
//升级测试
update();
_target._showScore.text=String(_score);
}
functioninitFood_test(){
_food_test._path=_target;
_food_test.connectToHero(_hero);
for(vari=0;i<10;i++){
_food_test.create();
}
_food_test.addEventListener("onHit",Delegate.create(this,setLife));
}
functionsetLife(){
if(_life>0){
_life-=10;
_target._showLife.text=String(_life);
}
}
functionupdate(){
if(_score==1000){
_food._id="f_0_1";
initFood();
_food_test._id="f_0_2";
initFood_test();
trace("ss");
}
}
}