mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
695 lines
26 KiB
JavaScript
695 lines
26 KiB
JavaScript
/****************************************************************************
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
Copyright (c) 2011-2012 cocos2d-x.org
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
var EventTarget = require("../event/event-target");
|
|
var Misc = require('../utils/misc');
|
|
|
|
/**
|
|
* <p>_ccsg.Sprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) ) <br/>
|
|
*
|
|
* _ccsg.Sprite can be created with an image, or with a sub-rectangle of an image. <br/>
|
|
*
|
|
* If the parent or any of its ancestors is a cc.SpriteBatchNode then the following features/limitations are valid <br/>
|
|
* - Features when the parent is a cc.BatchNode: <br/>
|
|
* - MUCH faster rendering, specially if the cc.SpriteBatchNode has many children. All the children will be drawn in a single batch. <br/>
|
|
*
|
|
* - Limitations <br/>
|
|
* - Camera is not supported yet (eg: CCOrbitCamera action doesn't work) <br/>
|
|
* - GridBase actions are not supported (eg: CCLens, CCRipple, CCTwirl) <br/>
|
|
* - The Alias/Antialias property belongs to CCSpriteBatchNode, so you can't individually set the aliased property. <br/>
|
|
* - The Blending function property belongs to CCSpriteBatchNode, so you can't individually set the blending function property. <br/>
|
|
* - Parallax scroller is not supported, but can be simulated with a "proxy" sprite. <br/>
|
|
*
|
|
* If the parent is an standard ccsg.Node, then _ccsg.Sprite behaves like any other ccsg.Node: <br/>
|
|
* - It supports blending functions <br/>
|
|
* - It supports aliasing / antialiasing <br/>
|
|
* - But the rendering will be slower: 1 draw per children. <br/>
|
|
*
|
|
* The default anchorPoint in _ccsg.Sprite is (0.5, 0.5). </p>
|
|
* @class
|
|
* @extends _ccsg.Node
|
|
*
|
|
* @param {cc.SpriteFrame|cc.Texture2D} textureOrSpriteFrame
|
|
* @param {Rect} rect Only the contents inside rect of pszFileName's texture will be applied for this sprite.
|
|
* @param {Boolean} [rotated] Whether or not the texture rectangle is rotated.
|
|
* @example
|
|
*
|
|
* 1.Create a sprite with a sprite frame
|
|
* var sprite = new _ccsg.Sprite(spriteFrame);
|
|
*
|
|
* 2.Create a sprite with an existing texture contained in a CCTexture2D object
|
|
* After creation, the rect will be the size of the texture, and the offset will be (0,0).
|
|
* var texture = cc.textureCache.addImage("HelloHTML5World.png");
|
|
* var sprite1 = new _ccsg.Sprite(texture);
|
|
* var sprite2 = new _ccsg.Sprite(texture, cc.rect(0,0,480,320));
|
|
*
|
|
* @property {Boolean} dirty - Indicates whether the sprite needs to be updated.
|
|
* @property {Boolean} flippedX - Indicates whether or not the sprite is flipped on x axis.
|
|
* @property {Boolean} flippedY - Indicates whether or not the sprite is flipped on y axis.
|
|
* @property {Number} offsetX - <@readonly> The offset position on x axis of the sprite in texture. Calculated automatically by editors like Zwoptex.
|
|
* @property {Number} offsetY - <@readonly> The offset position on x axis of the sprite in texture. Calculated automatically by editors like Zwoptex.
|
|
* @property {Texture2D} texture - Texture used to render the sprite.
|
|
* @property {Boolean} textureRectRotated - <@readonly> Indicate whether the texture rectangle is rotated.
|
|
* @property {cc.SpriteBatchNode} batchNode - The batch node object if this sprite is rendered by cc.SpriteBatchNode.
|
|
* @property {cc.V3F_C4B_T2F_Quad} quad - <@readonly> The quad (tex coords, vertex coords and color) information.
|
|
*/
|
|
_ccsg.Sprite = _ccsg.Node.extend({
|
|
dirty:false,
|
|
|
|
_recursiveDirty:null, //Whether all of the sprite's children needs to be updated
|
|
_shouldBeHidden:false, //should not be drawn because one of the ancestors is not visible
|
|
_transformToBatch:null,
|
|
|
|
//
|
|
// Data used when the sprite is self-rendered
|
|
//
|
|
_blendFunc:null, //It's required for CCTextureProtocol inheritance
|
|
_texture:null, //cc.Texture2D object that is used to render the sprite
|
|
|
|
//
|
|
// Shared data
|
|
//
|
|
// texture
|
|
_rect:null, //Rectangle of cc.Texture2D
|
|
_rectRotated:false, //Whether the texture is rotated
|
|
|
|
// Offset Position (used by Zwoptex)
|
|
_offsetPosition:null, // absolute
|
|
_unflippedOffsetPositionFromCenter:null,
|
|
|
|
_opacityModifyRGB:false,
|
|
|
|
// image is flipped
|
|
_flippedX:false, //Whether the sprite is flipped horizontally or not.
|
|
_flippedY:false, //Whether the sprite is flipped vertically or not.
|
|
|
|
_textureLoaded:false,
|
|
_className:"Sprite",
|
|
|
|
ctor: function (textureOrSpriteFrame, rect, rotated) {
|
|
var self = this;
|
|
_ccsg.Node.prototype.ctor.call(self);
|
|
EventTarget.call(self);
|
|
|
|
self._shouldBeHidden = false;
|
|
self._offsetPosition = cc.p(0, 0);
|
|
self._unflippedOffsetPositionFromCenter = cc.p(0, 0);
|
|
self._blendFunc = {src: cc.macro.BLEND_SRC, dst: cc.macro.BLEND_DST};
|
|
self._rect = cc.rect(0, 0, 0, 0);
|
|
|
|
self._softInit(textureOrSpriteFrame, rect, rotated);
|
|
},
|
|
|
|
/**
|
|
* Returns whether the texture have been loaded
|
|
* @returns {boolean}
|
|
*/
|
|
textureLoaded:function(){
|
|
return this._textureLoaded;
|
|
},
|
|
|
|
/**
|
|
* Add a event listener for texture loaded event.
|
|
* @param {Function} callback
|
|
* @param {Object} target
|
|
* @deprecated since 3.1, please use EventTarget instead
|
|
*/
|
|
addLoadedEventListener:function(callback, target){
|
|
this.once("load", callback, target);
|
|
},
|
|
|
|
/**
|
|
* Returns whether or not the Sprite needs to be updated in the Atlas
|
|
* @return {Boolean} True if the sprite needs to be updated in the Atlas, false otherwise.
|
|
*/
|
|
isDirty:function () {
|
|
return this.dirty;
|
|
},
|
|
|
|
/**
|
|
* Makes the sprite to be updated in the Atlas.
|
|
* @param {Boolean} bDirty
|
|
*/
|
|
setDirty:function (bDirty) {
|
|
this.dirty = bDirty;
|
|
},
|
|
|
|
/**
|
|
* Returns whether or not the texture rectangle is rotated.
|
|
* @return {Boolean}
|
|
*/
|
|
isTextureRectRotated:function () {
|
|
return this._rectRotated;
|
|
},
|
|
|
|
/**
|
|
* Returns the rect of the _ccsg.Sprite in points
|
|
* @return {Rect}
|
|
*/
|
|
getTextureRect:function () {
|
|
return cc.rect(this._rect);
|
|
},
|
|
|
|
/**
|
|
* Returns the offset position of the sprite. Calculated automatically by editors like Zwoptex.
|
|
* @return {cc.Vec2}
|
|
*/
|
|
getOffsetPosition:function () {
|
|
return cc.p(this._offsetPosition);
|
|
},
|
|
|
|
_getOffsetX: function () {
|
|
return this._offsetPosition.x;
|
|
},
|
|
_getOffsetY: function () {
|
|
return this._offsetPosition.y;
|
|
},
|
|
|
|
/**
|
|
* Returns the blend function
|
|
* @return {cc.BlendFunc}
|
|
*/
|
|
getBlendFunc:function () {
|
|
return this._blendFunc;
|
|
},
|
|
|
|
/**
|
|
* Initializes a sprite with a SpriteFrame. The texture and rect in SpriteFrame will be applied on this sprite.<br/>
|
|
* Please pass parameters to the constructor to initialize the sprite, do not call this function yourself,
|
|
* @param {cc.SpriteFrame} spriteFrame A CCSpriteFrame object. It should includes a valid texture and a rect
|
|
* @return {Boolean} true if the sprite is initialized properly, false otherwise.
|
|
*/
|
|
initWithSpriteFrame:function (spriteFrame) {
|
|
cc.assertID(spriteFrame, 2606);
|
|
|
|
if(!spriteFrame.textureLoaded()){
|
|
//add event listener
|
|
this._textureLoaded = false;
|
|
spriteFrame.once("load", this._renderCmd._spriteFrameLoadedCallback, this._renderCmd);
|
|
}
|
|
|
|
//TODO
|
|
var rotated = cc._renderType === cc.game.RENDER_TYPE_CANVAS ? false : spriteFrame._rotated;
|
|
var ret = this.initWithTexture(spriteFrame.getTexture(), spriteFrame.getRect(), rotated);
|
|
this.setSpriteFrame(spriteFrame);
|
|
|
|
return ret;
|
|
},
|
|
|
|
/**
|
|
* Initializes a sprite with a sprite frame name. <br/>
|
|
* A cc.SpriteFrame will be fetched from the cc.SpriteFrameCache by name. <br/>
|
|
* If the cc.SpriteFrame doesn't exist it will raise an exception. <br/>
|
|
* Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.
|
|
* @param {String} spriteFrameName A key string that can fected a valid cc.SpriteFrame from cc.SpriteFrameCache
|
|
* @return {Boolean} true if the sprite is initialized properly, false otherwise.
|
|
* @deprecated
|
|
*/
|
|
initWithSpriteFrameName:function () {
|
|
cc.warnID(2608);
|
|
return;
|
|
},
|
|
|
|
/**
|
|
* <p>
|
|
* set the vertex rect.<br/>
|
|
* It will be called internally by setTextureRect. <br/>
|
|
* Useful if you want to create 2x images from SD images in Retina Display. <br/>
|
|
* Do not call it manually. Use setTextureRect instead. <br/>
|
|
* (override this method to generate "double scale" sprites)
|
|
* </p>
|
|
* @param {Rect} rect
|
|
*/
|
|
setVertexRect:function (rect) {
|
|
var locRect = this._rect;
|
|
locRect.x = rect.x;
|
|
locRect.y = rect.y;
|
|
locRect.width = rect.width;
|
|
locRect.height = rect.height;
|
|
},
|
|
|
|
//
|
|
// _ccsg.Node property overloads
|
|
//
|
|
|
|
/**
|
|
* Sets whether the sprite should be flipped horizontally or not.
|
|
* @param {Boolean} flippedX true if the sprite should be flipped horizontally, false otherwise.
|
|
*/
|
|
setFlippedX:function (flippedX) {
|
|
if (this._flippedX !== flippedX) {
|
|
this._flippedX = flippedX;
|
|
this.setTextureRect(this._rect, this._rectRotated, this._contentSize);
|
|
this.setNodeDirty(true);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Sets whether the sprite should be flipped vertically or not.
|
|
* @param {Boolean} flippedY true if the sprite should be flipped vertically, false otherwise.
|
|
*/
|
|
setFlippedY:function (flippedY) {
|
|
if (this._flippedY !== flippedY) {
|
|
this._flippedY = flippedY;
|
|
this.setTextureRect(this._rect, this._rectRotated, this._contentSize);
|
|
this.setNodeDirty(true);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* <p>
|
|
* Returns the flag which indicates whether the sprite is flipped horizontally or not. <br/>
|
|
* <br/>
|
|
* It only flips the texture of the sprite, and not the texture of the sprite's children. <br/>
|
|
* Also, flipping the texture doesn't alter the anchorPoint. <br/>
|
|
* If you want to flip the anchorPoint too, and/or to flip the children too use: <br/>
|
|
* sprite.setScaleX(sprite.getScaleX() * -1); <p/>
|
|
* @return {Boolean} true if the sprite is flipped horizontally, false otherwise.
|
|
*/
|
|
isFlippedX:function () {
|
|
return this._flippedX;
|
|
},
|
|
|
|
/**
|
|
* <p>
|
|
* Return the flag which indicates whether the sprite is flipped vertically or not. <br/>
|
|
* <br/>
|
|
* It only flips the texture of the sprite, and not the texture of the sprite's children. <br/>
|
|
* Also, flipping the texture doesn't alter the anchorPoint. <br/>
|
|
* If you want to flip the anchorPoint too, and/or to flip the children too use: <br/>
|
|
* sprite.setScaleY(sprite.getScaleY() * -1); <p/>
|
|
* @return {Boolean} true if the sprite is flipped vertically, false otherwise.
|
|
*/
|
|
isFlippedY:function () {
|
|
return this._flippedY;
|
|
},
|
|
|
|
//
|
|
// RGBA protocol
|
|
//
|
|
/**
|
|
* Sets whether opacity modify color or not.
|
|
* @function
|
|
* @param {Boolean} modify
|
|
*/
|
|
setOpacityModifyRGB: function (modify) {
|
|
if (this._opacityModifyRGB !== modify) {
|
|
this._opacityModifyRGB = modify;
|
|
this._renderCmd._setColorDirty();
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Returns whether opacity modify color or not.
|
|
* @return {Boolean}
|
|
*/
|
|
isOpacityModifyRGB:function () {
|
|
return this._opacityModifyRGB;
|
|
},
|
|
|
|
// Animation
|
|
|
|
/**
|
|
* Changes the display frame with animation name and index.<br/>
|
|
* The animation name will be get from the CCAnimationCache
|
|
* @param {String} animationName
|
|
* @param {Number} frameIndex
|
|
*/
|
|
setDisplayFrameWithAnimationName:function (animationName, frameIndex) {
|
|
cc.assertID(animationName, 2610);
|
|
|
|
var cache = cc.spriteFrameAnimationCache.getAnimation(animationName);
|
|
if(!cache){
|
|
cc.logID(2602);
|
|
return;
|
|
}
|
|
var animFrame = cache.getFrames()[frameIndex];
|
|
if(!animFrame){
|
|
cc.logID(2603);
|
|
return;
|
|
}
|
|
this.setSpriteFrame(animFrame.getSpriteFrame());
|
|
},
|
|
|
|
// CCTextureProtocol
|
|
/**
|
|
* Returns the texture of the sprite node
|
|
* @returns {Texture2D}
|
|
*/
|
|
getTexture:function () {
|
|
return this._texture;
|
|
},
|
|
|
|
_softInit: function (textureOrSpriteFrame, rect, rotated) {
|
|
if (textureOrSpriteFrame === undefined)
|
|
_ccsg.Sprite.prototype.init.call(this);
|
|
else if (textureOrSpriteFrame instanceof cc.Texture2D) {
|
|
// Init with texture and rect
|
|
this.initWithTexture(textureOrSpriteFrame, rect, rotated);
|
|
} else if (textureOrSpriteFrame instanceof cc.SpriteFrame) {
|
|
// Init with a sprite frame
|
|
this.initWithSpriteFrame(textureOrSpriteFrame);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* conforms to cc.TextureProtocol protocol
|
|
* @function
|
|
* @param {Number|cc.BlendFunc} src
|
|
* @param {Number} dst
|
|
*/
|
|
setBlendFunc: function (src, dst) {
|
|
var locBlendFunc = this._blendFunc;
|
|
if (dst === undefined) {
|
|
locBlendFunc.src = src.src;
|
|
locBlendFunc.dst = src.dst;
|
|
} else {
|
|
locBlendFunc.src = src;
|
|
locBlendFunc.dst = dst;
|
|
}
|
|
this._renderCmd.updateBlendFunc(locBlendFunc);
|
|
},
|
|
|
|
/**
|
|
* Initializes an empty sprite with nothing init.<br/>
|
|
* Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.
|
|
* @function
|
|
* @return {Boolean}
|
|
*/
|
|
init: function () {
|
|
var _t = this;
|
|
if (arguments.length > 0)
|
|
return _t.initWithFile(arguments[0], arguments[1]);
|
|
|
|
_t.dirty = _t._recursiveDirty = false;
|
|
|
|
_t._blendFunc.src = cc.macro.BLEND_SRC;
|
|
_t._blendFunc.dst = cc.macro.BLEND_DST;
|
|
|
|
_t.texture = null;
|
|
_t._flippedX = _t._flippedY = false;
|
|
|
|
// default transform anchor: center
|
|
_t.anchorX = 0.5;
|
|
_t.anchorY = 0.5;
|
|
|
|
// zwoptex default values
|
|
_t._offsetPosition.x = 0;
|
|
_t._offsetPosition.y = 0;
|
|
|
|
// updated in "useSelfRender"
|
|
// Atlas: TexCoords
|
|
_t.setTextureRect(cc.rect(0, 0, 0, 0), false, cc.size(0, 0));
|
|
return true;
|
|
},
|
|
|
|
/**
|
|
* <p>
|
|
* Initializes a sprite with an image filename.<br/>
|
|
*
|
|
* This method will find pszFilename from local file system, load its content to CCTexture2D,<br/>
|
|
* then use CCTexture2D to create a sprite.<br/>
|
|
* After initialization, the rect used will be the size of the image. The offset will be (0,0).<br/>
|
|
* Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.
|
|
* </p>
|
|
* @param {String} filename The path to an image file in local file system
|
|
* @param {Rect} rect The rectangle assigned the content area from texture.
|
|
* @return {Boolean} true if the sprite is initialized properly, false otherwise.
|
|
*/
|
|
initWithFile:function (filename, rect) {
|
|
cc.assertID(filename, 2609);
|
|
|
|
var tex = cc.textureCache.getTextureForKey(filename);
|
|
if (!tex) {
|
|
tex = cc.textureCache.addImage(filename);
|
|
return this.initWithTexture(tex, rect || cc.rect(0, 0, tex.width, tex.height));
|
|
} else {
|
|
if (!rect) {
|
|
var size = tex.getContentSize();
|
|
rect = cc.rect(0, 0, size.width, size.height);
|
|
}
|
|
return this.initWithTexture(tex, rect);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Initializes a sprite with a texture and a rect in points, optionally rotated. <br/>
|
|
* After initialization, the rect used will be the size of the texture, and the offset will be (0,0).<br/>
|
|
* Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.
|
|
* @function
|
|
* @param {cc.Texture2D|HTMLImageElement|HTMLCanvasElement} texture A pointer to an existing CCTexture2D object. You can use a CCTexture2D object for many sprites.
|
|
* @param {Rect} [rect] Only the contents inside rect of this texture will be applied for this sprite.
|
|
* @param {Boolean} [rotated] Whether or not the texture rectangle is rotated.
|
|
* @param {Boolean} [counterclockwise=true] Whether or not the texture rectangle rotation is counterclockwise (texture package is counterclockwise, spine is clockwise).
|
|
* @return {Boolean} true if the sprite is initialized properly, false otherwise.
|
|
*/
|
|
initWithTexture: function (texture, rect, rotated, counterclockwise) {
|
|
var _t = this;
|
|
cc.assertID(arguments.length !== 0, 2710);
|
|
|
|
rotated = rotated || false;
|
|
texture = this._renderCmd._handleTextureForRotatedTexture(texture, rect, rotated, counterclockwise);
|
|
|
|
_t._recursiveDirty = false;
|
|
_t.dirty = false;
|
|
_t._opacityModifyRGB = true;
|
|
|
|
_t._blendFunc.src = cc.macro.BLEND_SRC;
|
|
_t._blendFunc.dst = cc.macro.BLEND_DST;
|
|
|
|
_t._flippedX = _t._flippedY = false;
|
|
|
|
// default transform anchor: center
|
|
_t.setAnchorPoint(0.5, 0.5);
|
|
|
|
// zwoptex default values
|
|
_t._offsetPosition.x = 0;
|
|
_t._offsetPosition.y = 0;
|
|
|
|
var locTextureLoaded = texture.loaded;
|
|
_t._textureLoaded = locTextureLoaded;
|
|
|
|
if (!locTextureLoaded) {
|
|
_t._rectRotated = rotated;
|
|
if (rect) {
|
|
_t._rect.x = rect.x;
|
|
_t._rect.y = rect.y;
|
|
_t._rect.width = rect.width;
|
|
_t._rect.height = rect.height;
|
|
}
|
|
if(_t.texture)
|
|
_t.texture.off("load", _t._renderCmd._textureLoadedCallback, _t._renderCmd);
|
|
texture.once("load", _t._renderCmd._textureLoadedCallback, _t._renderCmd);
|
|
_t.setTexture(texture);
|
|
return true;
|
|
}
|
|
|
|
if (!rect)
|
|
rect = cc.rect(0, 0, texture.width, texture.height);
|
|
|
|
this._renderCmd._checkTextureBoundary(texture, rect, rotated);
|
|
|
|
_t.setTexture(texture);
|
|
_t.setTextureRect(rect, rotated);
|
|
this.emit("load");
|
|
return true;
|
|
},
|
|
|
|
/**
|
|
* Updates the texture rect of the CCSprite in points.
|
|
* @function
|
|
* @param {Rect} rect a rect of texture
|
|
* @param {Boolean} [rotated] Whether or not the texture is rotated
|
|
* @param {Size} [untrimmedSize] The original pixels size of the texture
|
|
* @param {Boolean} [needConvert] contentScaleFactor switch
|
|
*/
|
|
setTextureRect: function (rect, rotated, untrimmedSize, needConvert) {
|
|
var _t = this;
|
|
_t._rectRotated = rotated || false;
|
|
_t.setContentSize(untrimmedSize || rect);
|
|
|
|
_t.setVertexRect(rect);
|
|
_t._renderCmd._setTextureCoords(rect, needConvert);
|
|
|
|
var relativeOffsetX = _t._unflippedOffsetPositionFromCenter.x, relativeOffsetY = _t._unflippedOffsetPositionFromCenter.y;
|
|
if (_t._flippedX)
|
|
relativeOffsetX = -relativeOffsetX;
|
|
if (_t._flippedY)
|
|
relativeOffsetY = -relativeOffsetY;
|
|
var locRect = _t._rect;
|
|
_t._offsetPosition.x = relativeOffsetX + (_t._contentSize.width - locRect.width) / 2;
|
|
_t._offsetPosition.y = relativeOffsetY + (_t._contentSize.height - locRect.height) / 2;
|
|
},
|
|
|
|
// Frames
|
|
/**
|
|
* Sets a new sprite frame to the sprite.
|
|
* @function
|
|
* @param {cc.SpriteFrame} newFrame
|
|
*/
|
|
setSpriteFrame: function (newFrame) {
|
|
var _t = this;
|
|
cc.assertID(newFrame, 2712);
|
|
|
|
this.setNodeDirty(true);
|
|
|
|
var frameOffset = newFrame.getOffset();
|
|
_t._unflippedOffsetPositionFromCenter.x = frameOffset.x;
|
|
_t._unflippedOffsetPositionFromCenter.y = frameOffset.y;
|
|
|
|
// update rect
|
|
var pNewTexture = newFrame.getTexture();
|
|
var locTextureLoaded = newFrame.textureLoaded();
|
|
if (!locTextureLoaded) {
|
|
_t._textureLoaded = false;
|
|
newFrame.once("load", function (event) {
|
|
var sender = event.currentTarget;
|
|
_t._textureLoaded = true;
|
|
var locNewTexture = sender.getTexture();
|
|
if (locNewTexture !== _t._texture)
|
|
_t._setTexture(locNewTexture);
|
|
_t.setTextureRect(sender.getRect(), sender.isRotated(), sender.getOriginalSize());
|
|
_t.emit("load");
|
|
_t.setColor(_t._realColor);
|
|
}, _t);
|
|
} else {
|
|
_t._textureLoaded = true;
|
|
// update texture before updating texture rect
|
|
if (pNewTexture !== _t._texture) {
|
|
_t._setTexture(pNewTexture);
|
|
_t.setColor(_t._realColor);
|
|
}
|
|
_t.setTextureRect(newFrame.getRect(), newFrame.isRotated(), newFrame.getOriginalSize());
|
|
}
|
|
this._renderCmd._updateForSetSpriteFrame(pNewTexture);
|
|
},
|
|
|
|
/**
|
|
* Sets a new display frame to the sprite.
|
|
* @param {cc.SpriteFrame|String} newFrame
|
|
* @deprecated
|
|
*/
|
|
setDisplayFrame: function(newFrame){
|
|
cc.logID(2604);
|
|
this.setSpriteFrame(newFrame);
|
|
},
|
|
|
|
/**
|
|
* Returns whether or not a cc.SpriteFrame is being displayed
|
|
* @function
|
|
* @param {cc.SpriteFrame} frame
|
|
* @return {Boolean}
|
|
*/
|
|
isFrameDisplayed: function(frame){
|
|
return this._renderCmd.isFrameDisplayed(frame);
|
|
},
|
|
|
|
/**
|
|
* Returns the current displayed frame.
|
|
* @deprecated since 3.4, please use getSpriteFrame instead
|
|
* @return {cc.SpriteFrame}
|
|
*/
|
|
displayFrame: function () {
|
|
return this.getSpriteFrame();
|
|
},
|
|
|
|
/**
|
|
* Returns the current displayed frame.
|
|
* @return {cc.SpriteFrame}
|
|
*/
|
|
getSpriteFrame: function () {
|
|
return new cc.SpriteFrame(this._texture,
|
|
this._rect,
|
|
this._rectRotated,
|
|
this._unflippedOffsetPositionFromCenter,
|
|
this._contentSize);
|
|
},
|
|
|
|
// CCTextureProtocol
|
|
/**
|
|
* Sets the texture of sprite
|
|
* @function
|
|
* @param {cc.Texture2D|String} texture
|
|
*/
|
|
setTexture: function (texture) {
|
|
if(!texture)
|
|
return this._renderCmd._setTexture(null);
|
|
|
|
//CCSprite.cpp 327 and 338
|
|
var isFileName = cc.js.isString(texture);
|
|
|
|
if(isFileName)
|
|
texture = cc.textureCache.addImage(texture);
|
|
|
|
if(texture.loaded){
|
|
this._setTexture(texture, isFileName);
|
|
this.setColor(this._realColor);
|
|
this._textureLoaded = true;
|
|
this.emit("load");
|
|
}else{
|
|
this._renderCmd._setTexture(texture);
|
|
texture.once("load", function (event) {
|
|
this._setTexture(texture, isFileName);
|
|
this.setColor(this._realColor);
|
|
this._textureLoaded = true;
|
|
this.emit("load");
|
|
}, this);
|
|
}
|
|
},
|
|
|
|
_setTexture: function(texture, change){
|
|
this._renderCmd._setTexture(texture);
|
|
if(change)
|
|
this._changeRectWithTexture(texture);
|
|
},
|
|
|
|
_changeRectWithTexture: function(texture) {
|
|
var rect = cc.rect(0, 0, texture.width, texture.height);
|
|
this.setTextureRect(rect);
|
|
},
|
|
|
|
_createRenderCmd: function(){
|
|
if(cc._renderType === cc.game.RENDER_TYPE_CANVAS)
|
|
return new _ccsg.Sprite.CanvasRenderCmd(this);
|
|
else
|
|
return new _ccsg.Sprite.WebGLRenderCmd(this);
|
|
}
|
|
});
|
|
|
|
cc.js.addon(_ccsg.Sprite.prototype, EventTarget.prototype);
|
|
|
|
var SameNameGetSets = ['opacity', 'color', 'texture'];
|
|
var DiffNameGetSets = {
|
|
opacityModifyRGB: ['isOpacityModifyRGB', 'setOpacityModifyRGB'],
|
|
flippedX: ['isFlippedX', 'setFlippedX'],
|
|
flippedY: ['isFlippedY', 'setFlippedY'],
|
|
offsetX: ['_getOffsetX'],
|
|
offsetY: ['_getOffsetY'],
|
|
textureRectRotated: ['isTextureRectRotated'],
|
|
};
|
|
Misc.propertyDefine(_ccsg.Sprite, SameNameGetSets, DiffNameGetSets);
|