cocos2d-x Button的监听使用

前端之家收集整理的这篇文章主要介绍了cocos2d-x Button的监听使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

virtual bool init();
void touchEvent(Ref *pSender,cocos2d::ui::Widget::TouchEventType type,@H_404_2@int tt);


Size wsize = Director::getInstance()->getVisibleSize();
_displayValueLabel = cocos2d::ui::Text::create("No Event","fonts/Marker Felt.ttf",32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f,-1.0f));
_displayValueLabel->setPosition(Vec2(wsize.width / 2.0f,wsize.height / 2.0f + 100));
this->addChild(_displayValueLabel);

@H_404_2@Button* button = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
button->setPosition(Vec2(wsize.width / 2,wsize.height / 2));
button->addTouchEventListener(@H_404_2@CC_CALLBACK_2(HelloWorld::@H_404_2@touchEvent,this,@H_404_2@int tt));
button->setZoomScale(0.4f);
button->setPressedActionEnabled(true);
this->addChild(button);
button->setOpacity(100);

//CC_CALLBACK_0的this不是参数,而是执行这个回调函数的具体对象
//这个0也不能理解成多少个参数,而是指的占位符,比如某个回调函数有5个参数,如add(int,int,int,int,int),
//然后用CC_CALLBACK_2占上前两个参数,给后面三个参数绑定,可以写成
//CC_CALLBACK_2(LogicTest::add,15,15);


void HelloWorld::@H_404_2@touchEvent(Ref *pSender,@H_404_2@int tt)
{

log("---------------->%d",@H_404_2@ tt);//测试多个参数
switch (type)
{
case cocos2d::ui::Widget::TouchEventType::BEGAN:
_displayValueLabel->setString(String::createWithFormat("Touch Down")->getCString());
break;
case Widget::TouchEventType::MOVED:
_displayValueLabel->setString(String::createWithFormat("Touch Move")->getCString());
break;

case Widget::TouchEventType::ENDED:
break;

case Widget::TouchEventType::CANCELED:
_displayValueLabel->setString(String::createWithFormat("Touch Cancelled")->getCString());
break;
default:
break;
}

}


原文链接:https://www.f2er.com/cocos2dx/340274.html

猜你在找的Cocos2d-x相关文章