Texture2D
类型
继承于 Asset
, EventTarget
(mixin)
This class allows to easily create OpenGL or Canvas 2D textures from images, text or raw data.
The created cc.Texture2D object will always have power-of-two dimensions.
Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions
i.e. "contentSize" != (pixelsWidth, pixelsHight) and (maxS, maxT) != (1.0, 1.0).
Be aware that the content of the generated textures will be upside-down!
索引
属性(properties)
url
String
贴图文件的 url,当贴图不是由文件创建时值可能为空width
Number
贴图像素宽度height
Number
贴图像素高度pixelFormat
Number
Pixel format of the texture.pixelWidth
Number
Width in pixels.pixelHeight
Number
Height in pixels.loaded
Boolean
该资源是否已经成功加载nativeUrl
String
返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。_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
该对象的名称。isValid
Boolean
表示该对象是否可用(被 destroy 后将不可用)。
方法
update
Update texture options, not available in Canvas render mode....getPixelWidth
Get width in pixels.getPixelHeight
Get height of in pixels.getContentSize
Get content size.getContentSizeInPixels
Get content size in pixels.initWithElement
Init with HTML element.initWithData
Intializes with a texture2d with data.initWithImage
Initializes a texture from a UIImage object....getHtmlElementObj
HTMLElement Object getter, available only on web....isLoaded
Check whether texture is loaded.handleLoadedTexture
Handler of texture loaded event.description
Description of cc.Texture2D.destroy
销毁该贴图,并立即释放它对应的显存。getPixelFormat
Pixel format of the texture.hasPremultipliedAlpha
Whether or not the texture has their Alpha premultiplied,...hasMipmaps
Whether or not use mipmap, support only in WebGl rendering mode.setTexParameters
Sets the min filter, mag filter, wrap s and wrap t texture parameters. ...setAntiAliasTexParameters
sets antialias texture parameters: ...setAliasTexParameters
Sets alias texture parameters: ...toString
Returns the asset's url....serialize
应 AssetDB 要求提供这个方法createNode
使用该资源在场景中创建一个新节点。_setRawAsset
Set native file name for this asset.on
注册事件目标的特定事件类型回调。off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent
分发事件到事件流中。emit
该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。_destruct
Clear all references in the instance...._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.
事件
load
当该资源加载成功后触发该事件
Details
属性(properties)
url
贴图文件的 url,当贴图不是由文件创建时值可能为空
meta | description |
---|---|
类型 | String |
定义于 | cocos2d/core/textures/CCTexture2D.js:205 |
width
贴图像素宽度
meta | description |
---|---|
类型 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:218 |
height
贴图像素高度
meta | description |
---|---|
类型 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:227 |
pixelFormat
Pixel format of the texture.
meta | description |
---|---|
类型 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:1027 |
pixelWidth
Width in pixels.
meta | description |
---|---|
类型 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:1034 |
废弃(Deprecated) | please use width instead |
pixelHeight
Height in pixels.
meta | description |
---|---|
类型 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:1042 |
废弃(Deprecated) | please use height instead |
loaded
该资源是否已经成功加载
meta | description |
---|---|
类型 | Boolean |
定义于 | cocos2d/core/assets/CCAsset.js:57 |
nativeUrl
返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。
meta | description |
---|---|
类型 | String |
定义于 | cocos2d/core/assets/CCAsset.js:70 |
_native
Serializable url for native asset.
meta | description |
---|---|
类型 | String |
定义于 | 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 |
---|---|
类型 | Object |
定义于 | cocos2d/core/assets/CCAsset.js:116 |
_uuid
meta | description |
---|---|
类型 | String |
定义于 | cocos2d/core/assets/CCRawAsset.js:46 |
_name
meta | description |
---|---|
类型 | String |
定义于 | cocos2d/core/platform/CCObject.js:76 |
_objFlags
meta | description |
---|---|
类型 | Number |
定义于 | cocos2d/core/platform/CCObject.js:83 |
name
该对象的名称。
meta | description |
---|---|
类型 | String |
定义于 | cocos2d/core/platform/CCObject.js:243 |
示例
obj.name = "New Obj";
isValid
表示该对象是否可用(被 destroy 后将不可用)。
当一个对象的destroy
调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始isValid
就会返回 false,而当前帧内isValid
仍然会是 true。如果希望判断当前帧是否调用过destroy
,请使用cc.isValid(obj, true)
,不过这往往是特殊的业务需求引起的,通常情况下不需要这样。
meta | description |
---|---|
类型 | Boolean |
定义于 | cocos2d/core/platform/CCObject.js:261 |
示例
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
方法
update
Update texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.
meta | description |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:282 |
参数列表
options
Object
getPixelWidth
Get width in pixels.
meta | description |
---|---|
返回 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:303 |
废弃(Deprecated) | use width or height property instead |
getPixelHeight
Get height of in pixels.
meta | description |
---|---|
返回 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:313 |
废弃(Deprecated) | use width or height property instead |
getContentSize
Get content size.
meta | description |
---|---|
返回 | Size |
定义于 | cocos2d/core/textures/CCTexture2D.js:323 |
废弃(Deprecated) | use width or height property instead |
getContentSizeInPixels
Get content size in pixels.
meta | description |
---|---|
返回 | Size |
定义于 | cocos2d/core/textures/CCTexture2D.js:333 |
废弃(Deprecated) | use width or height property instead |
initWithElement
Init with HTML element.
meta | description |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:343 |
参数列表
element
HTMLImageElement | HTMLCanvasElement
示例
var img = new Image();
img.src = dataURL;
texture.initWithElement(img);
texture.handleLoadedTexture();
initWithData
Intializes with a texture2d with data.
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/textures/CCTexture2D.js:362 |
参数列表
data
TypedArraypixelFormat
NumberpixelsWidth
NumberpixelsHeight
NumbercontentSize
Size contentSize is deprecated and ignored
initWithImage
Initializes a texture from a UIImage object. Extensions to make it easy to create a CCTexture2D object from an image file. Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/textures/CCTexture2D.js:377 |
参数列表
uiImage
HTMLImageElement
getHtmlElementObj
HTMLElement Object getter, available only on web. In most case, it will return null, because we are recycling the dom image element for better loading performance and lower image cache memory usage.
meta | description |
---|---|
返回 | HTMLImageElement | HTMLCanvasElement |
定义于 | cocos2d/core/textures/CCTexture2D.js:390 |
isLoaded
Check whether texture is loaded.
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/textures/CCTexture2D.js:428 |
废弃(Deprecated) | use `loaded` property instead |
handleLoadedTexture
Handler of texture loaded event.
meta | description |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:441 |
description
Description of cc.Texture2D.
meta | description |
---|---|
返回 | String |
定义于 | cocos2d/core/textures/CCTexture2D.js:458 |
destroy
销毁该贴图,并立即释放它对应的显存。(继承自 cc.Object.destroy)
销毁后,该对象不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
meta | description |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:474 |
getPixelFormat
Pixel format of the texture.
meta | description |
---|---|
返回 | Number |
定义于 | cocos2d/core/textures/CCTexture2D.js:490 |
hasPremultipliedAlpha
Whether or not the texture has their Alpha premultiplied, support only in WebGl rendering mode.
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/textures/CCTexture2D.js:500 |
hasMipmaps
Whether or not use mipmap, support only in WebGl rendering mode.
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/textures/CCTexture2D.js:510 |
setTexParameters
Sets the min filter, mag filter, wrap s and wrap t texture parameters.
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 |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:519 |
废弃(Deprecated) | use update function with filter and wrap options instead |
参数列表
texParams
Object | Number texParams object or minFiltermagFilter
NumberwrapS
Texture2D.WrapModewrapT
Texture2D.WrapMode
setAntiAliasTexParameters
sets antialias texture parameters:
- GL_TEXTURE_MIN_FILTER = GL_NEAREST
- GL_TEXTURE_MAG_FILTER = GL_NEAREST
supported only in native or WebGl rendering mode
meta | description |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:548 |
废弃(Deprecated) | use update function with filter options instead |
setAliasTexParameters
Sets alias texture parameters:
GL_TEXTURE_MIN_FILTER = GL_NEAREST
GL_TEXTURE_MAG_FILTER = GL_NEAREST
supported only in native or WebGl rendering mode
meta | description |
---|---|
定义于 | cocos2d/core/textures/CCTexture2D.js:560 |
废弃(Deprecated) | use update function with filter options instead |
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 |
---|---|
返回 | String |
定义于 | cocos2d/core/assets/CCAsset.js:165 |
serialize
应 AssetDB 要求提供这个方法
meta | description |
---|---|
返回 | String |
定义于 | cocos2d/core/assets/CCAsset.js:179 |
createNode
使用该资源在场景中创建一个新节点。
如果这类资源没有相应的节点类型,该方法应该是空的。
meta | description |
---|---|
定义于 | cocos2d/core/assets/CCAsset.js:190 |
参数列表
_setRawAsset
Set native file name for this asset.
meta | description |
---|---|
定义于 | cocos2d/core/assets/CCAsset.js:205 |
参数列表
on
注册事件目标的特定事件类型回调。
meta | description |
---|---|
返回 | Function |
定义于 | cocos2d/core/event/event-target.js:218 |
参数列表
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.
示例
node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
meta | description |
---|---|
定义于 | cocos2d/core/event/event-target.js:275 |
参数列表
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.
示例
// 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 |
---|---|
定义于 | cocos2d/core/event/event-target.js:330 |
参数列表
target
Object The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
meta | description |
---|---|
定义于 | cocos2d/core/event/event-target.js:352 |
参数列表
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.
示例
node.once(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
dispatchEvent
分发事件到事件流中。
meta | description |
---|---|
定义于 | cocos2d/core/event/event-target.js:397 |
参数列表
event
Event The Event object that is dispatched into the event flow
emit
该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。
meta | description |
---|---|
定义于 | cocos2d/core/event/event-target.js:411 |
参数列表
message
String the message to senddetail
Any whatever argument the message needs
_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 |
---|---|
定义于 | cocos2d/core/platform/CCObject.js:428 |
_onPreDestroy
Called before the object being destroyed.
meta | description |
---|---|
定义于 | cocos2d/core/platform/CCObject.js:461 |
_serialize
The customized serialization for this object. (Editor Only)
meta | description |
---|---|
返回 | object |
定义于 | cocos2d/core/platform/CCObject.js:486 |
参数列表
exporting
Boolean
_deserialize
Init this object from the custom serialized data.
meta | description |
---|---|
定义于 | cocos2d/core/platform/CCObject.js:496 |
参数列表
data
Object the serialized json datactx
_Deserializer
事件
load
Event
当该资源加载成功后触发该事件