oDustGgg个人原创、欢迎转载、转载请注明出处、http://blog.csdn.net/odustggg/article/details/8164693
一、CCSprite的创建方法
1、通过纹理来创建CCSprite::spriteWithTexture(CCTexture2D *pTexture,
const CCRect& rect)
使用:
local texture1 = CCTextureCache:sharedTextureCache():addImage("dog.png")
local spriteTest = CCSprite:spriteWithTexture(texture1, CCRectMake(0, 0, frameWidth, frameHeight))
2 、通过路径创建CCSprite::spriteWithFile(const char *pszFileName,
const CCRect& rect)
使用:
local spriteTest = CCSprite:spriteWithFile("dog.png")
local spriteTest = CCSprite:spriteWithFile("dog.png",CCRectMake(0, 0, frameWidth, frameHeight))
3 、通过加载帧缓存创建CCSprite::spriteWithSpriteFrame(CCSpriteFrame *pSpriteFrame)
使用:
local texture1 = CCTextureCache:sharedTextureCache():addImage("dog.png")
local frame1 = CCSpriteFrame:frameWithTexture(texture1, CCRectMake(0, 0, frameWidth, frameHeight))
local spriteTest = CCSprite:spriteWithSpriteFrame(frame1)
4、使用CCSprite对象更换贴图
比如在贪食蛇的游戏中、蛇前进的方向发生变化、蛇头的图片就要随着改变。
首先需要把需要使用的图片用纹理打包软件生成一个贴图集、然后
--载入贴图集
cache = CCSpriteFrameCache:sharedSpriteFrameCache()
cache:addSpriteFramesWithFile(".plist")
--生成Sprite
local spriteTest = CCSprite:spriteWithSpriteFrameName("1.png")
--需要更换图片时
local frame2 = cache:spriteFrameByName("2.png")
spriteTest:setDisplayFrame(frame2)
二、sprite的一些基本的操作:透明度、缩放、旋转、混色
spriteTest:setScaleX(-1)
spriteTest:setScaleY(1)
spriteTest:setScale(2)
spriteTest:setPosition(50,70)
spriteTest:setOpacity( 255) //0完全透明,255完全不透明
spriteTest:setAnchorPoint(CCSize(0,0))
spriteTest:setRotation( 90);
spriteTest:setColor( ccc3( 255,0,0))
spriteTest:setIsVisible(bVisible)
三、CCSprite动画
local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")
local rect = CCRectMake(0, 0, frameWidth, frameHeight)
local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect)
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)
local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect)
local animFrames = CCMutableArray_CCSpriteFrame__:new(2)
animFrames:addObject(frame0)
animFrames:addObject(frame1)
local animation = CCAnimation:animationWithFrames(animFrames, 0.5)
local animate = CCAnimate:actionWithAnimation(animation, false);
--animFrames:delete()
local spriteDog = CCSprite:spriteWithSpriteFrame(frame0)
spriteDog:setPosition(0, winSize.height / 4 * 3)
spriteDog:runAction(CCRepeatForever:actionWithAction(animate))
四、一些动画函数
--停止所有动画
sprite:stopAllActions()
--播放一次
sprite:runAction(CCRepeat:actionWithAction(_animate_go, 1))
--重复播放
sprite:runAction(CCRepeatForever:actionWithAction(_animate_go))
--停止动画
aimate:stop()
--延时调用
local action1 = CCMoveBy:actionWithDuration(1.0f, ccp(200, 200))
local action2 = action1->reverse()
sprite:runAction(CCSequence:actions(action1, CCDelayTime:actionWithDuration(1.0f), action2, NULL))
--函数调用 CCCallFunc
function Hello()
CCLog("Hello, this your world!\n");
end
function Action()
sprite:stopAllActions()
sfcache = CCSpriteFrameCache:sharedSpriteFrameCache()
sfcache:addSpriteFramesWithFile(".plist");
local animFrames = CCMutableArray_CCSpriteFrame__:new(2)
for i=1, 3 do
local frame = sfcache:spriteFrameByName(i..".png");
animFrames:addObject(frame)
end
local animation = CCAnimation:animationWithFrames(animFrames, 0.5)
local animate = CCAnimate:actionWithAnimation(animation, false)
animFrames:release()
local action = CCCallFunc:actionWithTarget((CCObject*)this, callfunc_selector(Hello))
sprite:runAction(CCSequence:actionsWithAction(animate))