cocos2d-js 创建帧动画

封装创建方法

var RunActionHelper = function(){

};
RunActionHelper.prototype.createAnimationByPlist = function(frames, time){
var animation = cc.Animation.create(frames, time);
var animate = cc.animate(animation);
return animate;
};
RunActionHelper.prototype.createAnimationByImg = function(imgAry, time){
var animation = cc.Animation.create();
for(var i=0; i<imgAry.length; i++){
animation.addSpriteFrameWithFile(imgAry[i]);
}
animation.setDelayPerUnit(time || 0.1);//每一帧播放间隔
animation.setRestoreOriginalFrame(true);//是否回到第一帧播放
var animate = cc.animate(animation);
return animate;
};

 

//创建动画

cc.spriteFrameCache.addSpriteFrames(res.s_img2list);
//cc.textureCache.addImage(res.s_img2);

var sprite1 = cc.Sprite.create(cc.spriteFrameCache.getSpriteFrame("1.png"));
sprite1.setPosition(cc.p(200,150));
this.addChild(sprite1, g_GameZOrder.ui);
var frames = [];
for(var i=1;i<9;i++){
var frame = cc.spriteFrameCache.getSpriteFrame(i+".png");
frames.push(frame);
}
var runAHelper = new RunActionHelper();
var animate = runAHelper.createAnimationByPlist(frames, 0.2);
sprite1.runAction(animate.repeatForever());

var sprite2 = cc.Sprite.create(cc.spriteFrameCache.getSpriteFrame("1.png"));
sprite2.setPosition(cc.p(200,50));
this.addChild(sprite2, g_GameZOrder.ui);
var img = [];
for(var i=1;i<9;i++){
img.push(res["s_img_"+i]);
}
var animate2 = runAHelper.createAnimationByImg(img, 0.2);
sprite2.runAction(cc.sequence(animate2).repeatForever());

你可能感兴趣的:(cocos2d-js)