Egret白鹭引擎实例开发之密室逃生RoomRun(三)角色状态类和墙体晃动类

一、角色状态类

1、人物角色有5个状态,默认为跳舞状态STAGE1

        //状态

public static STATE1:number = 0;

public static STATE2:number = 1;

public static STATE3:number = 2;

public static STATE4:number = 3;

public static STATE5:number = 4;

2、对应5个状态的精灵帧动画FRAMES

/**人物状态集合 */

public static FRAMES:Array<any> = [

["0020003", "0020004", "0020005", "0020006","0020007"],

        ["0020008"],

        ["0020009", "0020010"],

        ["0020011", "0020012"],

        ["xue0001", "xue0002", "xue0003", "xue0004", "xue0005"]

]

3、设置角色处于哪种状态

/**设置状态 */

public setState(state:number) :void {

this.state = state;

        //死亡状态

if(this.state == Role.STATE5) {

this.isLoop = false;

this.Body.anchorOffsetY = this.Body.height * 0;

}else{

            this.isLoop = true;

            this.Body.anchorOffsetY = this.Body.height * 1;

        }

        if(this.state == Role.STATE3 || this.state == Role.STATE4){

            this.currFrames = [];

            if(Math.random() > 0.5){

                this.currFrames.push(Role.FRAMES[this.state][0]);

            }else{

                this.currFrames.push(Role.FRAMES[this.state][1]);

            }

        }else{

            this.currFrames = Role.FRAMES[this.state];

        }

        this.currFramesIndex = 0;

        this.setBody();

}

二、墙体晃动类

当墙体要开始下落的时候,墙体会晃动提醒玩家要掉下来,增加游戏性

/**墙体晃动 */

class Shake {

private initY:number;

private shakeNum:number;

private overFunc:Function;

private obj:egret.DisplayObject;

private num:number;

private flag:number;

public run (obj:egret.DisplayObject, shakeNum:number, overFunc:Function = null) {

this.obj = obj;

this.initY = obj.y;

this.shakeNum = shakeNum;

this.overFunc = overFunc;

egret.startTick(this.loop, this);

this.num = 0;

this.flag = 0;

}

private loop():boolean {

if(this.flag == 0) {

if(this.obj.y <= this.initY) {

this.obj.y += 5;

} else {

this.obj.y -= 5;

}

if(this.obj.y == this.initY) {

this.num ++;

if(this.num == this.shakeNum) {

egret.stopTick(this.loop, this);

if(this.overFunc) {

this.overFunc();

}

}

}

}

this.flag++;

if(this.flag == 2) {

this.flag = 0;

}

return false;

}

}


发表评论

(必填)

(必填)

(以便回访)

博客已稳定运行: 访问量:180,268 views 网站备案号:闽ICP备17026396号-1