cocos2d-x Button的监听使用

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

virtual bool init();
void touchEvent(Ref *pSender,cocos2d::ui::Widget::TouchEventType type,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);

Button* button = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
button->setPosition(Vec2(wsize.width / 2,wsize.height / 2));
button->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent,this,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::touchEvent(Ref *pSender,int tt)
{

log("---------------->%d", 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;
}

}



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