SpriteFrame Class
Extends Asset, EventTarget(mixin)
Module: cc
A cc.SpriteFrame has:
- texture: A cc.Texture2D that will be used by render components
- rectangle: A rectangle of the texture
Examples
// load a cc.SpriteFrame with image path (Recommend)
var self = this;
var url = "test assets/PurpleMonster";
cc.loader.loadRes(url, cc.SpriteFrame, function (err, spriteFrame) {
var node = new cc.Node("New Sprite");
var sprite = node.addComponent(cc.Sprite);
sprite.spriteFrame = spriteFrame;
node.parent = self.node
});
Index
Properties
insetTopNumberTop border of the spriteinsetBottomNumberBottom border of the spriteinsetLeftNumberLeft border of the spriteinsetRightNumberRight border of the spriteloadedBooleanWhether the asset is loaded or notnativeUrlStringReturns the url of this asset's native object, if none it will returns an empty string._nativeStringSerializable url for native asset._nativeAssetObjectThe underlying native asset of this asset if one is available._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 loadedisRotatedReturns 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.ensureLoadTextureassociated by user's custom Components in the scene, will not preload automatically.clearTextureIf you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected.hasEventListenerChecks whether the EventTarget object has any callback registered for a specific type of event.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,...targetOffRemoves all callbacks previously registered with the same target (passed as parameter).onceRegister an callback of a specific event type on the EventTarget,...emitTrigger an event directly with the event name and necessary arguments.dispatchEventSend an event with the event object.toStringReturns the asset's url.serialize应 AssetDB 要求提供这个方法createNodeCreate a new node using this asset in the scene...._setRawAssetSet native file name for this asset.destroyActual object destruction will delayed until before rendering._destructClear all references in the instance._onPreDestroyCalled before the object being destroyed._serializeThe customized serialization for this object._deserializeInit this object from the custom serialized data.
Details
Properties
insetTop
Top border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:99 |
insetBottom
Bottom border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:118 |
insetLeft
Left border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:137 |
insetRight
Right border of the sprite
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:156 |
loaded
Whether the asset is loaded or not
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/assets/CCAsset.js:57 |
nativeUrl
Returns the url of this asset's native object, if none it will returns an empty string.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCAsset.js:70 |
_native
Serializable url for native asset.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCAsset.js:108 |
_nativeAsset
The underlying native asset of this asset if one is available. This property can be used to access additional details or functionality releated to the asset. This property will be initialized by the loader if
_nativeis available.
| meta | description |
|---|---|
| Type | Object |
| Defined in | cocos2d/core/assets/CCAsset.js:116 |
_uuid
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCRawAsset.js:46 |
_name
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/platform/CCObject.js:76 |
_objFlags
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/platform/CCObject.js:83 |
name
The name of the object.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/platform/CCObject.js:243 |
Examples
obj.name = "New Obj";
isValid
Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
When an object'sdestroyis called, it is actually destroyed after the end of this frame. SoisValidwill return false from the next frame, whileisValidin the current frame will still be true. If you want to determine whether the current frame has calleddestroy, usecc.isValid(obj, true), but this is often caused by a particular logical requirements, which is not normally required.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/platform/CCObject.js:261 |
Examples
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
Methods
constructor
Constructor of SpriteFrame class.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:176 |
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 | cocos2d/core/assets/CCSpriteFrame.js:235 |
isRotated
Returns whether the sprite frame is rotated in the texture.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:245 |
setRotated
Set whether the sprite frame is rotated in the texture.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:255 |
Parameters
bRotatedBoolean
getRect
Returns the rect of the sprite frame in the texture.
| meta | description |
|---|---|
| Returns | Rect |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:267 |
setRect
Sets the rect of the sprite frame in the texture.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:277 |
Parameters
rectRect
getOriginalSize
Returns the original size of the trimmed image.
| meta | description |
|---|---|
| Returns | Size |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:289 |
setOriginalSize
Sets the original size of the trimmed image.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:299 |
Parameters
sizeSize
getTexture
Returns the texture of the frame.
| meta | description |
|---|---|
| Returns | Texture2D |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:314 |
getOffset
Returns the offset of the frame in the texture.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:379 |
setOffset
Sets the offset of the frame in the texture.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:389 |
Parameters
offsetsVec2
clone
Clone the sprite frame.
| meta | description |
|---|---|
| Returns | SpriteFrame |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:399 |
setTexture
Set SpriteFrame with Texture, rect, rotated, offset and originalSize.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/assets/CCSpriteFrame.js:409 |
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 | cocos2d/core/assets/CCSpriteFrame.js:464 |
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 | cocos2d/core/assets/CCSpriteFrame.js:496 |
Examples
spriteFrame.clearTexture();
// when you need the SpriteFrame again...
spriteFrame.once('load', onSpriteFrameLoaded);
spriteFrame.ensureLoadTexture();
hasEventListener
Checks whether the EventTarget object has any callback registered for a specific type of event.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/event/event-target.js:68 |
Parameters
typeString The type of event.
on
Register an callback of a specific event type on the EventTarget.
This type of event should be triggered via emit.
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/event/event-target.js:76 |
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).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
Examples
eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, 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 | cocos2d/core/event/event-target.js:116 |
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 removed
Examples
// 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
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 | cocos2d/core/event/event-target.js:150 |
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 | cocos2d/core/event/event-target.js:163 |
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).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
Examples
eventTarget.once('fire', function () {
cc.log("this is the callback and will be invoked only once");
}, node);
emit
Trigger an event directly with the event name and necessary arguments.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:200 |
Parameters
typeString event typearg1Any First argumentarg2Any Second argumentarg3Any Third argumentarg4Any Fourth argumentarg5Any Fifth argument
Examples
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);
dispatchEvent
Send an event with the event object.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:220 |
Parameters
eventEvent
toString
Returns the asset's url.
The Asset object overrides the toString() method of the Object object.
For Asset objects, the toString() method returns a string representation of the object.
JavaScript calls the toString() method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation.
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/assets/CCAsset.js:169 |
serialize
应 AssetDB 要求提供这个方法
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/assets/CCAsset.js:183 |
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 | cocos2d/core/assets/CCAsset.js:194 |
Parameters
_setRawAsset
Set native file name for this asset.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCAsset.js:209 |
Parameters
destroy
Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
From the next frame, this object 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 | cocos2d/core/platform/CCObject.js:296 |
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 | cocos2d/core/platform/CCObject.js:430 |
_onPreDestroy
Called before the object being destroyed.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:463 |
_serialize
The customized serialization for this object. (Editor Only)
| meta | description |
|---|---|
| Returns | object |
| Defined in | cocos2d/core/platform/CCObject.js:488 |
Parameters
exportingBoolean
_deserialize
Init this object from the custom serialized data.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:498 |
Parameters
dataObject the serialized json datactx_Deserializer