都斌大苏打

游戏中战力增减的效果显示

//两个预加载的资源

 

//调用

PowerShow.getInstance().showPower(新战力, 老战力);

//战力展示

class PowerShow extends egret.Sprite{

private power: PowerMove;
private group: eui.Group;
private bgImg: eui.Image;
private bgImg1: eui.BitmapLabel;
private isload: boolean = false;
private static instance: PowerShow;

public constructor() {
super();
this.init();
}

public static getInstance(): PowerShow {
if (!this.instance) {
this.instance = new PowerShow();
}
return this.instance;
}

private init(): void {
this.touchEnabled = false;
this.visible = false;

this.bgImg1 = new eui.BitmapLabel();
this.bgImg1.font=RES.getRes("ImageFontStyle13_fnt");
this.bgImg1.text = "z";
this.bgImg1.touchEnabled = false;
this.addChild(this.bgImg1);
this.bgImg1.x = 15;
this.bgImg1.y = 6;

this.group = new eui.Group();
this.addChild(this.group);

this.power = new PowerMove();
this.group.addChild(this.power);
this.power.y = 10;

MtwGame.Instance.stage.addChild(this);
// GameEventCenter.Instance.addListener(E_GameEvent.Resize, this);
this.onResize(MtwGame.Instance.stage.stageWidth, MtwGame.Instance.stage.stageHeight);
}

private powerArr: Array<any> = [];
private isRuning: boolean;
public showPower(power: number, oldpower: number = 0): void {
this.powerArr.push({ power: power, oldpower: oldpower });
if (!this.visible) this.isRuning = false;
this.checkShow();
}

private checkShow(): void {
if (this.isRuning) return;
if (this.powerArr.length == 0) return;
this.isRuning = true;
egret.setTimeout(function () {
let data = this.powerArr.shift();
if (!data) {
this.isRuning = false;
return;
}
this.show(data.power, data.oldpower);
egret.setTimeout(() => {
this.isRuning = false;
this.checkShow();
}, this, 10);//400
}, this, !this.visible ? 700 : 10);//200
}

private mpower: number = 0;
private opower: number = 0;
private temp: number = 0;
private initX: number = 100;

public show(power: number, oldpower: number = 0): void {
this.clearTween();
this.clearTime();
let isShow: boolean = this.visible;
this.visible = oldpower!=0;
this.mpower = power;
if (this.opower == 0) this.opower = oldpower;
if (this.pn == 0) this.pn = oldpower;
this.temp = power - this.opower;
let point: egret.Point = new egret.Point();
if (this.opower != 0 && this.temp != 0) {
point.x = (((power.toString().length - 1) * 22) >> 1) - 10;
point.y = 55;
this.power.showPower(isShow ? this.mpower : this.opower, this.opower, isShow ? point : null, this.overHandler, isShow);
this.power.x = this.initX + (power.toString().length - 1) * 22;
}
this.opower = power;
}

private overHandler(): void {
if (this.temp == 0) {
this.visible = false;
return;
}
egret.setTimeout(this.clearTween, this, 400, [true]);
}

private end(): void {
this.clearTime();
this.timeId = egret.setTimeout(this.clear, this, 400);
}

private clearTime(): void {
if (this.timeId > 0) {
egret.clearTimeout(this.timeId);
this.timeId = 0;
}
}

private timeId: number = 0;
private pn: number = 0;
private clearTween(bool: boolean = false): void {
if (bool) {
if (this.power.power == this.mpower) {
this.pn = this.mpower;
this.end();
return;
}
let point: egret.Point = new egret.Point();
point.x = (((this.mpower.toString().length - 1) * 22) >> 1) - 10;
point.y = 55;
this.power.showPower(this.mpower, this.pn, point, this.end);
this.power.x = this.initX + (this.mpower.toString().length - 1) * 22;
this.pn = this.mpower;
}
}

public clear(): void {
this.visible = false;
this.power.dispose();
}

public onResize(nW: number, nH: number): void {
this.x = (MtwGame.Instance.stage.stageWidth - 240) >> 1;
this.y = 470;//(MtwGame.Instance.stage.stageHeight-500);
}
}

//战力变化效果

class PowerMove extends egret.Sprite {

private container: egret.Sprite;
private powergroup: egret.Sprite;
private powergroup1: egret.Sprite;
private oldint: number[] = [];
private curint: number[] = [];
private runing: boolean = false;
private currun: number = 0;
private movesp: eui.Group;
private mMask: egret.Shape;
private setW: number;
private setH: number;
private pre: string;
private isScroll: boolean;
private overFunction: Function;
public power: number = 0;
public oldpower: number = 0;
public lastoldpower: number = 0;
public topoint: egret.Point;
public timeid: number;

public constructor($setW: number = 22, $setH: number = 35, $pre: string = "z") {
super();
this.setW = $setW;
this.setH = $setH;
this.pre = $pre;
this.init();
}

private init(): void {
this.touchEnabled = false;
this.container = new egret.Sprite();
this.addChild(this.container);
this.powergroup = new egret.Sprite();
this.addChild(this.powergroup);
this.powergroup1 = new egret.Sprite();
this.addChild(this.powergroup1);

this.mMask = new egret.Shape();
this.mMask.graphics.beginFill(0xff0000, 1);
this.mMask.graphics.drawRect(0, 0, 50, this.setH);
this.mMask.graphics.endFill();
this.addChild(this.mMask);

this.movesp = new eui.Group();
this.movesp.width = 22;
let img: eui.Image;
for (let i = 0; i <= 9; i++) {
img = new eui.Image();
img.source = RES.getRes(`${this.pre + i}_png`);
img.y = i * this.setH;
this.movesp.addChild(img);
}
for (let i = 0; i <= 9; i++) {
img = new eui.Image();
img.source = RES.getRes(`${this.pre + i}_png`);
img.y = (i + 1) * this.setH + this.setH * 9;
this.movesp.addChild(img);
}
this.movesp.mask = this.mMask;
this.movesp.visible = false;
this.addChild(this.movesp);
}

public showPower(newpower: number, oldpower: number, topoint: egret.Point = null, overfuc: Function = null, isScroll: boolean = true): void {
this.container.visible = false;
this.container.x = this.container.y = 0;
if (this.timeid > 0) {
egret.clearTimeout(this.timeid);
this.timeid = 0;
}
let oldhurt: string = oldpower.toString();
let hurtstr: string = newpower.toString();
let addStr: string = Math.abs(newpower - oldpower).toString();
this.power = newpower;
this.oldpower = oldpower;
this.topoint = topoint;
if (this.lastoldpower == 0) this.lastoldpower = this.oldpower;

while (this.powergroup.numChildren > 0) {
this.powergroup.removeChildAt(0);
}
while (this.powergroup1.numChildren > 0) {
this.powergroup1.removeChildAt(0);
}
this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
this.overFunction = overfuc;
this.isScroll = isScroll;
this.currun = 0;
this.runing = false;
this.movesp.y = 0;
this.movesp.x = 0;
this.movesp.visible = true;
while (this.oldint.length > 0) {
this.oldint.pop();
}
while (this.curint.length > 0) {
this.curint.pop()
}
if (oldpower == 0 || oldhurt.length != hurtstr.length)
oldhurt = hurtstr;
if (this.powergroup.numChildren < 1) {
for (let i = 0; i < oldhurt.length; i++) {
let num = parseInt(oldhurt.substr(oldhurt.length - i - 1, 1));
let img: eui.Image = new eui.Image();
img.source = RES.getRes(`${this.pre + num}_png`);
img.x = -i * this.setW;
this.powergroup.addChild(img);
this.oldint.push(num);
}
} else {
for (let i = 0; i < oldhurt.length; i++) {
let num = parseInt(oldhurt.substr(oldhurt.length - i - 1, 1));
this.oldint.push(num);
}
}
if (isScroll) {
let rockstr: string = this.getRock(newpower, oldpower);
for (let i = rockstr.length - 1; i >= 0; i--) {
this.curint.push(parseInt(rockstr.substr(i, 1)));
}
} else {
this.movesp.x = -this.oldint.length * this.setW;
this.mMask.x = -(this.oldint.length - 1) * this.setW;
this.movesp.y = -9 * this.setH;
}
this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this)
}

private enterFrame(evt: egret.Event): void {
if (!this.runing) {
this.runing = true;
if (this.curint.length > this.currun) {
if (this.powergroup.numChildren > 0)
this.powergroup.removeChildAt(0);
let temp = 0;
if (this.oldint.length > this.currun) {
temp = this.oldint[this.currun];
}
this.movesp.y = 0;

let curtemp = this.curint[this.currun];
this.movesp.x = -this.currun * this.setW;
this.mMask.x = this.movesp.x;
egret.Tween.removeTweens(this.movesp);
egret.Tween.get(this.movesp).to({ y: -this.setH * 9 }, 200).call(this.completeHandler, this);
} else {
this.getPowerAdd(this.power, this.lastoldpower, this.topoint);
this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
this.movesp.visible = false;
this.timeid = egret.setTimeout(() => {
if (this.overFunction) {
this.overFunction.call(PowerShow.getInstance(), this);
this.overFunction = null;
}
}, this, 300);
}
}
}

private completeHandler(): void {
egret.Tween.removeTweens(this.movesp);
this.movesp.y = 0;
if (this.currun < this.curint.length) {
let img: eui.Image = new eui.Image();
img.source = RES.getRes(`${this.pre + this.curint[this.currun]}_png`);
img.x = -this.currun * this.setW;
if (this.powergroup1.numChildren > this.currun) {
this.powergroup1.addChildAt(img, this.currun);
} else {
this.powergroup1.addChild(img);
}
this.currun++;
}
this.runing = false;
}

private getPowerAdd(newpower: number, oldpower: number, topoint: egret.Point): void {
if (topoint == null) return;
if (newpower == oldpower) return;

while (this.container.numChildren > 0) {
this.container.removeChildAt(0);
}
let img: eui.Image = new eui.Image();
img.source = RES.getRes(`${newpower >= oldpower ? "add_+" : "dec_-"}_png`);
this.container.addChild(img);
let attr_value: string = newpower >= oldpower ? "add_" : "dec_";
let hurtstr: string = Math.abs(newpower - oldpower).toString();
var showX: number = img.width;
for (let i = 0; i < hurtstr.length; i++) {
let img: eui.Image = new eui.Image();
img.source = RES.getRes(`${attr_value + hurtstr.substr(i, 1)}_png`);
img.x = showX;
this.container.addChild(img);
showX += img.width;
}
this.container.visible = true;
this.container.x = this.powergroup.x + 30;//topoint.x;
this.container.y = topoint.y;
egret.Tween.removeTweens(this.container);
egret.Tween.get(this.container).to({ y: topoint.y - 50 }, 500);
this.lastoldpower = 0;
}

//找到不一样的后面几位
private getRock(power: number, oldpower: number): string {
let powstr: string = power.toString();
let oldpowstr: string = oldpower.toString();
if (power == oldpower || powstr.length != oldpowstr.length)
return powstr;
var ind: number = 0;
for (let i = 0; i <= powstr.length; i++) {
if (parseInt(powstr.charAt(i)) != parseInt(oldpowstr.charAt(i))) {
break;
}
ind += 1;
}
return powstr.substring(ind, powstr.length);
}

public dispose():void{
this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
}
}

posted on 2018-08-09 15:09  都斌大苏打  阅读(449)  评论(0编辑  收藏  举报

导航