setRect 设置 SpriteFrame
creator 图片截取
SpriteFrame 类型
cc.loader.load(anim_img, function(err, texture){
var allSpriteFrame = new cc.SpriteFrame(texture);
allSpriteFrame.setRect(cc.rect(0,0,111,139));
self.adSprite.spriteFrame = allSpriteFrame;
});
大图截取小图setRect
showGifAd(anim_json, anim_img){
var self = this;
// var anim_img = "https://wxmini-resource.xxx.com/images/gif/20180711/hdll2/movieclip/box_anim_tex.png";
// var anim_json = "https://wxmini-resource.xxx.com/images/gif/20180711/hdll2/movieclip/box_anim_mc.json";
// console.log('show gif', anim_json,anim_img);
this.adSprite.node.stopAllActions();
// 获取json
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
var response = xhr.responseText;
var resData = JSON.parse(response);
console.log('adgif:', resData);
let frameRate = resData.mc.box_anim.frameRate;
let oneTime = 1/frameRate;
// 解析json数据
var arrDuration = [];
var arrImgName = [];
// 获取图片名字
for (let index = 0; index < resData.mc.box_anim.frames.length; index++) {
const oneFrame = resData.mc.box_anim.frames[index];
arrImgName.push(oneFrame.res);
arrDuration.push(oneFrame.duration);
}
// 获取rect
var arrImgRect = [];
for (let index = 0; index < arrImgName.length; index++) {
const imgName = arrImgName[index];
const grect = resData.res[imgName];
const orect = cc.rect(grect.x, grect.y, grect.w, grect.h);
arrImgRect.push(orect);
}
var curIndex = 0;
// 获取整图
cc.loader.load(anim_img, function(err, texture){
var arrSpriteFrame = [];
for (let index = 0; index < arrImgRect.length; index++) {
var oneSpriteFrame = new cc.SpriteFrame(texture);
oneSpriteFrame.setRect(arrImgRect[index]);
arrSpriteFrame.push(oneSpriteFrame);
}
// 创建动画
var setFrame = function(){
self.adSprite.spriteFrame = arrSpriteFrame[curIndex];
self.adSprite.node.runAction( cc.sequence( cc.delayTime(arrDuration[curIndex]*oneTime), cc.callFunc(setFrame) ) );
curIndex += 1;
if(curIndex >= arrImgRect.length){
curIndex = 0;
}
}
setFrame();
});
}
};
xhr.open("GET", anim_json, true);
xhr.send();
},