[cocos2d-x3.3]cocostudio的Button按钮触发两次回调函数问题解决

上周刚刚入职一家手游公司,应主管大神的要求开始编辑页面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<ui::Button *>(rootNode->getChildByTag(12)->getChildByTag(13)->getChildByName("Button_5"));
    common_silver->addTouchEventListener(this, toucheventselector(HelloWorld::touchEvent));


获取ui中的Buttonu并且执行回调函数,参数添加事件类型选择

  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)));
<p class="p1"> msg_toast_bg-><span class="s1">runAction</span>(<span class="s2">MoveTo</span>::<span class="s1">create</span>(<span class="s3">2</span>, <span class="s2">Point</span>(<span class="s3">320</span> ,<span class="s3">600</span>)));</p>

设置一个层的现实隐藏

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);
                        }

cocostudio中loardingbar的使用

void HelloWorld::hpLogic(float t)
{    hp++;
    CCLOG("%d",hp);
    if (hp>=100) {
        hp=0;
    }
   
    auto loadingBar_left_to_right=static_cast<ui::LoadingBar *>(getChildByTag(111)->getChildByName("LoadingBar_1"));
    loadingBar_left_to_right->setPercent(hp);
  
    
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值