Texture2D
Class
Extends Asset
, EventTarget
(mixin)
Module: cc
This class allows to easily create OpenGL or Canvas 2D textures from images or raw data.
Index
Properties
genMipmaps
Boolean
Sets whether generate mipmaps for the texturepackable
Boolean
Sets whether texture can be packed into texture atlas.loaded
Boolean
Whether the texture is loaded or notwidth
Number
Texture width in pixelheight
Number
Texture height in pixelurl
String
Points to the true url of this asset's native object, only valid when asset is loaded and asyncLoadAsset is not enabled.nativeUrl
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
getImpl
Get renderer texture implementation objectupdate
Update texture options, not available in Canvas render mode.initWithElement
Init with HTML element.initWithData
Intializes with a texture2d with data in Uint8Array.getHtmlElementObj
HTMLElement Object getter, available only on web....destroy
Destory this texture and immediately release its video memory.getPixelFormat
Pixel format of the texture.hasPremultipliedAlpha
Whether or not the texture has their Alpha premultiplied.handleLoadedTexture
Handler of texture loaded event.description
Description of cc.Texture2D.releaseTexture
Release texture, please use destroy instead.setTexParameters
Sets the wrap s and wrap t options.setFilters
Sets the minFilter and magFilter optionssetFlipY
Sets the flipY optionssetPremultiplyAlpha
Sets the premultiply alpha optionstoString
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.hasEventListener
Checks whether the EventTarget object has any callback registered for a specific type of event.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
Send an event with the event object._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.
Events
load
This event is emitted when the asset is loaded
Details
Properties
genMipmaps
Sets whether generate mipmaps for the texture
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/assets/CCTexture2D.js:329 |
packable
Sets whether texture can be packed into texture atlas. If need use texture uv in custom Effect, please sets packable to false.
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/assets/CCTexture2D.js:349 |
loaded
Whether the texture is loaded or not
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/assets/CCTexture2D.js:383 |
width
Texture width in pixel
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/assets/CCTexture2D.js:392 |
height
Texture height in pixel
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/assets/CCTexture2D.js:401 |
url
Points to the true url of this asset's native object, only valid when asset is loaded and asyncLoadAsset is not enabled. The difference between nativeUrl and url is that the latter is final path, there is no needs to transform url by md5 and subpackage. Besides, url may points to temporary path or cached path on mini game platform which has cache mechanism (WeChat etc). If you want to make use of the native file on those platforms, you should use url instead of nativeUrl.
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/assets/CCAsset.js:68 |
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:85 |
_native
Serializable url for native asset.
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/assets/CCAsset.js:123 |
_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:131 |
_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:240 |
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:258 |
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
getImpl
Get renderer texture implementation object extended from render.Texture2D
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:420 |
update
Update texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:439 |
Parameters
options
Object
initWithElement
Init with HTML element.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:517 |
Parameters
element
HTMLImageElement | HTMLCanvasElement
Examples
var img = new Image();
img.src = dataURL;
texture.initWithElement(img);
initWithData
Intializes with a texture2d with data in Uint8Array.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/assets/CCTexture2D.js:546 |
Parameters
getHtmlElementObj
HTMLElement Object getter, available only on web.
Note: texture is packed into texture atlas by default
you should set texture.packable as false before getting Html element object.
meta | description |
---|---|
Returns | HTMLImageElement | HTMLCanvasElement |
Defined in | cocos2d/core/assets/CCTexture2D.js:588 |
destroy
Destory this texture and immediately release its video memory. (Inherit from cc.Object.destroy)
After destroy, 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/assets/CCTexture2D.js:603 |
getPixelFormat
Pixel format of the texture.
meta | description |
---|---|
Returns | Number |
Defined in | cocos2d/core/assets/CCTexture2D.js:624 |
hasPremultipliedAlpha
Whether or not the texture has their Alpha premultiplied.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/assets/CCTexture2D.js:636 |
handleLoadedTexture
Handler of texture loaded event. Since v2.0, you don't need to invoke this function, it will be invoked automatically after texture loaded.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:647 |
Parameters
premultiplied
Boolean
description
Description of cc.Texture2D.
meta | description |
---|---|
Returns | String |
Defined in | cocos2d/core/assets/CCTexture2D.js:694 |
releaseTexture
Release texture, please use destroy instead.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:705 |
Deprecated | since v2.0 |
setTexParameters
Sets the wrap s and wrap t options.
If the texture size is NPOT (non power of 2), then in can only use gl.CLAMPTO_EDGE in gl.TEXTURE_WRAP{S,T}.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:717 |
Parameters
wrapS
Texture2D.WrapModewrapT
Texture2D.WrapMode
setFilters
Sets the minFilter and magFilter options
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:735 |
Parameters
minFilter
Texture2D.FiltermagFilter
Texture2D.Filter
setFlipY
Sets the flipY options
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:751 |
Parameters
flipY
Boolean
setPremultiplyAlpha
Sets the premultiply alpha options
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCTexture2D.js:766 |
Parameters
premultiply
Boolean
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:184 |
serialize
应 AssetDB 要求提供这个方法
meta | description |
---|---|
Returns | String |
Defined in | cocos2d/core/assets/CCAsset.js:198 |
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:209 |
Parameters
_setRawAsset
Set native file name for this asset.
meta | description |
---|---|
Defined in | cocos2d/core/assets/CCAsset.js:224 |
Parameters
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:69 |
Parameters
type
String 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:77 |
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).
arg1
Any arg1arg2
Any arg2arg3
Any arg3arg4
Any arg4arg5
Any arg5
target
Object 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:119 |
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 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:163 |
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:182 |
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).
arg1
Any arg1arg2
Any arg2arg3
Any arg3arg4
Any arg4arg5
Any arg5
target
Object 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);
dispatchEvent
Send an event with the event object.
meta | description |
---|---|
Defined in | cocos2d/core/event/event-target.js:208 |
Parameters
event
Event
_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:427 |
_onPreDestroy
Called before the object being destroyed.
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:460 |
_serialize
The customized serialization for this object. (Editor Only)
meta | description |
---|---|
Returns | object |
Defined in | cocos2d/core/platform/CCObject.js:485 |
Parameters
exporting
Boolean
_deserialize
Init this object from the custom serialized data.
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:495 |
Parameters
data
Object the serialized json datactx
_Deserializer
Events
load
Event
Module: cc
This event is emitted when the asset is loaded