[Cocos2d-x For WP8]Menu菜单

    菜单栏这是游戏里面基本的控件来的,

头文件:

#include <CCMenu.h>                       

继承关系图:

 

头文件:

 #include <CCMenuItem.h>                       

继承关系图:

   CCNode CCObject CCCopying CCMenuItemLabel CCMenuItemSprite CCMenuItemToggle CCMenuItemAtlasFont CCMenuItemFont CCMenuItemImage                         

1 CCMenuItemLable

包括CCMenuItemAtlasFont和CCMenuItemFont是两种定义菜单项字体的方式,其中CCMenuItemFont是通过设定字体名称来设置字体,这个字体是系统自带的。CCMenuItemAtlasFont则是通过字体配置文件png文件,即配置文件所对应的图片。

2.CCMenuItemSprite 这个菜单项的特点就是可以封装图片进入菜单项

3.CCMenuItemToggle 可以将任意的CCMenuItem封装进去,作为一个按钮式的开关

示例代码:

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {   // 'scene'是一个可以自动释放的对象
        scene = CCScene::create();
        //创建失败跳出循环
        CC_BREAK_IF(! scene);
        MenuLayer1 *layer = new MenuLayer1();
        scene->addChild(layer);
    } while (0);
    // 返回scene
    return scene;
}

MenuLayer1::MenuLayer1()
{
    //设置默认字体的大小
    CCMenuItemFont::setFontSize( 30 );
    //设置默认字体的名字
    CCMenuItemFont::setFontName("Courier New");
    // Font Item
    // 创建三个精灵作为字体的三个状态
    CCSprite* spriteNormal = CCSprite::create("cat.png", CCRectMake(0,23*2,115,23));
    CCSprite* spriteSelected = CCSprite::create("cat.png", CCRectMake(0,23*1,115,23));
    CCSprite* spriteDisabled = CCSprite::create("cat.png", CCRectMake(0,23*0,115,23));
    //dynamic_cast<CCNode*>(mgr)->addChild(spriteNormal);
    //dynamic_cast<CCNode*>(mgr)->addChild(spriteSelected);
    //dynamic_cast<CCNode*>(mgr)->addChild(spriteDisabled);

    //创建一个精灵菜单
    CCMenuItemSprite* item1 = CCMenuItemSprite::create(spriteNormal, spriteSelected, spriteDisabled, this, menu_selector(MenuLayer1::menuCallback) );
    
    //创建一个图片的菜单栏项 Image Item
    CCMenuItem* item2 = CCMenuItemImage::create("SendScoreButton.png", "SendScoreButtonPressed.png", this, menu_selector(MenuLayer1::menuCallback2) );

    //创建一个label  Label Item (LabelAtlas)
    CCLabelAtlas* labelAtlas = CCLabelAtlas::create("0123456789", "fps_images.png", 16, 24, '.');
    //创建一个label菜单栏
    CCMenuItemLabel* item3 = CCMenuItemLabel::create(labelAtlas, this, menu_selector(MenuLayer1::menuCallbackDisabled) );
    item3->setDisabledColor( ccc3(32,32,64) );
    item3->setColor( ccc3(200,200,255) );
    
    //创建一个字体的菜单栏 Font Item
    CCMenuItemFont *item4 = CCMenuItemFont::create("I toggle enable items", this, menu_selector(MenuLayer1::menuCallbackEnable) );

    item4->setFontSizeObj(20);
    item4->setFontName("Marker Felt");
    
    // Font Item
    CCMenuItemFont* item5 = CCMenuItemFont::create("Quit", this, menu_selector(MenuLayer1::onQuit));
    //创建一个颜色变化的动作
    CCActionInterval* color_action = CCTintBy::create(0.5f, 0, -255, -255);
    //逆向动作
    CCActionInterval* color_back = color_action->reverse();
    //创建一个动作的组合
    CCFiniteTimeAction* seq = CCSequence::create(color_action, color_back, NULL);
    //重复地运行该动作
    item5->runAction(CCRepeatForever::create((CCActionInterval*)seq));
    //创建菜单栏
    CCMenu* menu = CCMenu::create( item1, item2, item3, item4, item5, NULL);
    //垂直布局
    menu->alignItemsVertically();
    
    
    //创建菜单栏的弹跳效果 elastic effect
    //获取屏幕的大小
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    
    int i=0;
    CCNode* child;
    CCArray * pArray = menu->getChildren();
    CCObject* pObject = NULL;
    //类似foreach循环
    CCARRAY_FOREACH(pArray, pObject)
    {
        if(pObject == NULL)
            break;
        //转化为基类CCNode
        child = (CCNode*)pObject;
        //获取坐标,该坐标相对于左下角作为(0,0)参考点
        CCPoint dstPoint = child->getPosition();
        int offset = (int) (s.width/2 + 50);
        if( i % 2 == 0)
            offset = -offset;
        
        child->setPosition( CCPointMake( dstPoint.x + offset, dstPoint.y) );
        child->runAction( // 创建弹跳效果的动画
                            CCEaseElasticOut::create(
                                                                CCMoveBy::create(2, CCPointMake(dstPoint.x - offset,0)), 0.35f
                                                            ) 
                        );
        i++;
    }

    m_disabledItem = item3; item3->retain();
    //m_disabledItem->setIsEnabled( false );

    addChild(menu);

}

void MenuLayer1::registerWithTouchDispatcher()
{
     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, cocos2d::kCCMenuHandlerPriority+1, true);
}


MenuLayer1::~MenuLayer1()
{
    m_disabledItem->release();
}

void MenuLayer1::menuCallback(CCObject* sender)
{

}

void MenuLayer1::menuCallbackConfig(CCObject* sender)
{

}

void MenuLayer1::allowTouches(ccTime dt)
{
     CCDirector::sharedDirector()->getTouchDispatcher()->setPriority(cocos2d::kCCMenuHandlerPriority+1, this);
    unscheduleAllSelectors();
    CCLog("TOUCHES ALLOWED AGAIN");
}

void MenuLayer1::menuCallbackDisabled(CCObject* sender) 
{
    // hijack all touch events for 5 seconds
     CCDirector::sharedDirector()->getTouchDispatcher()->setPriority(cocos2d::kCCMenuHandlerPriority-1, this);
    schedule(schedule_selector(MenuLayer1::allowTouches), 5.0f);
    CCLog("TOUCHES DISABLED FOR 5 SECONDS");
}

void MenuLayer1::menuCallbackEnable(CCObject* sender) 
{
    m_disabledItem->setEnabled(! m_disabledItem->isEnabled() );
}

void MenuLayer1::menuCallback2(CCObject* sender)
{
    ((CCLayerMultiplex*)m_pParent)->switchTo(2);
}

void MenuLayer1::onQuit(CCObject* sender)
{
    //[[Director sharedDirector] end];
    //getCocosApp()->exit();
}

运行的效果:

 

 

 

posted on 2013-08-24 16:41  linzheng  阅读(901)  评论(0编辑  收藏  举报

导航