SpriteFrame Class
Extends Asset
A cc.SpriteFrame has:
- texture: A cc.Texture2D that will be used by the _ccsg.Sprite
- rectangle: A rectangle of the texture
Index
Properties
_textureFilenameSetterStringUse this property to set raw texture url during loadinginsetTopNumberTop border of the spriteinsetBottomNumberBottom border of the spriteinsetLeftNumberLeft border of the spriteinsetRightNumberRight border of the spriterawUrlStringReturns the url of this asset's first raw file, if none of rawFile exists, it will returns an empty string.rawUrlsString[]Returns the url of this asset's raw files, if none of rawFile exists, it will returns an empty array._rawFilesString[]在 lite 版的 Fireball 里,raw asset 并不仅仅是在 properties 里声明了 rawType 才有, 而是每个 asset 都能指定自己的 raw file url。这些 url 就存在 _rawFiles 字段中。 AssetLibrary 并不会帮你加载这些 url,除非你声明了 rawType。 在 Creator 里,_rawFiles 保留了下来,为了复用 cocos 引擎原有实现,直接用 _rawFiles 来加载 Asset 在 import 之前的源文件。_uuidString_nameString_objFlagsNumbernameStringThe name of the object.isValidBooleanIndicates whether the object is not yet destroyed.
Methods
constructorConstructor of SpriteFrame class.textureLoadedReturns whether the texture have been loadedaddLoadedEventListenerAdd a event listener for texture loaded event.isRotatedReturns whether the sprite frame is rotated in the texture.setRotatedSet whether the sprite frame is rotated in the texture.getRectReturns the rect of the sprite frame in the texture.setRectSets the rect of the sprite frame in the texture.getOriginalSizeReturns the original size of the trimmed image.setOriginalSizeSets the original size of the trimmed image.getTextureReturns the texture of the frame.getOffsetReturns the offset of the frame in the texture.setOffsetSets the offset of the frame in the texture.cloneClone the sprite frame.setTextureSet SpriteFrame with Texture, rect, rotated, offset and originalSize.ensureLoadTextureIf a loading scene (or prefab) is marked asasyncLoadAssets, all the textures of the SpriteFrame which associated by user's custom Components in the scene, will not preload automatically. These textures will be load when Sprite component is going to render the SpriteFrames. You can call this method if you want to load the texture early.clearTextureIf you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected. Then when you need to render the SpriteFrame, you should callensureLoadTexturemanually to reload texture.serialize应 AssetDB 要求提供这个方法createNodeCreate a new node using this asset in the scene.
If this type of asset dont have its corresponding node type, this method should be null._setRawFilesSet raw file names for this asset._preloadRawFilesPreload raw files when loading scene.onRegister an callback of a specific event type on the EventTarget.offRemoves the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.targetOffRemoves all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.onceRegister an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.dispatchEventDispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.emitSend an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.destroyDestroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
After destroy, this CCObject is not usable any more. You can use cc.isValid(obj) to check whether the object is destroyed before accessing it._destructClear 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; } } }
_onPreDestroyCalled before the object being destroyed._serializeThe customized serialization for this object. (Editor Only)_deserializeInit this object from the custom serialized data.
Details
Properties
_textureFilenameSetter
Use this property to set raw texture url during loading
| meta | description |
|---|---|
| Type | String |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:63 |
insetTop
Top border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:113 |
insetBottom
Bottom border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:122 |
insetLeft
Left border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:131 |
insetRight
Right border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:140 |
rawUrl
Returns the url of this asset's first raw file, if none of rawFile exists, it will returns an empty string.
| meta | description |
|---|---|
| Type | String |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:53 |
rawUrls
Returns the url of this asset's raw files, if none of rawFile exists, it will returns an empty array.
| meta | description |
|---|---|
| Type | String[] |
| Defined in | 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 |
|---|---|
| Type | String[] |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:104 |
_uuid
| meta | description |
|---|---|
| Type | String |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCRawAsset.js:49 |
_name
| meta | description |
|---|---|
| Type | String |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:50 |
_objFlags
| meta | description |
|---|---|
| Type | Number |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:57 |
name
The name of the object.
| meta | description |
|---|---|
| Type | String |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:208 |
Examples
obj.name = "New Obj";
isValid
Indicates whether the object is not yet destroyed.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:225 |
Examples
cc.log(obj.isValid);
Methods
constructor
Constructor of SpriteFrame class.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:83 |
Parameters
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
Returns whether the texture have been loaded
| meta | description |
|---|---|
| Returns | boolean |
| Defined in | 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 |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:175 |
| Deprecated | since 3.1, please use EventTarget API instead |
Parameters
isRotated
Returns whether the sprite frame is rotated in the texture.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:186 |
setRotated
Set whether the sprite frame is rotated in the texture.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:196 |
Parameters
bRotatedBoolean
getRect
Returns the rect of the sprite frame in the texture.
| meta | description |
|---|---|
| Returns | Rect |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:206 |
setRect
Sets the rect of the sprite frame in the texture.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:216 |
Parameters
rectRect
getOriginalSize
Returns the original size of the trimmed image.
| meta | description |
|---|---|
| Returns | Size |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:226 |
setOriginalSize
Sets the original size of the trimmed image.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:236 |
Parameters
sizeSize
getTexture
Returns the texture of the frame.
| meta | description |
|---|---|
| Returns | Texture2D |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:251 |
getOffset
Returns the offset of the frame in the texture.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:330 |
setOffset
Sets the offset of the frame in the texture.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:340 |
Parameters
offsetsVec2
clone
Clone the sprite frame.
| meta | description |
|---|---|
| Returns | SpriteFrame |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:350 |
setTexture
Set SpriteFrame with Texture, rect, rotated, offset and originalSize.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:360 |
Parameters
ensureLoadTexture
If a loading scene (or prefab) is marked as asyncLoadAssets, all the textures of the SpriteFrame which
associated by user's custom Components in the scene, will not preload automatically.
These textures will be load when Sprite component is going to render the SpriteFrames.
You can call this method if you want to load the texture early.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:418 |
Examples
if (spriteFrame.textureLoaded()) {
this._onSpriteFrameLoaded();
}
else {
spriteFrame.once('load', this._onSpriteFrameLoaded, this);
spriteFrame.ensureLoadTexture();
}
clearTexture
If you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected. Then when you need to render the SpriteFrame, you should call ensureLoadTexture manually to reload texture.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/sprites/CCSpriteFrame.js:442 |
Examples
spriteFrame.clearTexture();
// when you need the SpriteFrame again...
spriteFrame.once('load', onSpriteFrameLoaded);
spriteFrame.ensureLoadTexture();
serialize
应 AssetDB 要求提供这个方法
| meta | description |
|---|---|
| Returns | String |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:143 |
createNode
Create a new node using this asset in the scene.
If this type of asset dont have its corresponding node type, this method should be null.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:154 |
Parameters
_setRawFiles
Set raw file names for this asset.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:168 |
Parameters
rawFilesString[]
_preloadRawFiles
Preload raw files when loading scene.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCAsset.js:179 |
Parameters
on
Register an callback of a specific event type on the EventTarget.
| meta | description |
|---|---|
| Returns | Function |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217 |
Parameters
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).eventEvent event
targetObject The target (this object) to invoke the callback, can be nulluseCaptureBoolean When set to true, the capture argument prevents callbackfrom 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.
Examples
node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
off
Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274 |
Parameters
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 removeduseCaptureBoolean 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.
Examples
// 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
Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329 |
Parameters
targetObject The target to be searched for all related listeners
once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 |
Parameters
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).eventEvent event
targetObject The target (this object) to invoke the callback, can be nulluseCaptureBoolean When set to true, the capture argument prevents callbackfrom 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.
Examples
node.once(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
dispatchEvent
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 |
Parameters
eventEvent The Event object that is dispatched into the event flow
emit
Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 |
Parameters
messageString the message to senddetailAny whatever argument the message needs
destroy
Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
After destroy, this CCObject is not usable any more.
You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:246 |
Examples
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 |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:379 |
_onPreDestroy
Called before the object being destroyed.
| meta | description |
|---|---|
| Defined in | 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 |
|---|---|
| Returns | object |
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:437 |
Parameters
exportingBoolean
_deserialize
Init this object from the custom serialized data.
| meta | description |
|---|---|
| Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:447 |
Parameters
dataObject the serialized json datactx_Deserializer