cocos2d-x Button的监听使用
2016-01-31 01:52
435 查看
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, this,15,15,15);
void HelloWorld::touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type,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;
}
}
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, this,15,15,15);
void HelloWorld::touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type,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 创建精灵的五种方法
- Cocos2d-x 用lambda表达式实现监听,改变精灵的透明度和层级
- cocos2d-x-3.8.1 WP8 竖屏解决办法
- Cocos移植到Android的一些问题-中文乱码问题
- 13、Cocos2dx 3.0游戏开发找小三之3.0中的Director :郝萌主,一统江湖
- cocos2dx 3.0 飞机大战
- cocos2dx中使用tiled地图实现sprite动态遮挡
- 使用quick-cocos-x接入GameCenter
- cocos2dx - Sqlite简单封装使用
- cocospod 安装和使用(汇总)
- Windows下Cocos2dx-3.10的Android交叉编译环境搭建
- Cocos2dx3.4 VS2013无法打开包括文件extensions/ExtensionExport.h解决的方法
- cocos2dx CCLabelTTF自己定义字体的使用
- cocos2dx3.2 ——屏幕适配
- cocos2dx 3.x win7+VS2012开发环境搭建及HelloWorld
- cocos studio的node文件
- 《Cocos2d-x之Lua核心编程》读书笔记:Lua基础
- [置顶] 使用Android Studio来为Cocos2dx-lua项目打包
- cocos2d-x 2.x CCScale9Sprite shader渲染变灰方法
- cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程)