上周刚刚入职一家手游公司,应主管大神的要求开始编辑页面ui,在添加button时发现一个问题,第一次button按钮,回调一次添加函数,但是之后就会点击一次回调两次,
问题在有,ui中的按钮将button事件集成了,点击按钮会执行 集成下的事件 解决方案就是为按钮的事件添加事件类型的选择
代码如下:
auto rootNode = CSLoader::createNode("MainScene.csb");
rootNode->setTag(111);
addChild(rootNode);
新出的cocos引擎v1.0 PreView集成下的cocostudio可以将编辑好的ui界面直接生成.csb文件并且发布到xcode工程(6以上版本)
auto common_silver=static_cast(rootNode->getChildByTag(12)->getChildByTag(13)->getChildByName("Button_5"));
common_silver->addTouchEventListener(this, toucheventselector(HelloWorld::touchEvent));
void touchEvent(Ref *pSender, TouchEventType type);
void HelloWorld::touchEvent(Ref *pSender, TouchEventType type)
{
switch (type)
{
case TOUCH_EVENT_BEGAN://触摸开始
if (silvercount>=2000)
{
//
auto label=(Label*)getChildByTag(111)->getChildByTag(12)->getChildByTag(13)->getChildByTag(116);
count+=10;
label->setString(StringUtils::format("战力提升%d%%",count));
silvercount-=2000;
auto label2=(Label*)getChildByTag(111)->getChildByTag(115);
label2->setString(StringUtils::format("%d",silvercount));
}
else
{
auto msg_toast_bg=Sprite::create("res/iphone/msg_toast_bg.png");
this->addChild(msg_toast_bg);
msg_toast_bg->setPosition(320,400);
auto msg_toast_label=Label::createWithSystemFont("银币不够", "", 30);
msg_toast_bg->addChild(msg_toast_label);
msg_toast_label->setPosition(320,25);
msg_toast_bg->runAction(MoveTo::create(2, Point(320 ,600)));
msg_toast_bg->runAction(Sequence::create(FadeIn::create(1),FadeOut::create(0.1f ),NULL));
msg_toast_label->runAction(Sequence::create(FadeIn::create(1),FadeOut::create(0.1f ),NULL));
// msg_toast_bg->runAction(Sequence::create(MoveTo::create(2, Point(320,600), CallFunc::create( HelloWorld::jump(), this) ), NULL));
//msg_toast_bg->runAction(Sequence::create(MoveTo::create(2, Point(320,600)),CallFunc::create(this, callfunc_selector(this,(HelloWorld::jump()))),NULL));
break;
}
}
}
循环播放一个动画
mowangb->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(2, 1.05), ScaleTo::create(2, 1),NULL)));
msg_toast_bg->runAction(MoveTo::create(2, Point(320 ,600)));
panle->setVisile(false);
panle->setVisible(true);
循环事件 每0.05执行一边
this->schedule(schedule_selector(Helloworld::hplogic),0.05);
转义字符%%
label2->setString(StringUtils::format("%d",silvercount));
事件的暂停与启用
if (panelGold->isVisible()){
Director::getInstance()->getEventDispatcher()->pauseEventListenersForTarget(this);
}else{
Director::getInstance()->getEventDispatcher()->resumeEventListenersForTarget(this);
}
void HelloWorld::hpLogic(float t)
{ hp++;
CCLOG("%d",hp);
if (hp>=100) {
hp=0;
}
auto loadingBar_left_to_right=static_cast(getChildByTag(111)->getChildByName("LoadingBar_1"));
loadingBar_left_to_right->setPercent(hp);
}