SpriteFrame 类型

继承于 Asset

模块: cc 父模块: cc

一个 SpriteFrame 包含:

  • 纹理:会被 Sprite 使用的 Texture2D 对象。
  • 矩形:在纹理中的矩形区域。

索引

属性(properties)
  • _textureFilenameSetter String Use this property to set raw texture url during loading
  • insetTop Number sprite 的顶部边框
  • insetBottom Number sprite 的底部边框
  • insetLeft Number sprite 的左边边框
  • insetRight Number sprite 的左边边框
  • rawUrl String 返回该资源的原始文件的 URL,如果不支持 RAW 文件,它将返回一个空字符串。
  • rawUrls String[] 返回该资源的原文件的 URL 数组,如果不支持 RAW 文件,它将返回一个空数组。
  • _rawFiles String[] 在 lite 版的 Fireball 里,raw asset 并不仅仅是在 properties 里声明了 rawType 才有, 而是每个 asset 都能指定自己的 raw file url。这些 url 就存在 _rawFiles 字段中。 AssetLibrary 并不会帮你加载这些 url,除非你声明了 rawType。 在 Creator 里,_rawFiles 保留了下来,为了复用 cocos 引擎原有实现,直接用 _rawFiles 来加载 Asset 在 import 之前的源文件。
  • _uuid String
  • _name String
  • _objFlags Number
  • name String 该对象的名称。
  • isValid Boolean 表示该对象是否可用(被销毁后将不可用)。
方法
  • constructor SpriteFrame 类的构造函数。
  • textureLoaded 返回是否已加载纹理
  • addLoadedEventListener Add a event listener for texture loaded event.
  • isRotated 获取 SpriteFrame 是否旋转
  • setRotated 设置 SpriteFrame 是否旋转
  • getRect 获取 SpriteFrame 的纹理矩形区域
  • setRect 设置 SpriteFrame 的纹理矩形区域
  • getOriginalSize 获取修剪前的原始大小
  • setOriginalSize 设置修剪前的原始大小
  • getTexture 获取使用的纹理实例
  • getOffset 获取偏移量
  • setOffset 设置偏移量
  • clone 克隆 SpriteFrame
  • setTexture 通过 Texture,rect,rotated,offset 和 originalSize 设置 SpriteFrame
  • ensureLoadTexture 当加载中的场景或 Prefab 被标记为 asyncLoadAssets 时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。 只有当 Sprite 组件要渲染这些 SpriteFrame 时,才会检查贴图是否加载。如果你希望加载过程提前,你可以手工调用这个方法。
  • clearTexture 当你暂时不再使用这个 SpriteFrame 时,可以调用这个方法来保证引用的贴图对象能被 GC。然后当你要渲染 SpriteFrame 时,你需要手动调用 ensureLoadTexture 来重新加载贴图。
  • serialize 应 AssetDB 要求提供这个方法
  • createNode 使用该资源在场景中创建一个新节点。
    如果这类资源没有相应的节点类型,该方法应该是空的。
  • _setRawFiles Set raw file names for this asset.
  • _preloadRawFiles Preload raw files when loading scene.
  • on 注册事件目标的特定事件类型回调。
  • off 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
  • targetOff 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
  • once 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
  • dispatchEvent 分发事件到事件流中。
  • emit 该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。
  • destroy 销毁该对象,并释放所有它对其它对象的引用。
    销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 实际销毁操作会延迟到当前帧渲染前执行。
  • _destruct Clear all references in the instance.

NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _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; } } }

  • _onPreDestroy Called before the object being destroyed.
  • _serialize The customized serialization for this object. (Editor Only)
  • _deserialize Init this object from the custom serialized data.

Details

属性(properties)

_textureFilenameSetter

Use this property to set raw texture url during loading

meta description
类型 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:63
insetTop

sprite 的顶部边框

meta description
类型 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:113
insetBottom

sprite 的底部边框

meta description
类型 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:122
insetLeft

sprite 的左边边框

meta description
类型 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:131
insetRight

sprite 的左边边框

meta description
类型 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:140
rawUrl

返回该资源的原始文件的 URL,如果不支持 RAW 文件,它将返回一个空字符串。

meta description
类型 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:53
rawUrls

返回该资源的原文件的 URL 数组,如果不支持 RAW 文件,它将返回一个空数组。

meta description
类型 String[]
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:77
_rawFiles

在 lite 版的 Fireball 里,raw asset 并不仅仅是在 properties 里声明了 rawType 才有, 而是每个 asset 都能指定自己的 raw file url。这些 url 就存在 _rawFiles 字段中。 AssetLibrary 并不会帮你加载这些 url,除非你声明了 rawType。 在 Creator 里,_rawFiles 保留了下来,为了复用 cocos 引擎原有实现,直接用 _rawFiles 来加载 Asset 在 import 之前的源文件。

meta description
类型 String[]
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:104
_uuid
meta description
类型 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCRawAsset.js:49
_name
meta description
类型 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:50
_objFlags
meta description
类型 Number
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:57
name

该对象的名称。

meta description
类型 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:208
示例
obj.name = "New Obj";
isValid

表示该对象是否可用(被销毁后将不可用)。

meta description
类型 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:225
示例
cc.log(obj.isValid);

方法

constructor

SpriteFrame 类的构造函数。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:83
参数列表
  • filename String | Texture2D
  • rect Rect
  • rotated Boolean Whether the frame is rotated in the texture
  • offset Vec2 The offset of the frame in the texture
  • originalSize Size The size of the frame in the texture
textureLoaded

返回是否已加载纹理

meta description
返回 boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:165
addLoadedEventListener

Add a event listener for texture loaded event.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:175
废弃(Deprecated) since 3.1, please use EventTarget API instead
参数列表
isRotated

获取 SpriteFrame 是否旋转

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:186
setRotated

设置 SpriteFrame 是否旋转

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:196
参数列表
getRect

获取 SpriteFrame 的纹理矩形区域

meta description
返回 Rect
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:206
setRect

设置 SpriteFrame 的纹理矩形区域

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:216
参数列表
getOriginalSize

获取修剪前的原始大小

meta description
返回 Size
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:226
setOriginalSize

设置修剪前的原始大小

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:236
参数列表
getTexture

获取使用的纹理实例

meta description
返回 Texture2D
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:251
getOffset

获取偏移量

meta description
返回 Vec2
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:330
setOffset

设置偏移量

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:340
参数列表
clone

克隆 SpriteFrame

meta description
返回 SpriteFrame
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:350
setTexture

通过 Texture,rect,rotated,offset 和 originalSize 设置 SpriteFrame

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:360
参数列表
ensureLoadTexture

当加载中的场景或 Prefab 被标记为 asyncLoadAssets 时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。 只有当 Sprite 组件要渲染这些 SpriteFrame 时,才会检查贴图是否加载。如果你希望加载过程提前,你可以手工调用这个方法。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:418
示例
if (spriteFrame.textureLoaded()) {
    this._onSpriteFrameLoaded();
}
else {
    spriteFrame.once('load', this._onSpriteFrameLoaded, this);
    spriteFrame.ensureLoadTexture();
}
clearTexture

当你暂时不再使用这个 SpriteFrame 时,可以调用这个方法来保证引用的贴图对象能被 GC。然后当你要渲染 SpriteFrame 时,你需要手动调用 ensureLoadTexture 来重新加载贴图。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:442
示例
spriteFrame.clearTexture();
// when you need the SpriteFrame again...
spriteFrame.once('load', onSpriteFrameLoaded);
spriteFrame.ensureLoadTexture();
serialize

应 AssetDB 要求提供这个方法

meta description
返回 String
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:143
createNode

使用该资源在场景中创建一个新节点。
如果这类资源没有相应的节点类型,该方法应该是空的。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:154
参数列表
_setRawFiles

Set raw file names for this asset.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:168
参数列表
_preloadRawFiles

Preload raw files when loading scene.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:179
参数列表
on

注册事件目标的特定事件类型回调。

meta description
返回 Function
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217
参数列表
  • type String A string representing the event type to listen for.
  • callback Function The callback that will be invoked when the event is dispatched.
                           The callback is ignored if it is a duplicate (the callbacks are unique).
    
  • target Object The target (this object) to invoke the callback, can be null
  • useCapture Boolean When set to true, the capture argument prevents callback
                           from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
                           When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
                           Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
    
示例
node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
off

删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274
参数列表
  • type String A string representing the event type being removed.
  • callback Function The callback to remove.
  • target Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
  • useCapture Boolean Specifies whether the callback being removed was registered as a capturing callback or not.
                           If not specified, useCapture defaults to false. If a callback was registered twice,
                           one with capture and one without, each must be removed separately. Removal of a capturing callback
                           does not affect a non-capturing version of the same listener, and vice versa.
    
示例
// register touchEnd eventListener
var touchEnd = node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
// remove touch end event listener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);
// remove all touch end event listeners
node.off(cc.Node.EventType.TOUCH_END);
targetOff

在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329
参数列表
  • target Object The target to be searched for all related listeners
once

注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351
参数列表
  • type String A string representing the event type to listen for.
  • callback Function The callback that will be invoked when the event is dispatched.
                           The callback is ignored if it is a duplicate (the callbacks are unique).
    
  • target Object The target (this object) to invoke the callback, can be null
  • useCapture Boolean When set to true, the capture argument prevents callback
                           from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
                           When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
                           Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
    
示例
node.once(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
dispatchEvent

分发事件到事件流中。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396
参数列表
  • event Event The Event object that is dispatched into the event flow
emit

该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410
参数列表
  • message String the message to send
  • detail Any whatever argument the message needs
destroy

销毁该对象,并释放所有它对其它对象的引用。
销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 实际销毁操作会延迟到当前帧渲染前执行。

meta description
返回 Boolean
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:246
示例
obj.destroy();
_destruct

Clear all references in the instance.

NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _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
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:379
_onPreDestroy

Called before the object being destroyed.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:412
_serialize

The customized serialization for this object. (Editor Only)

meta description
返回 object
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:437
参数列表
_deserialize

Init this object from the custom serialized data.

meta description
定义于 https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:447
参数列表
  • data Object the serialized json data
  • ctx _Deserializer

results matching ""

    No results matching ""