cocos2dx 的简单动画创建

来源:互联网 发布:数据颗粒度是什么意思 编辑:程序博客网 时间:2024/04/29 05:45



        //缓冲 帧和纹理
// 首先,调用CCSpriteFrameCache的addSpriteFramesWithFile方法,然后把tp生成的plist文件当作参数传进去。
// 这个方法做了以下几件事:
// 寻找工程目录下面和输入的参数名字一样,但是后缀是.png的图片文件。然后把这个文件加入到共享的CCTextureCache中。
// 解析plist文件,追踪所有的sprite在spritesheet中的位置,内部使用CCSpriteFrame对象来追踪这些信息。

CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();

//为cache 字典中添加资源,字典 是一个单例.

//这个地方 只传入.plist  就可以,果然cache 只是存储帧信息, batchnode  用于存储图片(目前的我只理解到这个程度)
cache->addSpriteFramesWithFile("AnimBear.plist");
// 同时  传出 AnimBear.png也可以
//cache->addSpriteFramesWithFile("AnimBear.plist", "AnimBear.png");


//创建一个精灵批处理结点
//把spritesheet当作参数传进去。spritesheet在cocos2d中的工作原理如下:
//你创建一个CCSpriteBatchNode对象,它可以包含很多sprite .然后 把spriteBatch 加入到 layer中.
//接下来,你从spriteBatch中创建的任何sprite,你应该把它当作CCSpriteBatchNode的一个孩子加进去。


//只要sprite包含在spritesheet中,那么就没问题,否则会出错。(sprite 不可以直接加载到 layer中了)
//CCSpriteBatchNode可以智能地遍历它的所有的孩子结点,并通过一次OpenGL ES call来渲染这些孩子,
//而不是以前每个sprite都需要一个OpenGL call,这样渲染速度就会更快。


//注意:CCSpriteBatchNode以前叫做CCSpriteSheet,你可能会在一起比较老的代码里面看见它。
        CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("AnimBear.png");
//batch node  负责管理 text true 这个纹理.  
         CCSpriteBatchNode *spriteBatch = CCSpriteBatchNode::batchNodeWithTexture(texture);

        addChild(spriteBatch);


        // 创建一个指针数组, 他是指向  spriteFrame的

        CCMutableArray<CCSpriteFrame*> *framesArray = new CCMutableArray<CCSpriteFrame*>(8);
//然后使用共享的CCSpriteFrameCache来获得每一个动画帧。
//记住,它们已经在缓存里了,因为我们前面调用了addSpriteFramesWithFile方法。

//把动画所需的所有图片帧添加到数组里
CCSpriteFrame* frame = cache->spriteFrameByName("bear1.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear2.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear3.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear4.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear5.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear6.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear7.png");
framesArray->addObject(frame);
frame = cache->spriteFrameByName("bear8.png");
framesArray->addObject(frame);


//创建动画(动画帧数组, 每一帧的延时:秒)
        CCAnimation *animation = CCAnimation::animationWithFrames(framesArray, 0.1f);
        
//使 动画循环
CCAction *ActionAnimation = CCAnimate::actionWithAnimation(animation, false);
CCAction *ActionForover = CCRepeatForever::actionWithAction((CCSequence*)ActionAnimation);


        // Create a sprite for our bear
//创建 精灵
CCSprite * sprite = CCSprite::spriteWithSpriteFrameName("bear1.png");
        CCSize size = CCDirector::sharedDirector()->getWinSize();
sprite->setPosition(ccp(size.width/4,size.height/2));


//让熊精灵 来运行 动画
sprite->runAction(ActionForover);
//最后 我们把 熊精灵添加到 batch中去,前面我们已经把 batch添加到 场景中了.

spriteBatch->addChild(sprite);


参考了两篇文章:  

这一篇是 讲的 cocos2d 的

http://blog.csdn.net/boyjimeking/article/details/6707743

另一篇叫 地球人已经无法阻止程序员学习cocos2dx了, 这名字多欢乐O(∩_∩)O,开心学习了.


PS:本人小白,刚刚进入 cocos2dx ,有不对的地方望大家多多指点.


原创粉丝点击