天天看點

Cocos2d-x 在緩存建立圖檔

    CCSpriteFrameCache *cache=CCSpriteFrameCache::sharedSpriteFrameCache();

    cache->addSpriteFramesWithFile("ghosts.plist", "ghosts.png");

    CCTexture2D *texturee=CCTextureCache::sharedTextureCache()->textureForKey("ghosts.png");

    CCSpriteBatchNode *batchnode=CCSpriteBatchNode::createWithTexture(texturee, 10);

    this->addChild(batchnode,1);

    batchnode->setPosition(ccp(size.width/3,size.height/3));

    for (int i=0; i<10; i++) {

        int x=CCRANDOM_0_1()*320;

        int y=CCRANDOM_0_1()*32;

        CCSprite *spritte=CCSprite::createWithSpriteFrameName("father.gif");

        spritte->setPosition(ccp(x,y));

        batchnode->addChild(spritte);

    }

    //從緩存總讀取圖檔,圖檔是在ghosts.png中內建的

    CCMenuItemImage *closed=CCMenuItemImage::create();

    closed->setNormalSpriteFrame(cache->spriteFrameByName("sister1.gif"));

    closed->setSelectedSpriteFrame(cache->spriteFrameByName("sister2.gif"));

    closed->initWithTarget(this, menu_selector(HelloWorld::menuCloseCallback));

    closed->setPosition(ccp(size.width/5,size.height/5));

    //this->addChild(closed);

    CCMenu *menus=CCMenu::create(closed,NULL);

    this->addChild(menus);