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

在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);
}

运行结果:

在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的触摸注册函数即可。

运行结果:

最新文章

  1. angular-route 和soket注意细节点
  2. activity与fragment之间传递数据
  3. Unity3d《Shader篇》描边
  4. LightOj 1213 - Fantasy of a Summation(推公式 快速幂)
  5. 在腾讯云上创建您的SQL Cluster(4)
  6. 【转】Javascript+css 实现网页换肤功能
  7. relative和absolute的效果
  8. android的项目文件介绍
  9. Android 使用HttpClient方式提交POST请求
  10. Mysql彻底卸载
  11. linux目录跳转快捷方式——z武器
  12. 微信小程序本地引用iconfont(阿里巴巴矢量图标库)
  13. 第一阶段——站立会议总结DAY08
  14. 案例情景--在一次Oracle 数据库导出时 EXP-00008;ORA-00904:EXP-00000: oracle不同版本导入导出规则
  15. 【刷题】BZOJ 4078 [Wf2014]Metal Processing Plant
  16. 论Java的ArrayList.add(e)和C++的vector.push_back(e)的区别
  17. Ubuntu 安装 Zabbix 3.2详细步骤
  18. Ubuntu14.04下Cloudera安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)(在线或离线)
  19. Chromebook 阿里云ECS 配置 jupyter Notebook
  20. 稀疏编码直方图----一种超越HOG的轮廓特征

热门文章

  1. C# socket 发送图片和文件
  2. JQUERY中find方法
  3. android kl文件
  4. Huawei DHCP 全局配置与接口配置
  5. mysql host'XXX' is not allowed to connect to this mysql server
  6. Vue图片懒加载
  7. Breaking Down Type Erasure in Swift
  8. 基于汇编的 C/C++ 协程 - 切换上下文
  9. php实现链表的基本操作
  10. linux 的常用命令---------第三阶段