cc.Sprite
Classcc.Sprite
- Defined in: CCSprite.js
- Extends cc.NodeRGBA
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
cc.Sprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) ) |
| Field Attributes | Field Name and Description |
|---|---|
|
Add child to sprite (override cc.Node )
|
|
|
Constructor
|
|
|
draw sprite to canvas
|
|
|
Initializes an empty sprite with nothing init.
|
|
|
Initializes a sprite with a texture and a rect in points, optionally rotated.
|
|
|
Returns whether or not a cc.SpriteFrame is being displayed
|
|
|
Sets the batch node to sprite
|
|
|
conforms to cc.TextureProtocol protocol
|
|
|
color setter
|
|
|
Sets a new display frame to the cc.Sprite.
|
|
|
opacity setter
|
|
|
opacity: conforms to CCRGBAProtocol protocol
|
|
|
Texture of sprite setter
|
|
|
updates the texture rect of the CCSprite in points.
|
|
|
updates the quad according the the rotation, position, scale values.
|
- Fields borrowed from class cc.Node:
- nodeToParentTransform, setNodeDirty, transform, visit
Method Summary
| Method Attributes | Method Name and Description |
|---|---|
| <static> |
cc.Sprite.create(fileName, rect)
Create a sprite with filename and rect
|
| <static> |
cc.Sprite.createWithSpriteFrame(spriteFrame)
Creates a sprite with a sprite frame. |
| <static> |
cc.Sprite.createWithSpriteFrameName(spriteFrameName)
Creates a sprite with a sprite frame. |
| <static> |
cc.Sprite.createWithTexture(texture, rect)
Creates a sprite with an exsiting texture contained in a CCTexture2D object |
|
Returns the current displayed frame.
|
|
|
Returns the index used on the TextureAtlas.
|
|
|
Returns the batch node object if this sprite is rendered by cc.SpriteBatchNode
|
|
|
conforms to cc.TextureProtocol protocol
|
|
|
Gets the offset position of the sprite.
|
|
|
getQuad()
Returns the quad (tex coords, vertex coords and color) information.
|
|
|
return the SpriteBatchNode of the cc.Sprite
|
|
|
Gets the weak reference of the cc.TextureAtlas when the sprite is rendered using via cc.SpriteBatchNode
|
|
|
returns the rect of the cc.Sprite in points
|
|
|
ignoreAnchorPointForPosition(relative)
IsRelativeAnchorPoint setter (override cc.Node )
|
|
|
initWithFile(filename, rect)
Initializes a sprite with an image filename. |
|
|
initWithSpriteFrame(spriteFrame)
Initializes a sprite with an SpriteFrame.
|
|
|
initWithSpriteFrameName(spriteFrameName)
Initializes a sprite with a sprite frame name.
|
|
|
isDirty()
Whether or not the Sprite needs to be updated in the Atlas
|
|
|
Returns the flag which indicates whether the sprite is flipped horizontally or not. |
|
|
Return the flag which indicates whether the sprite is flipped vertically or not. |
|
|
return IsOpacityModifyRGB value
|
|
|
Returns whether or not the texture rectangle is rotated.
|
|
|
removeAllChildren(cleanup)
Removes all children from the container (override cc.Node )
|
|
|
removeChild(child, cleanup)
Removes a child from the sprite.
|
|
|
reorderChild(child, zOrder)
Reorders a child according to a new z value.
|
|
|
HACK: optimization
|
|
|
setAnchorPoint(anchor)
AnchorPoint setter (override cc.Node )
|
|
|
setAtlasIndex(atlasIndex)
Set the index used on the TextureAtlas.
|
|
|
setDirty(bDirty)
Makes the Sprite to be updated in the Atlas.
|
|
|
setDirtyRecursively(value)
set Recursively is or isn't Dirty used only when parent is cc.SpriteBatchNode
|
|
|
setDisplayFrameWithAnimationName(animationName, frameIndex)
changes the display frame with animation name and index.
|
|
|
setFlippedX(flippedX)
Sets whether the sprite should be flipped horizontally or not.
|
|
|
setFlippedY(flippedY)
Sets whether the sprite should be flipped vertically or not.
|
|
|
setPosition(pos)
position setter (override cc.Node )
|
|
|
setRotation(rotation)
Rotation setter (override cc.Node )
|
|
|
setScale(scale, scaleY)
The scale factor of the node. |
|
|
setScaleX(scaleX)
ScaleX setter (override cc.Node )
|
|
|
setScaleY(scaleY)
ScaleY setter (override cc.Node )
|
|
|
setSkewX(sx)
SkewX setter (override cc.Node )
|
|
|
setSkewY(sy)
SkewY setter (override cc.Node )
|
|
|
setSpriteBatchNode(spriteBatchNode)
set the SpriteBatchNode of the cc.Sprite
|
|
|
setTextureAtlas(textureAtlas)
Sets the weak reference of the cc.TextureAtlas when the sprite is rendered using via cc.SpriteBatchNode
|
|
|
setVertexRect(rect)
set the vertex rect. |
|
|
setVertexZ(vertexZ)
VertexZ setter (override cc.Node )
|
|
|
setVisible(visible)
visible setter (override cc.Node )
|
|
|
Update sprite's color
|
|
|
useBatchNode(batchNode)
tell the sprite to use batch node render.
|
- Methods borrowed from cc.Node:
- addComponent
- cleanup
- convertToNodeSpace
- convertToNodeSpaceAR
- convertTouchToNodeSpace
- convertTouchToNodeSpaceAR
- convertToWorldSpace
- convertToWorldSpaceAR
- description
- getActionByTag
- getActionManager
- getAnchorPoint
- getAnchorPointInPoints
- getBoundingBox
- getBoundingBoxToWorld
- getCamera
- getChildByTag
- getChildren
- getChildrenCount
- getComponent
- getContentSize
- getGLServerState
- getGrid
- getOrderOfArrival
- getParent
- getPosition
- getPositionX
- getPositionY
- getRotation
- getRotationX
- getRotationY
- getScale
- getScaleX
- getScaleY
- getScheduler
- getShaderProgram
- getSkewX
- getSkewY
- getTag
- getUserData
- getUserObject
- getVertexZ
- getZOrder
- isIgnoreAnchorPointForPosition
- isRunning
- isVisible
- nodeToWorldTransform
- numberOfRunningActions
- onEnter
- onEnterTransitionDidFinish
- onExit
- onExitTransitionDidStart
- parentToNodeTransform
- pauseSchedulerAndActions
- removeAllChildrenWithCleanup
- removeAllComponents
- removeChildByTag
- removeComponent
- removeFromParent
- removeFromParentAndCleanup
- resumeSchedulerAndActions
- retain
- runAction
- schedule
- scheduleOnce
- scheduleUpdate
- scheduleUpdateWithPriority
- setActionManager
- setAdditionalTransform
- setContentSize
- setGLServerState
- setGrid
- setOrderOfArrival
- setParent
- setPositionX
- setPositionY
- setRotationX
- setRotationY
- setScheduler
- setShaderProgram
- setTag
- setUserData
- setUserObject
- setZOrder
- sortAllChildren
- stopAction
- stopActionByTag
- stopAllActions
- transformAncestors
- unschedule
- unscheduleAllCallbacks
- unscheduleUpdate
- update
- worldToNodeTransform
Class Detail
cc.Sprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) )
cc.Sprite can be created with an image, or with a sub-rectangle of an image.
If the parent or any of its ancestors is a cc.SpriteBatchNode then the following features/limitations are valid
- Features when the parent is a cc.BatchNode:
- MUCH faster rendering, specially if the cc.SpriteBatchNode has many children. All the children will be drawn in a single batch.
- Limitations
- Camera is not supported yet (eg: CCOrbitCamera action doesn't work)
- GridBase actions are not supported (eg: CCLens, CCRipple, CCTwirl)
- The Alias/Antialias property belongs to CCSpriteBatchNode, so you can't individually set the aliased property.
- The Blending function property belongs to CCSpriteBatchNode, so you can't individually set the blending function property.
- Parallax scroller is not supported, but can be simulated with a "proxy" sprite.
If the parent is an standard cc.Node, then cc.Sprite behaves like any other cc.Node:
- It supports blending functions
- It supports aliasing / antialiasing
- But the rendering will be slower: 1 draw per children.
The default anchorPoint in cc.Sprite is (0.5, 0.5).
var aSprite = new cc.Sprite();
aSprite.initWithFile("HelloHTML5World.png",cc.rect(0,0,480,320));
Field Detail
After initialization, the rect used will be the size of the texture, and the offset will be (0,0).
var img =cc.TextureCache.getInstance().addImage("HelloHTML5World.png");
var mySprite = new cc.Sprite();
mySprite.initWithTexture(img,cc.rect(0,0,480,320));
var batch = cc.SpriteBatchNode.create("Images/grossini_dance_atlas.png", 15);
var sprite = cc.Sprite.createWithTexture(batch.getTexture(), cc.rect(0, 0, 57, 57));
batch.addChild(sprite);
layer.addChild(batch);
Method Detail
- <static> {cc.Sprite} cc.Sprite.create(fileName, rect)Create a sprite with filename and rect
//create a sprite with filename
var sprite1 = cc.Sprite.create("HelloHTML5World.png"); //create a sprite with filename and rect
var sprite2 = cc.Sprite.create("HelloHTML5World.png",cc.rect(0,0,480,320));- Parameters:
- {String} fileName
- The string which indicates a path to image file, e.g., "scene1/monster.png".
- {cc.Rect} rect
- Only the contents inside rect of pszFileName's texture will be applied for this sprite.
- Returns:
- {cc.Sprite} A valid sprite object
- <static> {cc.Sprite} cc.Sprite.createWithSpriteFrame(spriteFrame)
Creates a sprite with a sprite frame.
A CCSpriteFrame will be fetched from the CCSpriteFrameCache by pszSpriteFrameName param.
If the CCSpriteFrame doesn't exist it will raise an exception.//get a sprite frame
var spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame("grossini_dance_01.png"); //create a sprite with a sprite frame
var sprite = cc.Sprite.createWithSpriteFrame(spriteFrame);- Parameters:
- {cc.SpriteFrame} spriteFrame
- A sprite frame which involves a texture and a rect
- Returns:
- {cc.Sprite} A valid sprite object
- <static> {cc.Sprite} cc.Sprite.createWithSpriteFrameName(spriteFrameName)
Creates a sprite with a sprite frame.
A CCSpriteFrame will be fetched from the CCSpriteFrameCache by pszSpriteFrameName param.
If the CCSpriteFrame doesn't exist it will raise an exception.//create a sprite with a sprite frame
var sprite = cc.Sprite.createWithSpriteFrameName('grossini_dance_01.png');- Parameters:
- {String} spriteFrameName
- A sprite frame which involves a texture and a rect
- Returns:
- {cc.Sprite} A valid sprite object
- <static> {cc.Sprite} cc.Sprite.createWithTexture(texture, rect)
Creates a sprite with an exsiting texture contained in a CCTexture2D object
After creation, the rect will be the size of the texture, and the offset will be (0,0).//get an image
var img = cc.TextureCache.getInstance().addImage("HelloHTML5World.png"); //create a sprite with texture
var sprite1 = cc.Sprite.createWithTexture(img); //create a sprite with texture and rect
var sprite2 = cc.Sprite.createWithTexture(img, cc.rect(0,0,480,320));- Parameters:
- {cc.Texture2D} texture
- A pointer to an existing CCTexture2D object. You can use a CCTexture2D object for many sprites.
- {cc.Rect} rect
- Only the contents inside the rect of this texture will be applied for this sprite.
- Returns:
- {cc.Sprite} A valid sprite object
- {cc.SpriteFrame} displayFrame()Returns the current displayed frame.
- Returns:
- {cc.SpriteFrame}
- {Number} getAtlasIndex()Returns the index used on the TextureAtlas.
- Returns:
- {Number}
- {cc.SpriteBatchNode|null} getBatchNode()Returns the batch node object if this sprite is rendered by cc.SpriteBatchNode
- Returns:
- {cc.SpriteBatchNode|null} The cc.SpriteBatchNode object if this sprite is rendered by cc.SpriteBatchNode, null if the sprite isn't used batch node.
- {cc.BlendFunc} getBlendFunc()conforms to cc.TextureProtocol protocol
- Returns:
- {cc.BlendFunc}
- {cc.Point} getOffsetPosition()Gets the offset position of the sprite. Calculated automatically by editors like Zwoptex.
- Returns:
- {cc.Point}
- {cc.V3F_C4B_T2F_Quad} getQuad()Returns the quad (tex coords, vertex coords and color) information.
- Returns:
- {cc.V3F_C4B_T2F_Quad}
- {cc.SpriteBatchNode} getSpriteBatchNode()return the SpriteBatchNode of the cc.Sprite
- Returns:
- {cc.SpriteBatchNode}
- {cc.TextureAtlas} getTextureAtlas()Gets the weak reference of the cc.TextureAtlas when the sprite is rendered using via cc.SpriteBatchNode
- Returns:
- {cc.TextureAtlas}
- {cc.Rect} getTextureRect()returns the rect of the cc.Sprite in points
- Returns:
- {cc.Rect}
- ignoreAnchorPointForPosition(relative)IsRelativeAnchorPoint setter (override cc.Node )
- Parameters:
- {Boolean} relative
- {Boolean} initWithFile(filename, rect)
Initializes a sprite with an image filename. This method will find pszFilename from local file system, load its content to CCTexture2D, then use CCTexture2D to create a sprite. After initialization, the rect used will be the size of the image. The offset will be (0,0).
var mySprite = new cc.Sprite();
mySprite.initWithFile("HelloHTML5World.png",cc.rect(0,0,480,320));- Parameters:
- {String} filename
- The path to an image file in local file system
- {cc.Rect} rect
- The rectangle assigned the content area from texture.
- Returns:
- {Boolean} true if the sprite is initialized properly, false otherwise.
- {Boolean} initWithSpriteFrame(spriteFrame)Initializes a sprite with an SpriteFrame. The texture and rect in SpriteFrame will be applied on this sprite
var spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame("grossini_dance_01.png");
var sprite = new cc.Sprite();
sprite.initWithSpriteFrame(spriteFrame);- Parameters:
- {cc.SpriteFrame} spriteFrame
- A CCSpriteFrame object. It should includes a valid texture and a rect
- Returns:
- {Boolean} true if the sprite is initialized properly, false otherwise.
- {Boolean} initWithSpriteFrameName(spriteFrameName)Initializes a sprite with a sprite frame name.
A cc.SpriteFrame will be fetched from the cc.SpriteFrameCache by name.
If the cc.SpriteFrame doesn't exist it will raise an exception.var sprite = new cc.Sprite();
sprite.initWithSpriteFrameName("grossini_dance_01.png");- Parameters:
- {String} spriteFrameName
- A key string that can fected a volid cc.SpriteFrame from cc.SpriteFrameCache
- Returns:
- {Boolean} true if the sprite is initialized properly, false otherwise.
- {Boolean} isDirty()Whether or not the Sprite needs to be updated in the Atlas
- Returns:
- {Boolean} true if the sprite needs to be updated in the Atlas, false otherwise.
- {Boolean} isFlippedX()
Returns the flag which indicates whether the sprite is flipped horizontally or not.
It only flips the texture of the sprite, and not the texture of the sprite's children.
Also, flipping the texture doesn't alter the anchorPoint.
If you want to flip the anchorPoint too, and/or to flip the children too use:
sprite->setScaleX(sprite->getScaleX() * -1);- Returns:
- {Boolean} true if the sprite is flipped horizaontally, false otherwise.
- {Boolean} isFlippedY()
Return the flag which indicates whether the sprite is flipped vertically or not.
It only flips the texture of the sprite, and not the texture of the sprite's children.
Also, flipping the texture doesn't alter the anchorPoint.
If you want to flip the anchorPoint too, and/or to flip the children too use:
sprite->setScaleY(sprite->getScaleY() * -1);- Returns:
- {Boolean} true if the sprite is flipped vertically, flase otherwise.
- {Boolean} isOpacityModifyRGB()return IsOpacityModifyRGB value
- Returns:
- {Boolean}
- {Boolean} isTextureRectRotated()Returns whether or not the texture rectangle is rotated.
- Returns:
- {Boolean}
- removeAllChildren(cleanup)Removes all children from the container (override cc.Node )
- Parameters:
- cleanup
- whether or not cleanup all running actions
- removeChild(child, cleanup)Removes a child from the sprite. (override cc.Node )
- Parameters:
- child
- cleanup
- whether or not cleanup all running actions
- reorderChild(child, zOrder)Reorders a child according to a new z value. (override cc.Node )
- Parameters:
- {cc.Node} child
- {Number} zOrder
- SET_DIRTY_RECURSIVELY()HACK: optimization
- setAnchorPoint(anchor)AnchorPoint setter (override cc.Node )
- Parameters:
- {cc.Point} anchor
- setAtlasIndex(atlasIndex)Set the index used on the TextureAtlas.
- Parameters:
- {Number} atlasIndex
- setDirty(bDirty)Makes the Sprite to be updated in the Atlas.
- Parameters:
- {Boolean} bDirty
- setDirtyRecursively(value)set Recursively is or isn't Dirty used only when parent is cc.SpriteBatchNode
- Parameters:
- {Boolean} value
- setDisplayFrameWithAnimationName(animationName, frameIndex)changes the display frame with animation name and index.
The animation name will be get from the CCAnimationCache- Parameters:
- animationName
- frameIndex
- setFlippedX(flippedX)Sets whether the sprite should be flipped horizontally or not.
- Parameters:
- {Boolean} flippedX
- true if the sprite should be flipped horizontally, false otherwise.
- setFlippedY(flippedY)Sets whether the sprite should be flipped vertically or not.
- Parameters:
- {Boolean} flippedY
- true if the sprite should be flipped vertically, false otherwise.
- setPosition(pos)position setter (override cc.Node )
- Parameters:
- {cc.Point} pos
- setRotation(rotation)Rotation setter (override cc.Node )
- Parameters:
- {Number} rotation
- setScale(scale, scaleY)
The scale factor of the node. 1.0 is the default scale factor.
It modifies the X and Y scale at the same time. (override cc.Node )- Parameters:
- {Number} scale
- {Number|null} scaleY Optional
- setScaleX(scaleX)ScaleX setter (override cc.Node )
- Parameters:
- {Number} scaleX
- setScaleY(scaleY)ScaleY setter (override cc.Node )
- Parameters:
- {Number} scaleY
- setSkewX(sx)SkewX setter (override cc.Node )
- Parameters:
- {Number} sx
- SkewX value
- setSkewY(sy)SkewY setter (override cc.Node )
- Parameters:
- {Number} sy
- SkewY value
- setSpriteBatchNode(spriteBatchNode)set the SpriteBatchNode of the cc.Sprite
- Parameters:
- {cc.SpriteBatchNode} spriteBatchNode
- setTextureAtlas(textureAtlas)Sets the weak reference of the cc.TextureAtlas when the sprite is rendered using via cc.SpriteBatchNode
- Parameters:
- {cc.TextureAtlas} textureAtlas
- setVertexRect(rect)
set the vertex rect.
It will be called internally by setTextureRect.
Useful if you want to create 2x images from SD images in Retina Display.
Do not call it manually. Use setTextureRect instead.
(override this method to generate "double scale" sprites)- Parameters:
- rect
- setVertexZ(vertexZ)VertexZ setter (override cc.Node )
- Parameters:
- {Number} vertexZ
- setVisible(visible)visible setter (override cc.Node )
- Parameters:
- {Boolean} visible
- updateColor()Update sprite's color
- useBatchNode(batchNode)tell the sprite to use batch node render.
- Parameters:
- {cc.SpriteBatchNode} batchNode
cc.Sprite的更多相关文章
- <9>cc.Sprite组件
1.精灵 精灵(Sprite)是Cocos系列的核心概念之一,是Cocos Creator最常用的显示图像的组件. 游戏中显示一个图片,我们就可以把这个叫做”精灵” sprite,这只是简单理解概念. ...
- cocos动画没有cc.Sprite.spriteFrame属性
对于新人来说总是有那么多的坑等着你. 新建动画节点的时候千万别[新建空节点]!!! 上面这个就是新建了空的节点,导致没有cc.Sprite.spriteFrame属性. 正确姿势: 粗略试了一下除了空 ...
- cc.Sprite组件
1.精灵 精灵(Sprite)是Cocos系列的核心概念之一,是Cocos Creator最常用的显示图像的组件. 游戏中显示一个图片,我们就可以把这个叫做"精灵" sprite, ...
- cocos2d-js Shader系列2:在cc.Sprite上使用Shader(黑白、灰度、造旧效果)
在Sprite中使用Shader做特殊的颜色处理比较简单,只需要把Shader程序绑定到Sprite上即可: sprite.shaderProgram = alphaTestShader; Cocos ...
- cc.Sprite 与 ccui.ImageView 改变图片
sprite.setTexture(fileName); imageView.loadTexture(fileName);
- [cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)
[转]http://blog.csdn.net/realcrazysun1/article/details/42393629 本文基于cocos2d-js 3.0版本引擎开发 RenderTextur ...
- cocos2d-html5 sprite打印宽高都为0的问题
版本是2.1.4,在程序里直接通过图片路径addChild了一个cc.Sprite,想要缩放时通是不起做用,于是通过打印发现其宽,高都为0,查来查去,发现:原来是图片没有注册到Resource.js里 ...
- cc.RepeatForever和cc.Spawn冲突
正确 var tmpShip3 = cc.Sprite.createWithSpriteFrameName("w1_1.png"); tmpShip3.setPosition(,) ...
- Cocos2d-js中使用纹理对象创建Sprite对象
本节我们会通过一个实例介绍纹理对象创建Sprite对象使用,这个实例如图5-2所示,其中地面上的草是放在背景(如下图所示)中的,场景中的两棵树是从后图所示的“树”纹理图片中截取出来的,图5-5所示是树 ...
随机推荐
- 加密解密(6)RSA算法过程图解
转自: http://blog.csdn.net/21aspnet/article/details/7249401 RSA算法 RSA公钥加密算法是1977年由Ron Rivest.Adi Shami ...
- Redis Cluster架构和设计机制简单介绍
之前另一篇文章也介绍了 Redis Cluster (link,在文章的后半部分) 今天看到这一篇,简单说一下(http://hot66hot.iteye.com/blog/2050676) 作者的目 ...
- 初步窥探Git
码农之路恒久远,学习向上是真谛啊!在学习的过程中,相信大家或多或少都接触到Git这个东东.它到底是什么呢,有什么作用呢,为什么它会那么火呢?带着这些一连串的疑问,决心去揭开它的庐山真面目. 在软件开发 ...
- Qt之模型/视图(自定义进度条)
简述 在之前的章节中分享过关于QHeaderView表头排序.添加复选框等内容,相信大家模型/视图.自定义风格有了一定的了解,下面我们来分享一个更常用的内容-自定义进度条. 实现方式: 从QAbstr ...
- Eclipse附加项目中的某个jar包的源码
1.这里以web项目为例,打开项目应用的jar包:如下图 2.在想要引入源码的jar包上右键>属性(Properties)
- div中文字水平和垂直居中的css代码
HTML元素 <div>水平垂直居中</div> css样式 div{ width:200px;height:200px; /*设置div的大小*/ border:1px so ...
- UVa 12100 (模拟) Printer Queue
用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. #include <iostream> #include < ...
- UVa 101 The Blocks Problem
题意:给出从左到右放置的n块木块(从0开始编号),再给出四种操作,再给出相应的操作,输出操作结束后每一堆木块的情况. 学习的紫书,因为每一堆的木块数是在发生变化的,所以用vector. 然后就是模拟几 ...
- unity3d 破解安装
1.下载破解程序,执行生成unity_v4.x.ulf文件 2.断网 3.执行unity客户端,load该lisence文件即可 注意:安装unity客户端完成后,未破解,切记别打开unity客户端
- acdream 1681 跳远女王(BFS,暴力)
Problem Description 娜娜觉得钢琴很无趣了,就抛弃了钢琴,继续往前走,前面是一片湖,娜娜想到湖的对岸,可惜娜娜找了好久都没找到小桥和小船,娜娜也发现自己不是神仙,不能像八仙过海一样. ...