cocos2dx 单点触摸,按钮事件

1,单点触摸:
.h:
#include<vector>
using namespace std;
//触摸事件开始,手指按下时  
void onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event  *event);
//触摸移动事件,也就是手指在屏幕滑动的过程  
void onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event  *event);
//触摸事件结束,也就是手指松开时  
void onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event  *event); 
//打断触摸事件,一般是系统层级的消息,如来电话,触摸事件就会被打断  
void onTouchesCancelled(const std::vector<Touch*>& touches, cocos2d::Event  *event);
.cpp:
init(){
//允许接收触摸事件  
this->setTouchEnabled(true);  
}
void FirstScene::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event  *event)  
{  


}  
 
void FirstScene::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event  *event)  
{  


}  


void FirstScene::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event  *event)  
{  
 
}  
void FirstScene::onTouchesCancelled(const std::vector<Touch*>& touches, cocos2d::Event  *event)  
{  
log("TouchTest onTouchesCancelled");  
}  


2.按钮事件:
#include "ui/UIButton.h"
using namespace ui;
using namespace cocos2d;


.h:
 
void BtntouchEvent(cocos2d::Ref *pSender, TouchEventType type);
.cpp:
bool EndScene::init() {
if (!Layer::init()) {
return false;
}


this->scheduleUpdate();
visibSize = Director::getInstance()->getVisibleSize();
        Button*menuBtn = Button::create("res/menu.png");
        menuBtn->setPosition(Vec2(visibSize.width/2-50 , -100));
        addChild(menuBtn);
        menuBtn->setName("menuBtn");
        menuBtn->addTouchEventListener(this, toucheventselector(EndScene::BtntouchEvent));
return true;
}
 

void EndScene::BtntouchEvent(cocos2d::Ref *pSender, TouchEventType type)
{
Button*btn = (Button*)pSender;
switch (type) {
case cocos2d::ui::TOUCH_EVENT_BEGAN:
{
if(btn->getName()=="menuBtn")
{
CCDirector::sharedDirector()->replaceScene(FirstScene::createScene());
}
break;
}
case cocos2d::ui::TOUCH_EVENT_MOVED:
log("TOUCH_EVENT_MOVED");
break;
case cocos2d::ui::TOUCH_EVENT_ENDED:
log("TOUCH_EVENT_ENDED");
break;
case cocos2d::ui::TOUCH_EVENT_CANCELED:
log("TOUCH_EVENT_CANCELED");
break;
default:
break;
}
}

猜你喜欢

转载自blog.csdn.net/u014206831/article/details/77051146