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

一、CCSprite的创建方法

1、通过纹理来创建

   
   
  1. CCSprite::spriteWithTexture(CCTexture2D *pTexture, const CCRect rect)

使用:

   
   
  1. local texture1 = CCTextureCache:sharedTextureCache():addImage("dog.png")
  2. local spriteTest = CCSprite:spriteWithTexture(texture1, CCRectMake(0, 0, frameWidth, frameHeight))

2 、通过路径创建

   
   
  1. CCSprite::spriteWithFile(const char *pszFileName,const CCRect rect)

使用:

   
   
  1. local spriteTest = CCSprite:spriteWithFile("dog.png")
  2. local spriteTest = CCSprite:spriteWithFile("dog.png",CCRectMake(0, 0, frameWidth, frameHeight))

   
   
  1.  

3 、通过加载帧缓存创建

   
   
  1. CCSprite::spriteWithSpriteFrame(CCSpriteFrame *pSpriteFrame)

使用:

   
   
  1. local texture1 = CCTextureCache:sharedTextureCache():addImage("dog.png")
  2. local frame1 = CCSpriteFrame:frameWithTexture(texture1, CCRectMake(0, 0, frameWidth, frameHeight))
  3. local spriteTest = CCSprite:spriteWithSpriteFrame(frame1)

4、使用CCSprite对象更换贴图

比如在贪食蛇的游戏中、蛇前进的方向发生变化、蛇头的图片就要随着改变。
首先需要把需要使用的图片用纹理打包软件生成一个贴图集、然后

   
   
  1. --载入贴图集
  2. cache = CCSpriteFrameCache:sharedSpriteFrameCache()
  3. cache:addSpriteFramesWithFile(".plist")
  4. --生成Sprite
  5. local spriteTest = CCSprite:spriteWithSpriteFrameName("1.png")
  6. --需要更换图片时
  7. local frame2 = cache:spriteFrameByName("2.png")
  8. spriteTest:setDisplayFrame(frame2)

二、sprite的一些基本的操作:透明度、缩放、旋转、混色

   
   
  1. spriteTest:setScaleX(-1)
  2. spriteTest:setScaleY(1)
  3. spriteTest:setScale(2)
  4. spriteTest:setPosition(50,70)
  5. spriteTest:setOpacity( 255) //0完全透明,255完全不透明
  6. spriteTest:setAnchorPoint(CCSize(0,0))
  7. spriteTest:setRotation( 90);
  8. spriteTest:setColor( ccc3( 255,0,0))
  9. spriteTest:setIsVisiblebVisible

三、CCSprite动画

   
   
  1. local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")
  2. local rect = CCRectMake(0, 0, frameWidth, frameHeight)
  3. local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect)
  4. rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)
  5. local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect)
  6. local animFrames = CCMutableArray_CCSpriteFrame__:new(2)
  7. animFrames:addObject(frame0)
  8. animFrames:addObject(frame1)
  9. local animation = CCAnimation:animationWithFrames(animFrames, 0.5)
  10. local animate = CCAnimate:actionWithAnimation(animation, false);
  11. --animFrames:delete()
  12. local spriteDog = CCSprite:spriteWithSpriteFrame(frame0)
  13. spriteDog:setPosition(0, winSize.height / 4 * 3)
  14. spriteDog:runAction(CCRepeatForever:actionWithAction(animate))

四、一些动画函数

   
   
  1. --停止所有动画
  2. sprite:stopAllActions()
  3. --播放一次
  4. sprite:runAction(CCRepeat:actionWithAction(_animate_go, 1))
  5. --重复播放
  6. sprite:runAction(CCRepeatForever:actionWithAction(_animate_go))
  7. --停止动画
  8. aimate:stop()
  9. --延时调用
  10. local action1 = CCMoveBy:actionWithDuration(1.0f, ccp(200, 200))
  11. local action2 = action1->reverse()
  12. sprite:runAction(CCSequence:actions(action1, CCDelayTime:actionWithDuration(1.0f), action2, NULL))
  13. --函数调用 CCCallFunc
  14. function Hello()
  15. CCLog("Hello, this your world!\n");
  16. end
  17. function Action()<
  18. sprite:stopAllActions()
  19. sfcache = CCSpriteFrameCache:sharedSpriteFrameCache()
  20. sfcache:addSpriteFramesWithFile(".plist");
  21. local animFrames = CCMutableArray_CCSpriteFrame__:new(2)
  22. for i=1, 3 do
  23. local frame = sfcache:spriteFrameByName(i..".png");
  24. animFrames:addObject(frame)
  25. end
  26. local animation = CCAnimation:animationWithFrames(animFrames, 0.5)
  27. local animate = CCAnimate:actionWithAnimation(animation, false)
  28. animFrames:release()
  29. local action = CCCallFunc:actionWithTarget((CCObject*)this, callfunc_selector(Hello))
  30. sprite:runAction(CCSequence:actionsWithAction(animate))
  31. end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值