获取Cocos Studio制作的层

ui::Widget* m_pWidgetRoot= dynamic_cast(cocostudio::GUIReader::getInstance()->widgetFromJsonFile(“CpSelSexLayer.ExportJson”));//这里Cocos Studio对应层类型可以转换成Layout类型;

//获取Layout对象(Cocos Studio对应层类型)

m_pLayoutSex = (Layout*)Helper::seekWidgetByName(m_pWidgetRoot, "Panel_eq");

m_pLayoutSex->setBackGroundImage(addImgFile("OtherImage.bundle/Champion/cp_u_bk.jpg"));

//获取ImageView对象(Cocos Studio对应图片类型)

ImageView* pImgTitle = (ImageView*)Helper::seekWidgetByName(m_pWidgetRoot, "Image_title");

pImgTitle->loadTexture("cp_s_title_tw.png" );

//获取Button对象(Cocos Studio对应按钮类型)

Button* pBtn = (Button*)Helper::seekWidgetByName(m_pWidgetRoot, "Button_back");

pBtn->addTouchEventListener(CC_CALLBACK_2(CpSelSexLayer::onButtonCallback, this));

或者

pBtn->addTouchEventListener(Widget::ccWidgetTouchCallback(CC_CALLBACK_2(CpSelSexLayer::onButtonCallback, this)));

 

void CpSelSexLayer::onButtonCallback(Ref* sender, Widget::TouchEventType type){

Button* pBtn = (Button*)sender;

if (pButton && type == Widget::TouchEventType::ENDED)

{

CCLOG(“123”);

}

}

 

设置按钮的颜色:

pBtn->getVirtualRenderer()->setColor(Color3B::GRAY));

pBtn->getVirtualRenderer()获取的是一个Scale9Sprite*类型图片

使用Button控件在按钮上加文字,直接调用Button里面的接口就可以实现,就不用在自己创建一个Label或者Text类型的文本放到上面去;

pBtn ->setTitleFontName(FONT_APP);

pBtn ->setTitleFontSize(20);

pBtn ->setTitleText(“1123”);

如何将Button的文本转换成Label类型

Label* label =pBtn ->getTitleRenderer();

//获取Text对象(Cocos Studio对应文本框类型)

Text* pTitle = (Text *)pBtn->getChildByName("Label_title");

Text类型如何转换成Label类型:

Label* label =(Label*)pTitle->getVirtualRenderer();

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