Flash游戏制作--大鱼吃小鱼
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, depth:Number):Void {
_hero = path.attachMovie(name, "hero", depth);
_moveID = setInterval(this, "move", 30);
}
function move() {
var speed =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;
}
}
function getHero():MovieClip {
return _hero;
}
}
import game.Hero;
import game.food.*;
import mx.utils.Delegate;
class game.Player {
//放置游戏的路径
private var _target:MovieClip;
//游戏难度
private var _level:Number;
//游戏分数
private var _score:Number = 0;
//游戏主角
private var _hero:Hero;
private var _life = 1000;
//食物
private var _food:FirFood;
private var _food_test:SecFood;
function Player(tar) {
_target = tar;
_food = new FirFood();
_food_test = new SecFood();
initHero();
initFood();
initFood_test();
}
//创建主角
function initHero() {
_hero = new Hero();
//对数:路径,链接名,深度
_hero.create(_target, "hero", 1000);
}
function initFood() {
_food._path = _target;
_food.connectToHero(_hero);
for (var i = 0; i<10; i++) {
_food.create();
}
_food.addEventListener("onEat", Delegate.create(this, setScore));
}
function setScore() {
_score += 100;
//升级测试
update();
_target._showScore.text = String(_score);
}
function initFood_test() {
_food_test._path = _target;
_food_test.connectToHero(_hero);
for (var i = 0; i<10; i++) {
_food_test.create();
}
_food_test.addEventListener("onHit", Delegate.create(this, setLife));
}
function setLife() {
if (_life>0) {
_life -= 10;
_target._showLife.text = String(_life);
}
}
function update() {
if (_score == 1000) {
_food._id = "f_0_1";
initFood();
_food_test._id = "f_0_2";
initFood_test();
trace("ss");
}
}
}