135-1821-9792

十:Cocos2d-x缓存机制-创新互联

Sprite中图片加载会占用内存,而多次使用同一个图片精灵时,会使用到缓存机制。 还有有多个角色,多个怪物,多个动作时,要怎么利用内存的缓存机制。

成都创新互联专注于锦江企业网站建设,成都响应式网站建设公司,成都商城网站开发。锦江网站建设公司,为锦江等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

1. 纹理缓存(图片缓存)  CCTexture2D.h  (整个引擎只有一个)

        以文件名创建精灵时,其实还是要先加载成CCTexture2D,也可以直接用CCTexture2D创建。CCTexture2D有机制实现缓存。

  1. bool CCSprite::initWithFile(const char *pszFilename)

  2. {

  3.   CCAssert(pszFilename != NULL, "Invalid filename for sprite");

  4.    CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename);

  5.   if (pTexture)

  6.   {

  7.     CCRect rect = CCRectZero;

  8.     rect.size = pTexture->getContentSize();

  9.     return initWithTexture(pTexture, rect);

  10.   }

  11.   // don't release here.

  12.   // when load texture failed, it's better to get a "transparent" sprite then a crashed program

  13.   // this->release();

  14.   return false;

  15. }

  16. for(int i=0; i<9999; i++)

  17. {

  18. CCSprite* mysprite = CCSprite::create("CloseSelected.png");

  19. mysprite->setPosition( ccp (CCRANDOM_0_1()*480,CCRANDOM_0_1()*320) );

  20. this->addChild(mysprite,1);

  21. }

  22. for(int i=0; i<99; i++)

  23. {

  24. CCSprite* mysprite = CCSprite::create("CloseSelected.png");

  25. mysprite->setPosition(ccp(CCRANDOM_0_1()*480,CCRANDOM_0_1()*320));

  26. this->addChild(mysprite,1);

  27. }

  28. 精灵帧缓存(动画帧)

  29. CCSpriteBatchNode* batchSprite = CCSpriteBatchNode::create("CloseSelected.png");

  30. batchSprite->setPosition(CCPointZero);

  31. this->addChild(batchSprite,1);

  32. for (int i = 0; i<99 ; i++)

  33. {

  34. CCSprite* mysprite2 = CCSprite::createWithTexture(batchSprite->getTexture());

  35. mysprite2->setPosition(ccp(CCRANDOM_0_1()*360,CCRANDOM_0_1()*240));

  36. this->addChild(mysprite2,1);

  37. }

CCSpriteBatchNode,批精灵创建,可以认为是一个精灵容器。

3.动画缓存

    采用地图编辑器,将小图片编辑到一张大图片上,然后生成一张大图片和对应的plist文件。

    在plist文件中存放的每一帧都是一张小图片,在plist中旋转的图片在作为帧被使用时会自动还原。

    plist文件中每一帧的文件仍是源文件的名字,加载时使用小图片的名字即可,而且源文件被删除也不影响plist文件的使用。

缓存机制

CCSpriteCCTexture2D
CCTextureCache




CCSpriteBatchNodeCCTextureAtlas
CCTextureCache




CCAnimation

CCAnimationFrame

CCSpriteFrameCCSpriteFrameCache



CCAnimationCache


CCTexturePcaker

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:十:Cocos2d-x缓存机制-创新互联
当前链接:http://kswsj.com/article/dhsged.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号