• 【Cocosd2d-x CCMenu菜单之二】


    菜单项CCMenuItem是一个基类。 子类CCMenuItemFont、CCMenuItemLabel、CCMenuItemSprite、CCMenuItemToggle可增加CCMenu中形成菜单。

    程序代码:

    bool HelloWorld::init()
    {
    
        if ( !CCLayer::init() )
        {
            return false;
        }
        
        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
        CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                            "CloseNormal.png",
                                            "CloseSelected.png",
                                            this,
                                            menu_selector(HelloWorld::menuCloseCallback));
        
    	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                    origin.y + pCloseItem->getContentSize().height/2));
    
        // create menu, it's an autorelease object
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        this->addChild(pMenu, 1);
    
    	//add your code...
    	//使用字体定义菜单项,包含 CCMenuItemAtlasFont, CCMenuItemFont 两种方式,CCMenuItemFont通过设置字体名称来设置字体。   
    	CCMenuItemFont::setFontSize(30); //字号  
    	CCMenuItemFont::setFontName("Courier New");//字体  
    	CCMenuItemFont *item = CCMenuItemFont::create("--- Go Back ---", this, menu_selector(HelloWorld::menuItemFontcall));//參数1:文字内容。參数2:运行回调函数的目标类。參数3:按下触发的回调函数  
    	//item->setPosition(ccp(visibleSize.width/2,100));  
    
    	//通过字体配置PNG文件的 CCLabelAtlas  
    	CCLabelAtlas *atlasfont =  CCLabelAtlas::create("0123456789", "labelatlas.png", 16, 24, '.');//參数顺序:文字内容,图片,单个文字宽,单个文字高,默认显示字符   
    	CCMenuItemLabel *item1 = CCMenuItemLabel::create(atlasfont, this, menu_selector(HelloWorld::menuItemFontcall2));//參数顺序:文字渲染类,运行回调函数的目标类,按下触发的回调函数    
    	//item1->setPosition(ccp(visibleSize.width/2,400));  
    
    	//精灵菜单项  
    	CCSprite *spriteNormal   = CCSprite::create("menuitemsprite.png",  CCRectMake(0,23*2,115,23));  
    	CCSprite *spriteSelected = CCSprite::create("menuitemsprite.png",  CCRectMake(0,23*1,115,23));  
    	CCSprite *spriteDisabled = CCSprite::create("menuitemsprite.png",  CCRectMake(0,23*0,115,23));  
    
    	CCMenuItemSprite* item3 = CCMenuItemSprite::create(spriteNormal, spriteSelected, spriteDisabled, this, menu_selector(HelloWorld::menuCallback3));//參数:3种菜单状态及回调函数  
    	//item3->setPosition(ccp(visibleSize.width/2,300));  
    
    	//通过字体配置FNT类型文件的CCLabelBMFont  
    	CCLabelBMFont* label = CCLabelBMFont::create("Enable AtlasItem", "bitmapFontTest3.fnt");  
    	CCMenuItemLabel* item4 = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::menuCallback4) );  
    	//item4->setPosition(ccp(visibleSize.width/2+50,250));  
    
    	//触发器菜单项,能够将不论什么菜单项传递进去  
    	CCMenuItemToggle *toggle = CCMenuItemToggle::createWithTarget(  this,  
    		menu_selector(HelloWorld::menuCallback5),   
    		CCMenuItemFont::create("On"),  
    		CCMenuItemFont::create("Off"),   
    		NULL);  
    	//toggle->setPosition(ccp(visibleSize.width/2+100, 150));  
    
    	CCMenu *menu = CCMenu::create(item, item1, item3, item4, toggle, NULL); 
    	menu->alignItemsVertically();
    	
    
    	CCSize s = CCDirector::sharedDirector()->getWinSize();
    
    	int i=0;
    	CCNode* child;
    	CCArray * pArray = menu->getChildren(); //获取menu全部节点(菜单项)
    	CCObject* pObject = NULL;
    	CCARRAY_FOREACH(pArray, pObject)
    	{
    		if(pObject == NULL)
    			break;
    
    		child = (CCNode*)pObject;
    
    		CCPoint dstPoint = child->getPosition();//获取每一个节点位置
    		int offset = (int) (s.width/2 + 50);
    		if( i % 2 == 0)
    			offset = -offset;
    
    		child->setPosition( ccp( dstPoint.x + offset, dstPoint.y) );//设置菜单项初始位置
    		child->runAction( 
    			CCEaseElasticOut::create(CCMoveBy::create(2, ccp(dstPoint.x - offset,0)), 0.35f) //从初始位置移动新的位置。有振幅效果
    			);
    		i++;
    	}
    
    
    	menu->setPosition(ccp(s.width/2, s.height/2));
    	this->addChild(menu);
    
    	return true; 
    
    }
    
    void HelloWorld::menuItemFontcall(CCObject* pSender)
    {
    	CCLOG("menuItemFontcall...");
    }
    
    void HelloWorld::menuItemFontcall2(CCObject* pSender)
    {
    	CCLOG("menuItemFontcall2...");
    }
    void HelloWorld::menuCallback3(CCObject* pSender)
    {
    	CCLOG("menuCallback3...");
    }
    void HelloWorld::menuCallback4(CCObject* pSender)
    {
    	CCLOG("menuCallback4...");
    }
    void HelloWorld::menuCallback5(CCObject* pSender)
    {
    	CCLOG("menuCallback5...");
    }


     


  • 相关阅读:
    Common Words
    The end of other
    避免ssh断开导致运行命令的终止:screen
    Check iO:初学Python
    linux环境c++开发:ubuntu12.04使用llvm3.4.2
    boost库使用:仿SGI-STL实现的一个树节点allocator
    boost库使用:vs2013下boost::container::vector编译出错解决
    读论文系列:Nearest Keyword Search in XML Documents中使用的数据结构(CT、ECT)
    房产律师咨询
    E文阅读
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6763548.html
Copyright © 2020-2023  润新知