拆分Merger的帧动画

编程入门 行业动态 更新时间:2024-10-23 14:29:27

拆分Merger的帧<a href=https://www.elefans.com/category/jswz/34/1769013.html style=动画"/>

拆分Merger的帧动画

写个工具拆分Texture Merger做的帧动画。

//
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//enum EStatus {checkRect,prepareSave,save,end
}class Main extends egret.DisplayObjectContainer {public constructor() {super();this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addStage, this);}public addStage(event: egret.Event): void {egret.lifecycle.addLifecycleListener((context: egret.lifecycle.LifecycleContext) => {context.onUpdate = this.onUpdate.bind(this);});egret.lifecycle.onPause = (): void => {egret.ticker.pause();}egret.lifecycle.onResume = (): void => {egret.ticker.resume();}RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.configComplete, this);RES.loadConfig("resource/default.res.json", "resource/");}public configComplete(event: RES.ResourceEvent): void {RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.configComplete, this);this.parseAnims();}public getWidth(): number {return egret.MainContext.instance.stage.stageWidth;}public getHeight(): number {return egret.MainContext.instance.stage.stageHeight;}private SD_JSONS = ["Bomb.json","FishingNetGray.json","MR_fish.json","Medal1_jewel.json","Medal2_jewel.json","Medal3_jewel.json","Mermaid.json","Mermaid_die.json","Rainbow_coin.json","Rainbow_loop.json","Rainbow_start.json","TreasureBox-coin.json","TreasureBox.json","TreasureBox_vanish.json","aimShootEff.json","appearanceOfLight.json","autoShootEff.json","batteryChangeEff.json","clickingWaterWave.json","cloud_01.json","cloud_02.json","coin_fish.json","deng.json","fastShootEff.json","fish25_t.json","fish25_t_die.json","fishAll1.json","fishAll2.json","fishingNetPurple.json","jinbi_fish.json","js_fish.json","langbo.json","langbo1.json","long.json","long_die.json","mz_fish.json","paojilv_fish.json","paojizi_1_fish.json","qwy.json","qwy_die.json","wave.json","yinbi_fish.json","ywangdajin.json","zd_fish.json",];private lastTime: number = 0;private pastTime: number = 0;private animIndex: number = 0;private allAnim: string[] = [];private preLoadAllRes: string[] = [];private realLoadAllRes: string[] = [];private allActions: string[] = null;private mc: DefaultMovieClip;private sprite: egret.Sprite = null;private fishWidth = 0;private fishHeight = 0;private status: EStatus = EStatus.checkRect;private bg: egret.Shape;private factory: egret.MovieClipDataFactory;private rect: egret.Rectangle = null;private fishImageNames: string[] = [];private currentAnimPrefix: string = null;private currentAction = "";public parseAnims() {this.rect = new egret.Rectangle();this.bg = new egret.Shape();this.addChild(this.bg);this.bg.x = 0;this.bg.y = 0;this.bg.graphics.beginFill(0x000000, 0);this.bg.graphics.drawRect(0, 0, this.getWidth(), this.getHeight());this.bg.graphics.endFill();this.sprite = new egret.Sprite();this.sprite.x = 0;this.sprite.y = 0;this.addChild(this.sprite);for (let k in this.SD_JSONS) {let json: string = this.SD_JSONS[k];let prefix = json.substring(0, json.lastIndexOf(".json"));let jsonUrl = this.getPath(prefix).concat(".json");let pngUrl = this.getPath(prefix).concat(".png");this.allAnim.push(this.getPath(prefix));this.preLoadAllRes.push(jsonUrl);this.preLoadAllRes.push(pngUrl);RES.getResByUrl(jsonUrl, this.onLoadRes, this);RES.getResByUrl(pngUrl, this.onLoadRes, this);}}private onLoadRes(res: any, url: string) {this.realLoadAllRes.push(url);if (this.realLoadAllRes.length === this.preLoadAllRes.length) {if (this.allAnim.length > 0) {this.loadNextAnim();}}}private getPath(prefix: string): string {return "resource/assets/anim/".concat(prefix);}public loadNextAnim() {this.status = EStatus.checkRect;this.fishWidth = 0;this.fishHeight = 0;this.currentAnimPrefix = null;if (this.allAnim.length > 0 && this.animIndex < this.allAnim.length) {this.currentAnimPrefix = this.allAnim[this.animIndex];this.animIndex++;}if (this.mc) {this.mc.parent.removeChildren();this.mc = null;}if (!this.currentAnimPrefix) {egret.log("finish");this.status = EStatus.end;return;}egret.log("this.currentAnimPrefix = ", this.currentAnimPrefix);this.mc = new DefaultMovieClip();this.mc.setCompleteAction(thispleteAction, this);this.sprite.addChild(this.mc);let resPng = RES.getRes(this.currentAnimPrefix.concat(".png"));let resJson = RES.getRes(this.currentAnimPrefix.concat(".json"));this.factory = new egret.MovieClipDataFactory(resJson, resPng);this.allActions = null;if (this.factory.mcDataSet&& this.factory.mcDataSet.mc) {let actions: {} = this.factory.mcDataSet.mc;this.allActions = Object.keys(actions);egret.log("allActions  ", this.allActions);}this.loadNextAction();}private loadNextAction() {if (!this.allActions || this.allActions.length === 0) {this.loadNextAnim();return;}this.lastTime = egret.getTimer();this.pastTime = 0;this.fishWidth = 0;this.fishHeight = 0;this.status = EStatus.checkRect;this.currentAction = this.allActions.shift();egret.log("action name = ", this.currentAction);let mcData = this.factory.generateMovieClipData(this.currentAction);this.mc.setMcData(mcData);//还能再改进,但是不做了。this.mc.gotoAction("mc", 1);}public onUpdate(): void {let now = egret.getTimer();let delta = now - this.lastTime;this.lastTime = now;if (this.mc) {if (this.status === EStatus.save) {//andrew 保存图片的时候,时间太短texture.saveToFile会执行失败if (this.pastTime < 200) {this.pastTime += delta;return;}}this.mc.runAction();if (this.status === EStatus.checkRect) {if (this.sprite.width > this.fishWidth) {this.fishWidth = this.sprite.width;}if (this.sprite.height > this.fishHeight) {this.fishHeight = this.sprite.height;}}else if (this.status === EStatus.prepareSave) {this.status = EStatus.save;this.pastTime = 0;} else if (this.status === EStatus.save) {this.rect.x = 0;this.rect.y = 0;this.rect.width = this.fishWidth;this.rect.height = this.fishHeight;let img = this.mc.getImageName();if (this.fishImageNames.indexOf(img) === -1) {this.pastTime = 0;let renderTexture = new egret.RenderTexture();renderTexture.drawToTexture(this, this.rect);let prefix: string = img;let suffix: string = "_png";if (img.length > suffix.length) {let index: number = img.length - suffix.length;if (suffix === img.substr(index)) {prefix = img.substring(0, index);}}let savePath = prefix.concat(".png");renderTexture.saveToFile("image/png", savePath, this.rect);renderTexture.dispose();this.fishImageNames.push(img);egret.log("image = ", img);}}}}private completeAction() {let action = this.mc.getCurrAction();if (this.status === EStatus.checkRect) {this.status = EStatus.save;this.sprite.x = this.fishWidth / 2;this.sprite.y = this.fishHeight / 2;this.mc.gotoAction(action, 1);this.status = EStatus.prepareSave;} else if (this.status === EStatus.save) {this.loadNextAction();}}}

DefaultMovieClip.ts

/*** Created by yangsong on 2017/10/11.*/class DefaultAction {public static Normal: string = "normal";public static Die: string = "die";
}/*** @description 只管播放动画。旋转角度的问题交给 rotation属性* @author (..)* @date 2018-10-31* @class DefaultMovieClip* @extends {egret.Bitmap}*/
class DefaultMovieClip extends egret.Bitmap {private McFrameTime: number = 1000 / 8;private animSpeedScale: number = 1;private mcData: egret.MovieClipData;private currAction: string;private currFrame: number;private startFrame: number;private endFrame: number;private currFrameTime: number;private totalPlayNum: number;private currPlayNum: number;private completeAction: Function;private completeActionObj: any;private maxWidth: number = 0;//每一帧图片大小不一,取最大那张private maxHeight: number = 0;//每一帧图片大小不一,取最大那张private imageName = "null";public constructor() {super();}private setBitmap(texture: egret.Texture, scaleX: number): void {this.texture = texture;this.scaleX = scaleX;//TODO: andrew 这里偏移后子弹击中检测会有问题// AnchorUtil.setAnchorX(this, 0.5);// AnchorUtil.setAnchorY(this, 1);}public getImageName(): string {return this.imageName;}/*** Setter $animSpeedScale* @param {number } value*/public set $animSpeedScale(value: number) {this.animSpeedScale = value;}public setDefault(resName: string): void {this.setBitmap(RES.getRes(resName), 1);}public setMcData(mcData: egret.MovieClipData): void {this.mcData = mcData;}public runAction(): void {if (!this.mcData) {return;}if (this.animSpeedScale <= 0) {this.animSpeedScale = 1;}if (this.currFrame > this.endFrame) {this.currFrame = this.startFrame;this.currPlayNum++;}if (this.totalPlayNum && this.currPlayNum >= this.totalPlayNum) {thispleteAction && thispleteAction.apply(thispleteActionObj);} else {var bitmapTexture: egret.Texture = this.mcData.getTextureByFrame(this.currFrame);this.setBitmap(bitmapTexture, this.scaleX);this.adjustPosition();this.currFrame++;}}private adjustPosition() {let framData: any = this.mcData.getKeyFrameData(this.currFrame);if (framData) {if ("x" in framData && "y" in framData) {this.x = framData["x"];this.y = framData["y"];this.imageName = framData["res"];}}}public gotoAction(actionName: string, playNum: number = 0, cover: boolean = false): void {if (!this.mcData || !this.mcData.labels) {return;}//todo: andrew 实际死亡动画只用播放一次.var totalPlayNum: number = playNum;// if (gotoAction == DefaultAction.Stand) {//     totalPlayNum = 0;// }var actionName: string = actionName;var currLabel: egret.FrameLabel;for (var i = 0; i < this.mcData.labels.length; i++) {if (actionName == this.mcData.labels[i].name) {currLabel = this.mcData.labels[i];}}if (this.currAction == actionName && !cover) {this.currFrame = currLabel.frame + (this.currFrame - this.startFrame);}else {this.currFrame = currLabel.frame;this.currFrameTime = 0;}this.startFrame = currLabel.frame;this.endFrame = currLabel.end;this.currAction = actionName;this.totalPlayNum = totalPlayNum;this.currPlayNum = 0;this.maxWidth = 0;this.maxHeight = 0;for (let frame = this.startFrame; frame <= this.endFrame; frame++) {let texture: egret.Texture = this.mcData.getTextureByFrame(frame);if (texture) {if (texture.textureWidth > this.maxWidth) {this.maxWidth = texture.textureWidth;}if (texture.textureHeight > this.maxHeight) {this.maxHeight = texture.textureHeight;}}}// let bitmapTexture: egret.Texture = this.mcData.getTextureByFrame(this.currFrame);// this.setBitmap(bitmapTexture, 1);// this.adjustPosition();}public setCompleteAction(complateAction: Function, completeActionObj: any): void {thispleteAction = complateAction;thispleteActionObj = completeActionObj;}public destroy(): void {if (this.parent) {let parent = this.parent;parent.removeChild(this);}// App.DisplayUtils.removeFromParent(this);// ObjectPool.push(this);this.texture = null;this.mcData = null;this.currAction = null;this.currFrame = null;this.startFrame = null;this.endFrame = null;this.currFrameTime = null;this.totalPlayNum = null;this.currPlayNum = null;thispleteAction = null;thispleteActionObj = null;this.animSpeedScale = 1;}public getCurrAction(): string {return this.currAction;}public getMaxWidth(): number {return this.maxWidth;}public getMaxHeight(): number {return this.maxHeight;}
}

 

更多推荐

拆分Merger的帧动画

本文发布于:2024-02-27 02:26:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1704856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画   Merger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!