一、菜单类CCMenu:
CCMenu是一个菜单项的容器,用来装载各种菜单项。
(1)、定义CCMenu类实例:
MenuLayer2::MenuLayer2(){
for(int i =0;i < 2 ;++i){
CCMenuItemImage* item1 =
CCMenuItemImage::create(s_playNormal,s_PlaySelect,this,menu_selector(MenuLayer2::menuCallback));
CCMenuItemImage* item2 = CCMenuItemImage::create(s_HighNormal,s_HighSelect,this,menu_selector(MenuLayer2::menuCallbackOpacity));
CCMenuItemImage* item3 = CCMenuItemImage::create(s_AboutNormal,s_AboutSelect,this,menu_selector(MenuLayer2::menuCallbackAlign));
item1->setScaleX(1.5f);
item2->setScaleX(0,5f);
item3->setScaleX(0,5f);
CCMenu* menu = CCMenu::create(item1,item2,item3,NULL);
CCSize s = CCDirector::sharedDirector()->getWInSize();
menu->setPosition(ccp(s.width/2,s.height/2));
menu->setTag(kTagMenu);
addChild(menu,0,100+i);
m_centeredMenu = menu->getPosition();
}
m_aligne = true;
alignMenusH();
}
//alignMenusH菜单类是水平对其两种方法对比,一种是alignItemsHorizontally 水平对齐,底下是alignItemsHorizontallWithPadding留空间水平对齐,效果对比如图:
void MenuLayer2::alignMenusH(){
for(int i = 0;i<2;++i){
CCMenu* menu = (CCMenu*)getChildByTag(100+i);
menu->setPosition(m_centeredMenu);
if(i==0){
menu->alignItemsHorizontally();
CCPoint p = menu->getPosition(); //这句是不是有点脑残,对于上面的menu
menu->setPosition(ccpAdd(p,CCPointMake(0,30)));
}
else{
menu->alignItemsHorizontallyWithPadding(40);
CCPoint p = menu->getPosition();
menu->setPosition(ccpSub(p,CCPointMake(0,30)));
}
}
}
二、控件类及其子类:
1)拖动滑块的控件类CCControlSlider:
定义并初始化CCControlSliderTestlei实例:
bool CCControlSliderTest::init(){
if(CCControlScene::init()){
CCSize screenSize = CCDirector::sharedDIrector()->getWinSize();
//...省略定义标签的代码
CCControlSlider *slider = CCControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png",
"extensions/sliderThumb.png");
slider->setAnchorPoint(ccp(0,5f,1.0f));
slider->setMinimumValue(0,0f);
slider->setMaximumValue(5.0f);
sldier->setPosition(ccp(screenSize.width/2.0f,screenSize.height/2.0f));
slider->addTargetWithActionForControlEvents(this,cccontrol_selecor(CCControlSliderTest::valueChanged),
CCControlEventValueChanged);
addChild(slider);
return true;
}
return false;
}
void CCControlSliderTest::valueChanged(CCObject* sender,CCControlEvent controlEvent){
CCControlSlider* pSlider = (CCControlSlider*)sender;
m_pDisplayValueLabel->setString(CCString::createWithFormat("Slider value =
%.02f",pSlider->getValue())->getCString());
}
2)颜色选择盘类CCControlColourPicker:
CCControlColourPicker类的定义和初始化:
bool CCControlColourPickerTest::init(){
if(CCControlScene::init()){
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCNode *layer = CCNode::create();
layer->setPosition(ccp(screenSize.width/2,screenSize.height/2));
addChild(layer,1);
double layer_width = 0;
CCControlColourPicker* colourPicker = CCControlColourPicker::create();
colourPicker->setColor(ccc3(37,46,252));
colourPicker->setPosition(ccp(colourPicker->getContentSize().width/2,0));
layer->addChild(colourPicker);
colourPicker->addTargetWithActionForControlEvents(this,cccontrol_elector(CCControlColourPickerTest::colourValueChanged),
CCControlEventValueChanged);
//定义其他控件的代码 ...
return true;
}
return false;
}
void CCControlColourPickerTest::colourValueChanged(CCObject* sender,CCControlEvent controlEvent){
CCControlColourPicker* pPicker = (CCControlColourPicker*)sender;
m_pColorLabel->setString(CCString::createWithFormat("#%02X%02X%02X",pPicker->getColorValue().r,
pPicker->getColorValue().g,pPicker->getColorValue().b)->getCString());
}
3)开关类CCControlSwitch:
CCControlSwitch的定义和初始化:
boo CCControlSwitchTest::init(){
if(CCControlScene::init()){
//定义其他控件 ...
CCControlSwitch* switchControl = CCControlSwitch;;create(
CCSprite::create("extensions/switch-mask.png"),
CCSprite::create("extensions/switch-on.png"),
CCSprite::create("extensions/switch-off.png"),
CCSprite::create("extensions/switch-thumb.png"),
CCLabelTTF::create("On","Arial-BoldMT",16),
CCLabelTTF::create("Off","Arial-BoldMT",16)
);
switchControl->setPosition(ccp(layer-width+10+switchControl->getContentSize().width/2,0));
layer->addChild(switchControl);
switchControl->addTargetWithActionForControlEvents(this,cccontrol_selector(CCControlSwitchTest::valueChanged),
CCControlEventValueChanged);
}//其他控件代码省略 ...
return true;
}
return false;
//参数:背景图片路径,开状态背景图片,关状态背景图片,开关背景图片,开文字标签,关文字标签
4)按钮类CCControlButton:
CCControlButton类的定义和初始化:
bool CCConrolButtonTest_Event::init(){
if(CCControlScene::init()){
//...
CCControlButton * controlButton = CCControlButton::create(titileButton,backgroundButton);
controlButton->setBackgroundSpriteForState(backgroundHighlightButton,CCControlStateHighlighted);
controlButton->setTitleColorForState(ccWHITE,CCControlStateHighlighted);
controlButton->setAnchorPoint(ccp(0,5f,1));
controlButton->setPosition(ccp(screenSize.width/2.0f,screenSize.height/2.0f));
addChild(controlButton,1);
//...
controlButton->addTargetWithActionForControlEvent(this,cccontrolselector(CCControlButtonTest_Event::touchDownAction),
CCControlEventTouchDown);
cont rolButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDragInsideAction),
CCControlEventTouchDragInside);
cont rolButton->addTargetWithActionForControlEvent(this,cccontrol_selector(CCControlButtonTest_Event::touchDragOutsideAction),
CCControlEventTouchDragOutside);
cont rolButton->addTargetWithActionForControlEvent(this,cccontrol_selector(CCControlButtonTest_Event::touchDragEnterAction),
CCControlEventTouchDragEnter);
cont rolButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDragExitAction),
CCCControlEventTouchDragExit);
cont rolButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchUpInsideAction),
CCControlEventTouchUpInside);
cont rolButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchUpOutsideAction),
CCControlEventTouchUpOutside);
cont rolButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchCancelAction),
CCControlEventTouchCancel);
return true;
}
return false;
}