Texture2D 类型
继承于 RawAsset
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" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0).
Be aware that the content of the generated textures will be upside-down!
索引
属性(properties)
urlString贴图文件的 url,当贴图不是由文件创建时值可能为空loadedBoolean贴图是否已经成功加载widthNumber贴图像素宽度heightNumber贴图像素高度pixelFormatNumberPixel format of the texture.pixelWidthNumberWidth in pixels.pixelHeightNumberHeight in pixels._uuidString_nameString_objFlagsNumbernameString该对象的名称。isValidBoolean表示该对象是否可用(被销毁后将不可用)。
方法
updateUpdate texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.getPixelWidthGet width in pixels.getPixelHeightGet height of in pixels.getContentSizeGet content size.getContentSizeInPixelsGet content size in pixels.initWithElementInit with HTML element.initWithDataIntializes with a texture2d with data.initWithImageInitializes 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).getHtmlElementObjHTMLElement 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.isLoadedCheck whether texture is loaded.handleLoadedTextureHandler of texture loaded event.descriptionDescription of cc.Texture2D.releaseTextureRelease texture.getPixelFormatPixel format of the texture.hasPremultipliedAlphaWhether or not the texture has their Alpha premultiplied, support only in WebGl rendering mode.hasMipmapsWhether or not use mipmap, support only in WebGl rendering mode.setTexParametersSets 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}.setAntiAliasTexParameterssets antialias texture parameters:- GL_TEXTURE_MIN_FILTER = GL_NEAREST
- GL_TEXTURE_MAG_FILTER = GL_NEAREST
supported only in native or WebGl rendering mode
- GL_TEXTURE_MIN_FILTER = GL_NEAREST
setAliasTexParametersSets alias texture parameters:
GL_TEXTURE_MIN_FILTER = GL_NEAREST
GL_TEXTURE_MAG_FILTER = GL_NEAREST
supported only in native or WebGl rendering modeon注册事件目标的特定事件类型回调。off删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。once注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent分发事件到事件流中。emit该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。destroy销毁该对象,并释放所有它对其它对象的引用。
销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 实际销毁操作会延迟到当前帧渲染前执行。_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)
url
贴图文件的 url,当贴图不是由文件创建时值可能为空
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:223 |
loaded
贴图是否已经成功加载
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:232 |
width
贴图像素宽度
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:241 |
height
贴图像素高度
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:250 |
pixelFormat
Pixel format of the texture.
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:927 |
pixelWidth
Width in pixels.
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:934 |
| 废弃(Deprecated) | please use width instead |
pixelHeight
Height in pixels.
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:942 |
| 废弃(Deprecated) | please use height instead |
_uuid
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/assets/CCRawAsset.js:49 |
_name
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:50 |
_objFlags
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:57 |
name
该对象的名称。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:208 |
示例
obj.name = "New Obj";
isValid
表示该对象是否可用(被销毁后将不可用)。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:225 |
示例
cc.log(obj.isValid);
方法
update
Update texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:275 |
参数列表
optionsObject
getPixelWidth
Get width in pixels.
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:292 |
| 废弃(Deprecated) | use width or height property instead |
getPixelHeight
Get height of in pixels.
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:302 |
| 废弃(Deprecated) | use width or height property instead |
getContentSize
Get content size.
| meta | description |
|---|---|
| 返回 | Size |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:312 |
| 废弃(Deprecated) | use width or height property instead |
getContentSizeInPixels
Get content size in pixels.
| meta | description |
|---|---|
| 返回 | Size |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:322 |
| 废弃(Deprecated) | use width or height property instead |
initWithElement
Init with HTML element.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:332 |
参数列表
elementHTMLImageElement | HTMLCanvasElement
示例
var img = new Image();
img.src = dataURL;
texture.initWithElement(img);
texture.handleLoadedTexture();
initWithData
Intializes with a texture2d with data.
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:351 |
参数列表
dataTypedArraypixelFormatNumberpixelsWidthNumberpixelsHeightNumbercontentSizeSize 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 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:366 |
参数列表
uiImageHTMLImageElement
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 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:379 |
isLoaded
Check whether texture is loaded.
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:389 |
| 废弃(Deprecated) | use loaded property instead |
handleLoadedTexture
Handler of texture loaded event.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:399 |
参数列表
premultipliedBoolean
description
Description of cc.Texture2D.
| meta | description |
|---|---|
| 返回 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:417 |
releaseTexture
Release texture.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:426 |
getPixelFormat
Pixel format of the texture.
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:436 |
hasPremultipliedAlpha
Whether or not the texture has their Alpha premultiplied, support only in WebGl rendering mode.
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:446 |
hasMipmaps
Whether or not use mipmap, support only in WebGl rendering mode.
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:456 |
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 |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:465 |
| 废弃(Deprecated) | use update function with filter and wrap options instead |
参数列表
texParamsObject | Number texParams object or minFiltermagFilterNumberwrapSTexture2D.WrapModewrapTTexture2D.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 |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:494 |
| 废弃(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 |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/textures/CCTexture2D.js:506 |
| 废弃(Deprecated) | use update function with filter options instead |
on
注册事件目标的特定事件类型回调。
| meta | description |
|---|---|
| 返回 | Function |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217 |
参数列表
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.
示例
node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274 |
参数列表
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.
示例
// 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 |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329 |
参数列表
targetObject The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 |
参数列表
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.
示例
node.once(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
dispatchEvent
分发事件到事件流中。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 |
参数列表
eventEvent The Event object that is dispatched into the event flow
emit
该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 |
参数列表
messageString the message to senddetailAny whatever argument the message needs
destroy
销毁该对象,并释放所有它对其它对象的引用。
销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
实际销毁操作会延迟到当前帧渲染前执行。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:246 |
示例
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 |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:379 |
_onPreDestroy
Called before the object being destroyed.
| meta | description |
|---|---|
| 定义于 | 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 |
|---|---|
| 返回 | object |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:437 |
参数列表
exportingBoolean
_deserialize
Init this object from the custom serialized data.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:447 |
参数列表
dataObject the serialized json datactx_Deserializer