Layer其实继承了触控的接口. 所以只需要重写一些函数即可.
 
在helloword类中重写:
    virtual bool init();
    /** Callback function for touch began.
    *
    * @param touch Touch information.
    * @param unused_event Event information.
    * @return if return false, onTouchMoved, onTouchEnded, onTouchCancelled will never called.
    * @js NA
    */
    virtual bool onTouchBegan(Touch *touch, Event *unused_event);
    /** Callback function for touch moved.
    *
    * @param touch Touch information.
    * @param unused_event Event information.
    * @js NA
    */
    virtual void onTouchMoved(Touch *touch, Event *unused_event);
    /** Callback function for touch ended.
    *
    * @param touch Touch information.
    * @param unused_event Event information.
    * @js NA
    */
    virtual void onTouchEnded(Touch *touch, Event *unused_event);
    /** Callback function for touch cancelled.
    *
    * @param touch Touch information.
    * @param unused_event Event information.
    * @js NA
    */
    virtual void onTouchCancelled(Touch *touch, Event *unused_event);
 
 
此时,只要这个layer在界面上,点击就会在这几个函数中响应. 即可在这里面控制精灵的移动
 
 
 但是编译错误.
2>f:\cocos\p\demo\classes\gamescene.h(17): error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(24): error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(31): error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(38): error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(49): error C2143: syntax error : missing ';' before '*' (..\Classes\HelloWorldScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\HelloWorldScene.cpp)
2>f:\cocos\p\demo\classes\helloworldscene.cpp(105): warning C4996: 'cocos2d::CCString': was declared deprecated
2>          f:\cocos\p\demo\cocos2d\cocos\deprecated\ccdeprecated.h(1108) : see declaration of 'cocos2d::CCString'
2>f:\cocos\p\demo\classes\gamescene.h(17): error C2061: syntax error : identifier 'Touch' (..\Classes\GameScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(24): error C2061: syntax error : identifier 'Touch' (..\Classes\GameScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(31): error C2061: syntax error : identifier 'Touch' (..\Classes\GameScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(38): error C2061: syntax error : identifier 'Touch' (..\Classes\GameScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(49): error C2143: syntax error : missing ';' before '*' (..\Classes\GameScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\GameScene.cpp)
2>f:\cocos\p\demo\classes\gamescene.cpp(42): error C2065: 'zxsprite' : undeclared identifier
2>f:\cocos\p\demo\classes\gamescene.cpp(43): error C2065: 'zxsprite' : undeclared identifier
2>f:\cocos\p\demo\classes\gamescene.cpp(43): error C2227: left of '->setScale' must point to class/struct/union/generic type
2>          type is 'unknown-type'
2>f:\cocos\p\demo\classes\gamescene.cpp(44): error C2065: 'zxsprite' : undeclared identifier
2>f:\cocos\p\demo\classes\gamescene.cpp(44): error C2227: left of '->setPosition' must point to class/struct/union/generic type
2>          type is 'unknown-type'
2>f:\cocos\p\demo\classes\gamescene.cpp(45): error C2065: 'zxsprite' : undeclared identifier
2>f:\cocos\p\demo\classes\gamescene.cpp(49): warning C4996: 'cocos2d::Layer::setTouchMode': was declared deprecated
2>          f:\cocos\p\demo\cocos2d\cocos\2d\cclayer.h(181) : see declaration of 'cocos2d::Layer::setTouchMode'
2>f:\cocos\p\demo\classes\gamescene.cpp(56): error C2511: 'bool GameScene::onTouchBegan(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
2>          f:\cocos\p\demo\classes\gamescene.h(4) : see declaration of 'GameScene'
2>f:\cocos\p\demo\classes\gamescene.cpp(57): warning C4996: 'cocos2d::CCPoint': was declared deprecated
2>          f:\cocos\p\demo\cocos2d\cocos\deprecated\ccdeprecated.h(833) : see declaration of 'cocos2d::CCPoint'
2>f:\cocos\p\demo\classes\gamescene.cpp(58): error C2065: 'zxsprite' : undeclared identifier
2>f:\cocos\p\demo\classes\gamescene.cpp(58): error C2227: left of '->setPosition' must point to class/struct/union/generic type
2>          type is 'unknown-type'
2>f:\cocos\p\demo\classes\gamescene.cpp(64): error C2511: 'void GameScene::onTouchMoved(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
2>          f:\cocos\p\demo\classes\gamescene.h(4) : see declaration of 'GameScene'
2>f:\cocos\p\demo\classes\gamescene.cpp(70): error C2511: 'void GameScene::onTouchEnded(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
2>          f:\cocos\p\demo\classes\gamescene.h(4) : see declaration of 'GameScene'
2>f:\cocos\p\demo\classes\gamescene.cpp(75): error C2511: 'void GameScene::onTouchCancelled(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'

2>          f:\cocos\p\demo\classes\gamescene.h(4) : see declaration of 'GameScene'

 
解决办法:
在cocos2d.h包含路径下添加USING_NS_CC即可.
#include "cocos2d.h"

USING_NS_CC;

 
 
 
在<权威指南>中关于注册触摸响应的方法在3.x中已经不再支持.
在权威指南中是需要重载registerWithTouchDispatcher()函数进行操作的.但我当前使用的3.12版本不再支持.
因为:  在Layer.h源码中有 final 关键字. 所以我认为应该就是不再使用.(不知道是不是这么理解)
    /** If isTouchEnabled, this method is called onEnter. Override it to change the
    way Layer receives touch events.
    ( Default: TouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); )
    Example:
    void Layer::registerWithTouchDispatcher()
    {
    TouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,INT_MIN+1,true);
    }
    @since v0.8.0
    @js NA
    */

CC_DEPRECATED_ATTRIBUTE virtual void registerWithTouchDispatcher() final {};

 
 
 
 
 
 
 
 
 
 
 
 
 

cocos2dx触摸响应的更多相关文章

  1. cocos2d-x触摸事件优先级的探究与实践

    如何让自定义Layer触发触摸事件? bool LayerXXX::init() { this->setTouchEnabled(true); CCTouchDispatcher* td = C ...

  2. cocos2d-x触摸事件优先级

     CCTouchDispatcher是管理cocos2d-x中全部Touch事件派发的类, CCTouchDispatcher中包括了两个CCTouchHandler的列表, 分别存储Standa ...

  3. React-native 中的触摸响应功能

    我们在做APP的时候,与桌面应用系统不同的是触摸响应. web页面对触摸响应的支持和原生的APP有着很大的差异. 基本用法 componentWillMount: function() { this. ...

  4. 关于cocostudio动态添加控件触摸响应无效的学习

    time:2015/04/19 1. 描述 * 把studio制作的ui加载之后,动态添加事件(比如说,单点触摸),结果回调函数(eg:onTouchBegan等)根本没有响应! * 另外,网上有朋友 ...

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

    游戏不同于影音,强交互性是其一大特色,在游戏中主要体现为接受用户的输入并响应.智能手机触摸是其重要的输入方式. 在cocos2d-x中,触摸分为单点触摸和多点触摸. 单点触摸:主要继承CCTarget ...

  6. cocos2dx游戏--欢欢英雄传说--添加触摸响应

    主要的调整就是将HelloWorldScene改成了MainSecne,然后将Player作为了MainScene的私有成员变量来处理.修改了人物图片,使用了网上找到的三国战纪的人物素材代替我之前画的 ...

  7. cocos2d-x触摸分发器原理

    屏幕捕捉到触摸消息的派发流程: 如果有一个组件如果想要接收触摸事件,会通过继承一个CCTouchDelegate接口注册给CCTouchDispatcher,CCTouchDispatcher 中维护 ...

  8. cocos2dx 触摸钢琴

    1.触摸钢琴项目描写叙述 1.1触摸钢琴功能描写叙述 实现手指点按琴键发出相应的音调,按下位置出现星云的粒子特效,滚动实现移动到别的琴键的位置,按下安卓返回键运行关闭. 1.2触摸钢琴所需技术 粒子特 ...

  9. 3.x的触摸响应机制

    第一种是采用函数回调,主要是用于MenuItem [cpp] view plaincopy // a selector callback void menuCloseCallback(Object*  ...

随机推荐

  1. javascript基础总结之实例(一)

    样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  2. [JZOJ 5860] 荒诞

    思路: 头皮发麻的操作... 理解一下题意会发现:排名为\(i\)的前缀正好是第\(i\)个前缀. 所以问题就变成了求\(1->len\)的平方和,注意取模即可. #include <bi ...

  3. [JZOJ 100025] 棋盘

    题意:求剩余面积. 首先吐槽题号:究竟\(JZOJ\)有多少未公开的题目... 思路: 简单的一批啊... 不知道为啥上午不过下午就过了?? 难道是海螺姑娘光顾我?? 多说了都是灵异故事... 其实就 ...

  4. Go的异常处理 defer, panic, recover

    Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常, ...

  5. git学习记录2(远程库管理)

    学习参考地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 本编随笔只是自己对 ...

  6. Codeforces 1159A A pile of stones

    题目链接:http://codeforces.com/problemset/problem/1159/A 题意:初始石头堆石子数未知,求进行 n 次加减操作后,石头堆石子数最小的可能是多少. 思路:正 ...

  7. Linux下rsync的安装及简单使用

    2018-09-25 15:39:04 一.RSYNC安装环境: centos6.5 iptables关闭和selinux为disabled 源码安装:到rsync官网下载rsync源码安装包,上传到 ...

  8. MyBatis mappers元素标签及其属性、配置

    mappers:映射器,以最佳的方式是告诉 MyBatis 到哪里去找映射文件. <!-- 使用相对于类路径的资源引用,要满足一个条件:1.即映射文件只要放在类路径下,就可以根据相对路径找到,放 ...

  9. liunx crontab 参数代表含义

    * * * * * (下面的字体对应) 分钟 小时 几号 月份 星期几 星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作. 逗号(,): ...

  10. Xtrabackup 热备

    Xtrabackup介绍Xtrabackup是由percona开源的免费数据库热备份软件,它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁):m ...