SpriteFrame
Class
Extends Asset
, EventTarget
(mixin)
A cc.SpriteFrame has:
- texture: A cc.Texture2D that will be used by the _ccsg.Sprite
- rectangle: A rectangle of the texture
Index
Properties
insetTop
Number
Top border of the spriteinsetBottom
Number
Bottom border of the spriteinsetLeft
Number
Left border of the spriteinsetRight
Number
Right border of the spriteloaded
Boolean
Whether the asset is loaded or notnativeUrl
String
Returns the url of this asset's native object, if none it will returns an empty string._native
String
Serializable url for native asset._nativeAsset
Object
The underlying native asset of this asset if one is available._uuid
String
_name
String
_objFlags
Number
name
String
The name of the object.isValid
Boolean
Indicates whether the object is not yet destroyed.
Methods
constructor
Constructor of SpriteFrame class.textureLoaded
Returns whether the texture have been loadedaddLoadedEventListener
Add a event listener for texture loaded event.isRotated
Returns whether the sprite frame is rotated in the texture.setRotated
Set whether the sprite frame is rotated in the texture.getRect
Returns the rect of the sprite frame in the texture.setRect
Sets the rect of the sprite frame in the texture.getOriginalSize
Returns the original size of the trimmed image.setOriginalSize
Sets the original size of the trimmed image.getTexture
Returns the texture of the frame.getOffset
Returns the offset of the frame in the texture.setOffset
Sets the offset of the frame in the texture.clone
Clone the sprite frame.setTexture
Set SpriteFrame with Texture, rect, rotated, offset and originalSize.ensureLoadTexture
associated by user's custom Components in the scene, will not preload automatically.clearTexture
If you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected.on
Register an callback of a specific event type on the EventTarget.off
Removes the listeners previously registered with the same type, callback, target and or useCapture,...targetOff
Removes all callbacks previously registered with the same target (passed as parameter).once
Register an callback of a specific event type on the EventTarget,...dispatchEvent
Dispatches an event into the event flow.emit
Send an event to this object directly, this method will not propagate the event to any other objects.toString
Returns the asset's url.serialize
应 AssetDB 要求提供这个方法createNode
Create a new node using this asset in the scene...._setRawAsset
Set native file name for this asset.destroy
Actual object destruction will delayed until before rendering._destruct
Clear all references in the instance._onPreDestroy
Called before the object being destroyed._serialize
The customized serialization for this object._deserialize
Init this object from the custom serialized data.
Details
Properties
insetTop
Top border of the sprite
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:122 |
insetBottom
Bottom border of the sprite
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:131 |
insetLeft
Left border of the sprite
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:140 |
insetRight
Right border of the sprite
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:149 |
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
_native
is 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'sdestroy
is called, it is actually destroyed after the end of this frame. SoisValid
will return false from the next frame, whileisValid
in 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/sprites/CCSpriteFrame.js:92 |
Parameters
filename
String | Texture2Drect
Rectrotated
Boolean Whether the frame is rotated in the textureoffset
Vec2 The offset of the frame in the textureoriginalSize
Size The size of the frame in the texture
textureLoaded
Returns whether the texture have been loaded
meta | description |
---|---|
Returns | boolean |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:173 |
addLoadedEventListener
Add a event listener for texture loaded event.
meta | description |
---|---|
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:183 |
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 | cocos2d/core/sprites/CCSpriteFrame.js:194 |
setRotated
Set whether the sprite frame is rotated in the texture.
meta | description |
---|---|
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:204 |
Parameters
bRotated
Boolean
getRect
Returns the rect of the sprite frame in the texture.
meta | description |
---|---|
Returns | Rect |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:214 |
setRect
Sets the rect of the sprite frame in the texture.
meta | description |
---|---|
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:224 |
Parameters
rect
Rect
getOriginalSize
Returns the original size of the trimmed image.
meta | description |
---|---|
Returns | Size |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:234 |
setOriginalSize
Sets the original size of the trimmed image.
meta | description |
---|---|
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:244 |
Parameters
size
Size
getTexture
Returns the texture of the frame.
meta | description |
---|---|
Returns | Texture2D |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:259 |
getOffset
Returns the offset of the frame in the texture.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:321 |
setOffset
Sets the offset of the frame in the texture.
meta | description |
---|---|
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:331 |
Parameters
offsets
Vec2
clone
Clone the sprite frame.
meta | description |
---|---|
Returns | SpriteFrame |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:341 |
setTexture
Set SpriteFrame with Texture, rect, rotated, offset and originalSize.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/sprites/CCSpriteFrame.js:351 |
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/sprites/CCSpriteFrame.js:398 |
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/sprites/CCSpriteFrame.js:431 |
Examples
spriteFrame.clearTexture();
// when you need the SpriteFrame again...
spriteFrame.once('load', onSpriteFrameLoaded);
spriteFrame.ensureLoadTexture();
on
Register an callback of a specific event type on the EventTarget.
meta | description |
---|---|
Returns | Function |
Defined in | cocos2d/core/event/event-target.js:218 |
Parameters
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).
event
Event event
target
Object The target (this object) to invoke the callback, can be nulluseCapture
Boolean 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 | cocos2d/core/event/event-target.js:275 |
Parameters
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 removeduseCapture
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.
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 | cocos2d/core/event/event-target.js:330 |
Parameters
target
Object 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:352 |
Parameters
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).
event
Event event
target
Object The target (this object) to invoke the callback, can be nulluseCapture
Boolean 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 | cocos2d/core/event/event-target.js:397 |
Parameters
event
Event 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 | cocos2d/core/event/event-target.js:411 |
Parameters
message
String the message to senddetail
Any whatever argument the message needs
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:165 |
serialize
应 AssetDB 要求提供这个方法
meta | description |
---|---|
Returns | String |
Defined in | cocos2d/core/assets/CCAsset.js:179 |
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:190 |
Parameters
_setRawAsset
Set native file name for this asset.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCAsset.js:205 |
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:428 |
_onPreDestroy
Called before the object being destroyed.
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:461 |
_serialize
The customized serialization for this object. (Editor Only)
meta | description |
---|---|
Returns | object |
Defined in | cocos2d/core/platform/CCObject.js:486 |
Parameters
exporting
Boolean
_deserialize
Init this object from the custom serialized data.
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:496 |
Parameters
data
Object the serialized json datactx
_Deserializer