cocos2d-x之texture生成精灵和menu

生成精灵

    SpriteFrameCache* cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("common.plist", "common.png");

    //back_highlight和back为生成纹理之前的图片名字
    Sprite* backHighlightSprite = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("back_highlight.png"));
    backHighlightSprite->setPosition(Vec2(100, 100));
    addChild(backHighlightSprite);

    Sprite* backNormalSprite = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("back.png"));
    backNormalSprite->setPosition(Vec2(300, 300));
    addChild(backNormalSprite);

 

生成菜单

    SpriteFrameCache* cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("common.plist", "common.png");

    MenuItemImage* backItemImg = MenuItemImage::create();
    backItemImg->setCallback(GameRuleLayer::backMehuCallBack);
    backItemImg->setNormalSpriteFrame(cache->getSpriteFrameByName("back.png"));
    backItemImg->setSelectedSpriteFrame(cache->getSpriteFrameByName("back_highlight.png"));

    Menu* menu = Menu::create(backItemImg,NULL);
    Size size = Director::getInstance()->getVisibleSize();
    menu->setPosition(Vec2(100, size.height + + origin.y - 45));
    this->addChild(menu);

菜单的回调函数

static void backMehuCallBack(cocos2d::Ref* pSender);
void GameRuleLayer::backMehuCallBack(cocos2d::Ref* pSender)
{
    Scene* scene= GameMainMenuScene::createScene();
    Scene* newScene = TransitionFade::create(0.5,scene);
    Director::getInstance()->replaceScene(newScene);
}

 

你可能感兴趣的:(cocos2d-x)