📄 fish.as
字号:
import game.Hero;
import mx.events.EventDispatcher;
import game.phys.Particle;
import mx.utils.Delegate;
class game.fish.Fish {
//fish在场景中创建的路径
public var _path:MovieClip;
//fish在库中的链接名
public var _id:String;
//fish在不同的关卡中的角色(如食物,敌人、道具等)
public var _update:String;
//fish的数目
public var _total:Number = 1;
//fish游动的最大速度和最小速度
private var _maxSpd:Number;
private var _minSpd:Number;
//危险距离
private var _dis:Number = 50;
private var _score:Number = 0;
private var _attack:Number = 0;
private var _hero:MovieClip;
//主角实例
private var _heroIns:Hero;
private var _scenex:Number = Stage.width;
private var _sceney:Number = Stage.height;
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
public function Fish() {
EventDispatcher.initialize(this);
}
//与主角保持联系
public function connectToHero(h:Hero) {
_heroIns = h;
_hero = h.getHero();
}
//创建一只鱼
public function create():Void {
var depth = _path.getNextHighestDepth();
var f:MovieClip = _path.attachMovie(_id, "fish"+depth, depth);
f._x = (depth%3 == 0) ? random(400)+_scenex : random(200)-400;
f._y = random(250)+80;
move(f);
}
function die() {
for (var i in _path) {
if ((_path[i]._name.slice(0, 4)) == "fish") {
_path[i].removeMovieClip();
}
}
}
private function move(obj) {
var t:Particle = new Particle();
t._target = obj;
t.addEventListener("onStop", Delegate.create(this, init));
t.addEventListener("onPlay", Delegate.create(this, setEvent));
init();
//重新移动
function init() {
t._time = _minSpd+random(_maxSpd-_minSpd);
t._endx = random(_scenex+800)-400;
t._endy = random(250)+80;
obj._xscale = t._endx>=obj._x ? 100 : -100;
t.play();
}
function setEvent() {
//首先确定它是属于食物
if (this._update == "food") {
run(t, obj);
}
swim(t, obj);
if (_hero.fish.hitTest(obj)) {
dispatchEvent({type:"onHit", target:this, fish:obj});
//如果是食物或是道具
if (this._update == "food") {
//吃食物的声音
var my_sound:Sound = new Sound();
my_sound.attachSound("eat");
my_sound.start();
//加上相应的分数
_heroIns.setScore(_score);
createScore("score_0", obj, "+", _score);
obj.removeMovieClip();
create();
} else if (this._update == "enemy"&&_heroIns._hide) {
//敌人并且主角处于非隐身状态
var my_sound:Sound = new Sound();
my_sound.attachSound("tach");
my_sound.start();
//碰到敌人时的动作
_heroIns.touch(t._dirx, t._diry);
///减生命
_heroIns.setLife(-1*_attack);
createScore("score_1", obj, "-", _attack);
//减分
//_heroIns.setScore(-1*_score);
//createScore("score_0", obj, "-", _score);
}
}
}
}
//是否有游动的动画,根据速度进行调节
private function swim(t:Particle, obj) {
if (t._time<((_maxSpd-_minSpd)/2+_minSpd)) {
obj.gotoAndStop(5);
} else {
obj.gotoAndStop(1);
}
}
//逃跑(粒子实例,鱼实例)
public function run(t:Particle, obj:MovieClip) {
if (Math.abs(_hero._x-obj._x)<_dis && Math.abs(_hero._y-obj._y)<_dis) {
t._dirx = (_hero._x<=obj._x) ? 1 : -1;
t._diry = (_hero._y<=obj._y) ? 1 : -1;
obj._xscale = t._dirx*100;
//此鱼以它自己的最快速度逃跑
t._time = _minSpd;
}
}
//创建一个气泡,包含一些说明信息
function createScore(id, mc, str, val) {
var depth = _path.getNextHighestDepth();
var s = _path.attachMovie(id, id+depth, depth);
s._x = mc._x;
s._y = mc._y;
s.score.text = str+val;
s.onEnterFrame = function() {
var d = random(5)-2;
this._y -= 4;
this._x += d;
if (this._y<50) {
this.removeMovieClip();
}
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -