加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-js使用plist执行自身动作

发布时间:2020-12-14 16:50:32 所属栏目:百科 来源:网络整理
导读:首先需要将精灵动作帧动画图片使用TexturePacker创建plist,创建好后,将生成的plist和png图片(所有帧动画图片集成的一张大图); 1、进行预加载操作 cc.spriteFrameCache.addSpriteFrames(res.Mario_plist); 预加载后,可在工程中加载使用plist中的任何一

首先需要将精灵动作帧动画图片使用TexturePacker创建plist,创建好后,将生成的plist和png图片(所有帧动画图片集成的一张大图);

1、进行预加载操作

cc.spriteFrameCache.addSpriteFrames(res.Mario_plist);
预加载后,可在工程中加载使用plist中的任何一个动作图片,使用方式:
cc.spriteFrameCache.getSpriteFrame("mario_14_01.png");

2、精灵需要自身动作,如跑动或者击打等动作,执行代码如下:
addFrames:function(){
    	for(var i=1;i<21;i++){
    		if(i<10){
    			this.spriteFames.push(cc.spriteFrameCache.getSpriteFrame("mario_14_0"+i+".png"));
    		}else{
    			this.spriteFames.push(cc.spriteFrameCache.getSpriteFrame("mario_14_"+i+".png"));
    		}
    	}
    },runSelf:function(){
    	var animation = new cc.Animation(this.spriteFames);
    	animation.setDelayPerUnit(1/30);  
    	animation.setRestoreOriginalFrame(true); 
    	animation.setLoops(-1);
    	var animate = cc.Animate(animation);
    	this.mario.runAction(animate);
    }
如上代码,在addFrames函数中,将plist的动作图片加入spriteFrames数组中,在runSelf函数中,定义动画,最后让马里奥执行动画,如没有什么差错,此时应该能看到一个拼命在跑动的马里奥,虽然只是在原地踏步。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读