cocos2dx 用字符串创建按钮之 ControlButton
俺是个新手!没事就看看cpp-test 今天突然有一个重大发现……ControlButton 上次敲代码时候 就想直接用string创建 button!(png啥的弱爆了!!!) 结果翻遍了button的接口也没找到一个用string创建的! (才发现自己也弱爆了!!!)
不废话了,第一步:创建 static ControlButton* create(); static ControlButton* create(cocos2d::ui::Scale9Sprite* sprite); static ControlButton* create(Node* label,cocos2d::ui::Scale9Sprite* backgroundSprite); static ControlButton* create(const std::string& title,const std::string& fontName,float fontSize); </pre><p>头文件 </p><p></p><pre name="code" class="html"> #include "extensions/cocos-ext.h" USING_NS_CC_EXT;
.cpp 创建 <span style="white-space:pre"> </span>ControlButton *btn1 = ControlButton::create(); btn1->setPosition(VisibleRect::center()+Vec2(0,100)); this->addChild(btn1); ControlButton *btn2 = ControlButton::create(ui::Scale9Sprite::create("button.png")); btn2->setPosition(VisibleRect::center()); this->addChild(btn2); auto title = Label::createWithSystemFont("Name","fonts/Marker Felt.ttf",25); ControlButton *btn3 = ControlButton::create(title,ui::Scale9Sprite::create("button.png")); btn3->setPosition(VisibleRect::center()+Vec2(0,-100)); this->addChild(btn3); ControlButton *btn4 = ControlButton::create("Age",25); btn4->setPosition(VisibleRect::center()+Vec2(0,-200)); this->addChild(btn4); 没错! 创建的结果就是酱紫滴!!! 很明显, 第一个没有加载资源图,so 没东西(也可以单独加载!) 第二个 创建的九宫格,没设置尺寸(是九宫格没设置哦),so 现实的图是一半一半,我也没搞明白(囧,无所谓了,用的时候肯定要设置的,设置了就没事了) 第三个 用label创建的 第四个 直接用sting创建的 回调函数声明 void touchAction(Ref *sender,Control::EventType controlEvent); 设置事件处理程序 btn2->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorldScene::touchAction),<span style="background-color: rgb(255,0);">Control::EventType::TOUCH_DOWN</span>); 红色的这货是啥? 啥子都不是,就当她是娘,看看他孩子 TOUCH_DOWN 按下按钮调用事件
/* 话说:麻雀虽小,五脏俱全 */ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |