Flash games--ones (i)

Source: Internet
Author: User
Just wrote a part
Import mx.transitions.easing.*;
Import game. Hero;
Class Game.food.Food {
Specify path
public Var _path:movieclip;
Moving speed, controlled by time, specified in subclasses
private Var _time:number;
Default mode of Motion
private var _func:function = none.easeout;
Game lead
private Var _hero:movieclip;
The size of the scene
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;
Keep in touch with the protagonist
Public Function Connecttohero (H:hero) {
_hero = H.gethero ();
}
Create a food
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 (M) +_scenex:random (100)-100;
f._y = Random (_sceney-100) +50;
Move (f);
}
Movement mode, subclass implementation
Public function Move (obj:movieclip): Void {
}
}
Import Game.food.Food;
Import Mx.transitions.Tween;
Import Mx.events.EventDispatcher;
Class Game.food.FirFood extends Food {
The link name of food in the library
public var _id:string = "F_0_0";
New instance name of food
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 (m) -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 {
The link name of food in the library
public var _id:string = "F_0_1";
New instance name of food
private var _name:string = "OK";
Public Function Secfood () {
Eventdispatcher.initialize (this);
}
function Create () {
Super.create (_id, _name);
}
I would like to use a different animation effect, lazy to write, from the top of the copy over to forget
function Move (obj) {
Set different speeds depending on the situation to increase the difficulty
var _time:number = random (10) +5;
var _end:object = {X:random (_scenex), Y:random (m) -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 {
Place the path of the game
private Var _target:movieclip;
The difficulty of the game
private Var _level:number;
Game Score
private var _score:number = 0;
Game lead
private Var _hero:hero;
private var _life = 1000;
Food
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 ();
}
Create a lead
function Inithero () {
_hero = new Hero ();
Logarithm: path, link name, depth
_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;
Upgrade Test
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");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.