cocos2d_x+lua【1】 ------sprite的使用

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))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值