cocos2d-x学习记录3——CCTouch触摸响应

时间:2023-03-08 22:00:12

游戏不同于影音,强交互性是其一大特色,在游戏中主要体现为接受用户的输入并响应。智能手机触摸是其重要的输入方式。

在cocos2d-x中,触摸分为单点触摸和多点触摸。

单点触摸:主要继承CCTargetedTouchDelegate 实现。

多点触摸:主要继承CCStandardTouchDelegate实现。

MyScene.h

 #ifndef MyScene_H_H
#define MyScene_H_H #include "cocos2d.h"
using namespace cocos2d; class MyScene : public CCLayer
{
public:
static CCScene* createScene();
virtual bool init();
CREATE_FUNC( MyScene ); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); virtual void registerWithTouchDispatcher(); private:
}; #endif

MyScene.cpp

 #include "MyScene.h"

 CCScene* MyScene::createScene()
{
CCScene *scene = CCScene::create();
MyScene *layer = MyScene::create();
scene->addChild(layer);
return scene;
}; bool MyScene::init()
{
if( !CCLayer::init() ){
return false;
} CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite *sprite = CCSprite::create("pal4.png");
sprite->setAnchorPoint( ccp(0.5, 0.5) );
//sprite->setPosition( ccp(size.width/2, size.height/2) );
sprite->setPosition( ccp(size.width/, size.height/) );
sprite->setScale(0.5f);
sprite->setTag();
addChild(sprite); setTouchEnabled(true); return true;
} bool MyScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint point = pTouch->getLocationInView();
point = CCDirector::sharedDirector()->convertToGL(point);
CCNode *node = getChildByTag();
float x = node->getPositionX();
float y = node->getPositionY();
float width = node->getContentSize().width*node->getScale();
float height = node->getContentSize().height*node->getScale();
//CCRect rect = CCRectMake(node->getPositionX()-node->getContentSize().width/2, node->getPositionY()-node->getContentSize().height/2,
// node->getContentSize().width, node->getContentSize().height);
CCRect rect = CCRectMake(x-width/, y-height/, width, height); //CCLog("Touch start! %.1f,%.1f", point.x, point.y);
//CCMessageBox("start","info");
//return true; if( rect.containsPoint(point) ){
CCLog("Touch start! %.1f,%.1f", point.x, point.y);
return true;
} return false;
} void MyScene::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint point = pTouch->getLocation();
CCLog("Touch moved! %.1f,%.1f", point.x, point.y);
} void MyScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint point = pTouch->getLocation();
CCLog("Touch ended! %.1f, %.1f", point.x, point.y);
} void MyScene::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint point = pTouch->getLocation();
CCLog("Touch canceled! %.1f, %.1f", point.x, point.y);
} void MyScene::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, , true);
}

运行结果:

cocos2d-x学习记录3——CCTouch触摸响应

在TouchStart中,判断按下的点是否在图片显示区域内。如果在,则打出log,并返回true,然后CCTouchMove、CCTouchEnded等事件才会被响应;否则,不会响应。

Touches标准型。多点触摸时,会将所有的触摸点放进一个CCSet中。

 void MyScene::registerWithTouchDispatcher()
{
//CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCLayer::registerWithTouchDispatcher();
} void MyScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
if( pTouches->count() == ){
CCTouch *touch = (CCTouch*)(*( pTouches->begin() )); CCPoint point = touch->getLocationInView();
point = CCDirector::sharedDirector()->convertToGL(point);
CCNode *node = getChildByTag();
float x = node->getPositionX();
float y = node->getPositionY();
float width = node->getContentSize().width*node->getScale();
float height = node->getContentSize().height*node->getScale();
CCRect rect = CCRectMake(x-width/, y-height/, width, height); //CCLog("Touches start! %.1f,%.1f", point.x, point.y); if( rect.containsPoint(point) ){
CCLog("Touches start! %.1f,%.1f", point.x, point.y);
}
} //return false;
} void MyScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
if( pTouches->count() == ){
CCTouch *touch = (CCTouch*)(*( pTouches->begin() ));
CCPoint point = touch->getLocationInView();
CCLog("Touches moved! %.1f,%.1f", point.x, point.y);
}
} void MyScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
if( pTouches->count() == ){
CCTouch *touch = (CCTouch*)(*( pTouches->begin() ));
CCPoint point = touch->getLocationInView();
CCLog("Touches ended! %.1f,%.1f", point.x, point.y);
}
} void MyScene::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
if( pTouches->count() == ){
CCTouch *touch = (CCTouch*)(*( pTouches->begin() ));
CCPoint point = touch->getLocationInView();
CCLog("Touches canceled! %.1f,%.1f", point.x, point.y);
}
}

和标准CCTouch基本相同,只是在注册时,调用一次CCLayer的触摸注册函数即可。

运行结果:

cocos2d-x学习记录3——CCTouch触摸响应