Cocos2dx中精灵(CCSprite)、动画创建
来源:互联网 发布:mac怎么改语言 编辑:程序博客网 时间:2024/05/22 01:35
- 1 .精灵的3种创建方法
/* _MySprite自定义的一个类 */bool _MySprite::init(){ //方法1.直接使用图片来创建精灵 CCSprite* sprite = CCSprite::create("CloseNormal.png"); addChild(sprite); sprite->setPosition(ccp(100, 100));------------------------------------------------- //方法2.通过纹理创建精灵 CCTexture2D* texture = CCTextureCache::sharedTextureCache() ->addImage("CloseNormal.png"); CCSprite* sprite = CCSprite::createWithTexture(texture); addChild(sprite); sprite->setPosition(ccp(100, 100));------------------------------------------------- //方法3.通过精灵帧(帧)创建精灵 CCTexture2D* texture = CCTextureCache::sharedTextureCache() ->addImage("CloseNormal.png"); CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(texture, CCRect(0, 0, 20, 20)); CCSprite* sprite = CCSprite::createWithSpriteFrame(frame); addChild(sprite); sprite->setPosition(ccp(100, 100)); return true;}
- 2 .动作(CCMoveBy)和动画( CCAnimate)
bool _MoveByAction::init(){ CCLayer::init(); //(1). moveby CCSprite* sprite = CCSprite::create("CloseNormal.png"); sprite->setPosition(ccp(100, 100)); addChild(sprite);#if 0 /* 创建动作 */ CCMoveBy* moveby = CCMoveBy::create(); sprite->runAction(moveby);#endif----------------------------------------------------- //(2). 创建动画 CCArray* arr = CCArray::create(); for (auto i = 0; i < var; ++i) { /* var变量为当前resource中有多少张资源图片 */ char fileName[256]; memset(fileName, 0x00, sizeof(fileName)); sprintf(fileName, "图片名称%d.png", i); CCSpriteFrame* frame = CCSpriteFrame::create(fileName, CCRect(0, 0, 80, 80)); /* CCRect(0,0,80,80)中的80*80是因为图片资源为80*80 */ arr->addObject(frame); } CCAnimation* animation = CCAnimation::createWithSpriteFrames(arr, 0.1f); //0.1f是time CCAnimate* animate = CCAnimate::create(animation); /* 让动画不停的运行 */ CCRepeatForever* repeat = CCRepeatForever::create(animate); sprite->runAction(repeat); return true;}
上面创建动画中,采用了数组来存放图片资源,假如为植物大战僵尸游戏中的植物(豌豆),共有8张(每秒钟8张:p_2_01~p_2_08);如下所示例:
,
但是在实际的开发中,不会这样存放图片资源(会浪费空间),特别是序列帧的特性中;应为下图所示存放资源(将一系列的图片放在一起):
细心的读者会有这样的疑惑,那怎么去从一张图片中读出每一个小的图片资源?(假若上面的豌豆图片的名称是:pea.png,那么会有对应的pea.plist文件,其描述了该图片资源的详细信息)。cocos2dx为我们提供了函数来解决这个问题,这样便使得问题简单化;如下代码:
bool _MoveByAction::init(){ CCLayer::init(); //创建一个精灵 CCSprite* sprite = CCSprite::create("CloseNormal.png"); sprite->setPosition(ccp(100, 100)); addChild(sprite); //PictureName.plist //加载合成中的帧到内存 CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("pea.plist"); //找出缓冲中的帧并且放到数组arr中 CCArray* arr = CCArray::create(); for (auto i = 0; i < 8; ++i) { /* var变量为当前resource中有多少张资源图片 */ char FrameName[256]; memset(FrameName, 0x00, sizeof(FrameName)); sprintf(FrameName, "图片名称%d.png", i); CCSpriteFrame* frame = cache->spriteFrameByName(FrameName); arr->addObject(frame); } //创建animation CCAnimation* animation = CCAnimation::createWithSpriteFrames(arr, 0.1f); //0.1f是time //通过animation来创建animate CCAnimate* animate = CCAnimate::create(animation); /* 通过animate创建无限循环的动画动作 */ CCRepeatForever* repeat = CCRepeatForever::create(animate); //让精灵来执行动画 sprite->runAction(repeat); return true;}
阅读全文
0 0
- Cocos2dx中精灵(CCSprite)、动画创建
- cocos2d-x 中创建 CCSprite 精灵动画
- cocos2dx中的精灵CCSprite
- CCSprite 精灵动画
- [windows+cocos2dx]CCSprite精灵类
- cocos2d-x for android:CCSprite 精灵动画(转)
- cocos2dx CCSprite 精灵的常用函数
- Cocos2dx创建精灵的5中方法
- cocos2d-x for android:CCSprite 精灵动画
- cocos2d-x for android:CCSprite 精灵动画
- cocos2d-x for android:CCSprite 精灵动画
- 小鬼学cocos2dX笔记01——创建精灵动画(一)
- 小鬼学cocos2dX笔记01——创建精灵动画(二)
- cocos2dx创建精灵动作
- Cocos2dx之精灵创建
- CCSprite:精灵
- 精灵(CCSprite)
- CCSprite精灵
- 03hadoop的MapReduce
- Spring security
- 04hadoop的Yarn
- java基础之IO流--ByteArrayInputStream(没有使用系统资源不会抛异常的IO流对象)
- VMware 12 安装及激活
- Cocos2dx中精灵(CCSprite)、动画创建
- 05hadoop的安装部署及伪分布式搭建
- java io概述
- 枚举类型的转换
- C++类的引入
- Android Intent 用法
- Redis系列-远程连接redis
- css3实现翘边阴影效果
- 统计某区间内的素数并打印显示