SpriteFrame 类型
继承于 Asset, EventTarget(mixin)
一个 SpriteFrame 包含:
- 纹理:会被渲染组件使用的 Texture2D 对象。
- 矩形:在纹理中的矩形区域。
示例
// load a cc.SpriteFrame with image path (Recommend)
var self = this;
var url = "test assets/PurpleMonster";
cc.resources.load(url, cc.SpriteFrame, null, function (err, spriteFrame) {
var node = new cc.Node("New Sprite");
var sprite = node.addComponent(cc.Sprite);
sprite.spriteFrame = spriteFrame;
node.parent = self.node
});
索引
属性(properties)
insetTopNumbersprite 的顶部边框insetBottomNumbersprite 的底部边框insetLeftNumbersprite 的左边边框insetRightNumbersprite 的左边边框urlStringcc.Asset.urlis deprecated, please use nativeUrl instead_uuidStringloadedBoolean该资源是否已经成功加载。nativeUrlString返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。refCountNumber引用的数量_nativeString保存原生资源的 URL。_nativeAssetObject此资源依赖的底层原生资源(如果有的话)。_nameString_objFlagsNumbernameString该对象的名称。isValidBoolean表示该对象是否可用(被 destroy 后将不可用)。
方法
constructorSpriteFrame 类的构造函数。textureLoaded返回是否已加载纹理isRotated获取 SpriteFrame 是否旋转setRotated设置 SpriteFrame 是否旋转isFlipX获取 SpriteFrame 是否反转 x 轴isFlipY获取 SpriteFrame 是否反转 y 轴setFlipX设置 SpriteFrame 是否翻转 x 轴setFlipY设置 SpriteFrame 是否翻转 y 轴getRect获取 SpriteFrame 的纹理矩形区域setRect设置 SpriteFrame 的纹理矩形区域getOriginalSize获取修剪前的原始大小setOriginalSize设置修剪前的原始大小getTexture获取使用的纹理实例getOffset获取偏移量setOffset设置偏移量clone克隆 SpriteFramesetTexture通过 Texture,rect,rotated,offset 和 originalSize 设置 SpriteFrame。ensureLoadTexture当加载中的场景或 Prefab 被标记为asyncLoadAssets时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。clearTexture当你暂时不再使用这个 SpriteFrame 时,可以调用这个方法来保证引用的贴图对象能被 GC。hasEventListener检查事件目标对象是否有为特定类型的事件注册的回调。on注册事件目标的特定事件类型回调。off删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。once注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent通过事件对象派发事件clear销毁记录的事件toString返回资源的 URL。serialize应 AssetDB 要求提供这个方法。createNode使用该资源在场景中创建一个新节点。_setRawAsset为此资源设置原生文件名。addRef增加资源的引用decRef减少资源的引用并尝试进行自动释放。destroy销毁该对象,并释放所有它对其它对象的引用。_destruct清除实例中的所有引用。_onPreDestroy在对象被销毁之前调用。_serialize为此对象定制序列化。_deserialize从自定义序列化数据初始化此对象。
Details
属性(properties)
insetTop
sprite 的顶部边框
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:87 |
insetBottom
sprite 的底部边框
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:106 |
insetLeft
sprite 的左边边框
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:125 |
insetRight
sprite 的左边边框
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:144 |
url
cc.Asset.urlis deprecated, please use nativeUrl instead
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/asset-manager/deprecated.js:728 |
| 废弃(Deprecated) | cc.Asset.url is deprecated, please use cc.Asset.nativeUrl instead |
_uuid
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:57 |
loaded
该资源是否已经成功加载。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/assets/CCAsset.js:66 |
nativeUrl
返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:81 |
refCount
引用的数量
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCAsset.js:115 |
_native
保存原生资源的 URL。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:131 |
_nativeAsset
此资源依赖的底层原生资源(如果有的话)。 此属性可用于访问与资源相关的其他详细信息或功能。 如果
_native可用,则此属性将由加载器初始化。
| meta | description |
|---|---|
| 类型 | Object |
| 定义于 | cocos2d/core/assets/CCAsset.js:142 |
_name
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/platform/CCObject.js:76 |
_objFlags
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/platform/CCObject.js:83 |
name
该对象的名称。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/platform/CCObject.js:240 |
示例
obj.name = "New Obj";
isValid
表示该对象是否可用(被 destroy 后将不可用)。
当一个对象的destroy调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始isValid就会返回 false,而当前帧内isValid仍然会是 true。如果希望判断当前帧是否调用过destroy,请使用cc.isValid(obj, true),不过这往往是特殊的业务需求引起的,通常情况下不需要这样。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/platform/CCObject.js:258 |
示例
var node = new cc.Node();
cc.log(node.isValid); // true
node.destroy();
cc.log(node.isValid); // true, still valid in this frame
// after a frame...
cc.log(node.isValid); // false, destroyed in the end of last frame
方法
constructor
SpriteFrame 类的构造函数。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:164 |
参数列表
filenameString | Texture2DrectRectrotatedBoolean Whether the frame is rotated in the textureoffsetVec2 The offset of the frame in the textureoriginalSizeSize The size of the frame in the texture
textureLoaded
返回是否已加载纹理
| meta | description |
|---|---|
| 返回 | boolean |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:224 |
isRotated
获取 SpriteFrame 是否旋转
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:247 |
setRotated
设置 SpriteFrame 是否旋转
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:257 |
参数列表
bRotatedBoolean
isFlipX
获取 SpriteFrame 是否反转 x 轴
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:270 |
isFlipY
获取 SpriteFrame 是否反转 y 轴
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:280 |
setFlipX
设置 SpriteFrame 是否翻转 x 轴
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:290 |
参数列表
flipXBoolean
setFlipY
设置 SpriteFrame 是否翻转 y 轴
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:303 |
参数列表
flipYBoolean
getRect
获取 SpriteFrame 的纹理矩形区域
| meta | description |
|---|---|
| 返回 | Rect |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:316 |
setRect
设置 SpriteFrame 的纹理矩形区域
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:326 |
参数列表
rectRect
getOriginalSize
获取修剪前的原始大小
| meta | description |
|---|---|
| 返回 | Size |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:339 |
setOriginalSize
设置修剪前的原始大小
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:349 |
参数列表
sizeSize
getTexture
获取使用的纹理实例
| meta | description |
|---|---|
| 返回 | Texture2D |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:364 |
getOffset
获取偏移量
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:420 |
setOffset
设置偏移量
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:430 |
参数列表
offsetsVec2
clone
克隆 SpriteFrame
| meta | description |
|---|---|
| 返回 | SpriteFrame |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:440 |
setTexture
通过 Texture,rect,rotated,offset 和 originalSize 设置 SpriteFrame。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:450 |
参数列表
ensureLoadTexture
当加载中的场景或 Prefab 被标记为 asyncLoadAssets 时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。
只有当 Sprite 组件要渲染这些 SpriteFrame 时,才会检查贴图是否加载。如果你希望加载过程提前,你可以手工调用这个方法。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:498 |
示例
if (spriteFrame.textureLoaded()) {
this._onSpriteFrameLoaded();
}
else {
spriteFrame.once('load', this._onSpriteFrameLoaded, this);
spriteFrame.ensureLoadTexture();
}
clearTexture
当你暂时不再使用这个 SpriteFrame 时,可以调用这个方法来保证引用的贴图对象能被 GC。然后当你要渲染 SpriteFrame 时,你需要手动调用 ensureLoadTexture 来重新加载贴图。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCSpriteFrame.js:526 |
| 废弃(Deprecated) | since 2.1 |
hasEventListener
检查事件目标对象是否有为特定类型的事件注册的回调。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/event/event-target.js:69 |
参数列表
typeString The type of event.
on
注册事件目标的特定事件类型回调。这种类型的事件应该被 emit 触发。
| meta | description |
|---|---|
| 返回 | Function |
| 定义于 | cocos2d/core/event/event-target.js:77 |
参数列表
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
示例
eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:119 |
参数列表
typeString A string representing the event type being removed.callbackFunction The callback to remove.targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
示例
// register fire eventListener
var callback = eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
targetOff
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:163 |
参数列表
targetObject The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:182 |
参数列表
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
示例
eventTarget.once('fire', function () {
cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent
通过事件对象派发事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:208 |
参数列表
eventEvent
clear
销毁记录的事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:221 |
toString
返回资源的 URL。
Asset 对象将会重写 Object 对象的 toString() 方法。
对于 Asset 对象,toString() 方法返回该对象的字符串表示形式。
当资源要表示为文本值时或在字符串连接时引用时,JavaScript 会自动调用 toString() 方法。
| meta | description |
|---|---|
| 返回 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:212 |
serialize
应 AssetDB 要求提供这个方法。
| meta | description |
|---|---|
| 返回 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:232 |
createNode
使用该资源在场景中创建一个新节点。
如果这类资源没有相应的节点类型,该方法应该是空的。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCAsset.js:246 |
参数列表
_setRawAsset
为此资源设置原生文件名。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCAsset.js:261 |
参数列表
addRef
增加资源的引用
| meta | description |
|---|---|
| 返回 | Asset |
| 定义于 | cocos2d/core/assets/CCAsset.js:283 |
decRef
减少资源的引用并尝试进行自动释放。
| meta | description |
|---|---|
| 返回 | Asset |
| 定义于 | cocos2d/core/assets/CCAsset.js:301 |
destroy
销毁该对象,并释放所有它对其它对象的引用。
实际销毁操作会延迟到当前帧渲染前执行。从下一帧开始,该对象将不再可用。
您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/platform/CCObject.js:293 |
示例
obj.destroy();
_destruct
清除实例中的所有引用。
注意:此方法不会清除在 CCObject 实例中定义的 getter 或 setter。如果需要,你可以重写 _destruct 方法。例如:
_destruct: function () {
for (var key in this) {
if (this.hasOwnProperty(key)) {
switch (typeof this[key]) {
case 'string':
this[key] = '';
break;
case 'object':
case 'function':
this[key] = null;
break;
}
}
}
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/CCObject.js:427 |
_onPreDestroy
在对象被销毁之前调用。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/CCObject.js:483 |
_serialize
为此对象定制序列化。
| meta | description |
|---|---|
| 返回 | object |
| 定义于 | cocos2d/core/platform/CCObject.js:511 |
参数列表
exportingBoolean
_deserialize
从自定义序列化数据初始化此对象。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/CCObject.js:524 |
参数列表
dataObject the serialized json datactx_Deserializer