2013-02-16 31 views
0

請幫助我,我是一個初學者在cocos2d-x。 我有物品清單。 我怎樣才能做到這一點使用CCMenu :: createWithArray在最後顯示這個列表的項目數組? 我想控制菜單數組,從數組中添加/刪除項目(從我的列表菜單)。獲取項目數組。使用CCMenu :: createWithArray

這裏是代碼:

`` ...

CCLabelTTF* pp0BtnLabel = CCLabelTTF::create(string(ItemName[0]).c_str(), "Arial", TITLE_FONT_SIZE); 
CCMenuItemLabel *pp0Item = CCMenuItemLabel::create(
      pp0BtnLabel, 
      this, 
      menu_selector(Window::CheckItemCallback)); 
pp0Item->setTag(ItemTag[0]);  
CC_BREAK_IF(! pp0Item); 
pp0Item->setPosition(ccp(size.width*0.3f, size.height*0.8)); 

CCLabelTTF* pp1BtnLabel = CCLabelTTF::create(string(ItemName[0]).c_str(), "Arial", TITLE_FONT_SIZE); 
CCMenuItemLabel *pp1Item = CCMenuItemLabel::create(
      pp1BtnLabel, 
      this, 
      menu_selector(Window::CheckItemCallback)); 
pp1Item->setTag(ItemTag[0]);  
CC_BREAK_IF(! pp1Item); 
pp1Item->setPosition(ccp(size.width*0.3f, size.height*0.75)); 

CCLabelTTF* pp2BtnLabel = CCLabelTTF::create(string(ItemName[0]).c_str(), "Arial", TITLE_FONT_SIZE); 
CCMenuItemLabel *pp2Item = CCMenuItemLabel::create(
      pp2BtnLabel, 
      this, 
      menu_selector(Window::CheckItemCallback)); 
pp2Item->setTag(ItemTag[0]);  
CC_BREAK_IF(! pp2Item); 
pp2Item->setPosition(ccp(size.width*0.3f, size.height*0.7)); 

CCMenu* pMenuChapter = CCMenu::create(pp0Item, pp1Item, pp2Item, NULL); 
pMenuChapter->setPosition(CCPointZero); 
CC_BREAK_IF(! pMenuChapter); 

this->addChild(pMenuChapter, 1); 

      ... 

``

回答

0

我不知道理解你的問題......但這裏是你如何能使用CCArray創建一個CCMenu。

CCLabelTTF*label = CCLabelTTF::create("label text", "Arial", 32); 

CCMenuItemLabel *item1 = CCMenuItemLabel::create(label, this, menu_selector(CPhysicsLayer::itemCallback)); 
CCMenuItemLabel *item2 = CCMenuItemLabel::create(label, this, menu_selector(CPhysicsLayer::itemCallback)); 
CCMenuItemLabel *item3 = CCMenuItemLabel::create(label, this, menu_selector(CPhysicsLayer::itemCallback)); 

CCArray*array = CCArray::create(item1, item2, item3); 
CCMenu*menu = CCMenu::createWithArray(array); 

/// Iterate over the menu items 
CCObject*obj = NULL; 
CCARRAY_FOREACH(array, obj) { 
    CCMenuItemLabel*item = (CCMenuItemLabel*)obj; 
    /// Do something with the item... 
} 

若要添加項目到菜單中的「運行時」,然後使用CCMenu ::的addChild方法(和removeChild之刪除的項)。

希望它有幫助。

+0

非常感謝你! – user2077802 2013-02-16 08:21:32

+0

不客氣。如果您發現anwser有用,請投票;) – 2013-02-16 14:37:19