92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, '655f9KCYHJKK4xPQbomBRNE', 'nx.fx.sprite');
|
|
// Scripts/nx/cmp/flex/nx.fx.sprite.js
|
|
|
|
"use strict";
|
|
|
|
/******************************************************************
|
|
* Copyright(C) 2019 - 2020 Nx Studio
|
|
*
|
|
* 精灵强化
|
|
*
|
|
* 2023.11.01
|
|
******************************************************************/
|
|
|
|
cc.Class({
|
|
"extends": cc.Component,
|
|
// 编辑器特性
|
|
editor: {
|
|
// 允许当前组件在编辑器模式下运行
|
|
executeInEditMode: false,
|
|
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
|
|
disallowMultiple: true,
|
|
// menu 用来将当前组件添加到组件菜单中,方便用户查找
|
|
menu: "Nx/组件|精灵"
|
|
},
|
|
// 销毁
|
|
onDestroy: function onDestroy() {
|
|
this.cacheFrame();
|
|
},
|
|
// 设置精灵
|
|
setSpriteFrame: function setSpriteFrame(_sfkey, _cb) {
|
|
var _this = this;
|
|
var sprite = this.getComponent(cc.Sprite);
|
|
if (!sprite) {
|
|
nx.error("NxFxSprite:\u65E0\u6548\u7CBE\u7075\u7EC4\u4EF6!");
|
|
return;
|
|
}
|
|
|
|
// 直接设置
|
|
if (_sfkey == null || _sfkey instanceof cc.SpriteFrame) {
|
|
sprite.spriteFrame = _sfkey;
|
|
this.cacheFrame(_sfkey, "");
|
|
nx.dt.fnInvoke(_cb, true);
|
|
return;
|
|
}
|
|
|
|
// 空字符串
|
|
if (typeof _sfkey == "string" && _sfkey == "") {
|
|
sprite.spriteFrame = null;
|
|
this.cacheFrame();
|
|
nx.dt.fnInvoke(_cb, true);
|
|
return;
|
|
}
|
|
|
|
// 重复设置
|
|
if (this.curKey == _sfkey) {
|
|
return;
|
|
}
|
|
|
|
// 重载载入
|
|
nx.res.loadSpriteFrame(_sfkey, function (_err, _sframe) {
|
|
if (_err) {
|
|
sprite.spriteFrame = null;
|
|
_this.cacheFrame();
|
|
nx.dt.fnInvoke(_cb, false);
|
|
return;
|
|
}
|
|
sprite.spriteFrame = _sframe;
|
|
_this.cacheFrame(_sframe, _sfkey);
|
|
nx.dt.fnInvoke(_cb, true);
|
|
});
|
|
},
|
|
// 资源缓存
|
|
cacheFrame: function cacheFrame(_sframe, _key) {
|
|
if (this.sframe) {
|
|
if (window.nx && nx && nx.assets) {
|
|
nx.assets.uncacheAsset(this.sframe);
|
|
}
|
|
this.sframe = null;
|
|
this.curKey = "";
|
|
}
|
|
if (_sframe) {
|
|
if (window.nx && nx && nx.assets) {
|
|
nx.assets.cacheAsset(_sframe, _key);
|
|
}
|
|
this.sframe = _sframe;
|
|
this.curKey = _key || "";
|
|
}
|
|
}
|
|
});
|
|
|
|
cc._RF.pop(); |