zoukankan      html  css  js  c++  java
  • cocos2dx触摸响应

     
    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:cocospdemoclassesgamescene.h(17): error C2061: syntax error : identifier 'Touch' (..ClassesHelloWorldScene.cpp)
    2>f:cocospdemoclassesgamescene.h(24): error C2061: syntax error : identifier 'Touch' (..ClassesHelloWorldScene.cpp)
    2>f:cocospdemoclassesgamescene.h(31): error C2061: syntax error : identifier 'Touch' (..ClassesHelloWorldScene.cpp)
    2>f:cocospdemoclassesgamescene.h(38): error C2061: syntax error : identifier 'Touch' (..ClassesHelloWorldScene.cpp)
    2>f:cocospdemoclassesgamescene.h(49): error C2143: syntax error : missing ';' before '*' (..ClassesHelloWorldScene.cpp)
    2>f:cocospdemoclassesgamescene.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..ClassesHelloWorldScene.cpp)
    2>f:cocospdemoclasseshelloworldscene.cpp(105): warning C4996: 'cocos2d::CCString': was declared deprecated
    2>          f:cocospdemococos2dcocosdeprecatedccdeprecated.h(1108) : see declaration of 'cocos2d::CCString'
    2>f:cocospdemoclassesgamescene.h(17): error C2061: syntax error : identifier 'Touch' (..ClassesGameScene.cpp)
    2>f:cocospdemoclassesgamescene.h(24): error C2061: syntax error : identifier 'Touch' (..ClassesGameScene.cpp)
    2>f:cocospdemoclassesgamescene.h(31): error C2061: syntax error : identifier 'Touch' (..ClassesGameScene.cpp)
    2>f:cocospdemoclassesgamescene.h(38): error C2061: syntax error : identifier 'Touch' (..ClassesGameScene.cpp)
    2>f:cocospdemoclassesgamescene.h(49): error C2143: syntax error : missing ';' before '*' (..ClassesGameScene.cpp)
    2>f:cocospdemoclassesgamescene.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..ClassesGameScene.cpp)
    2>f:cocospdemoclassesgamescene.cpp(42): error C2065: 'zxsprite' : undeclared identifier
    2>f:cocospdemoclassesgamescene.cpp(43): error C2065: 'zxsprite' : undeclared identifier
    2>f:cocospdemoclassesgamescene.cpp(43): error C2227: left of '->setScale' must point to class/struct/union/generic type
    2>          type is 'unknown-type'
    2>f:cocospdemoclassesgamescene.cpp(44): error C2065: 'zxsprite' : undeclared identifier
    2>f:cocospdemoclassesgamescene.cpp(44): error C2227: left of '->setPosition' must point to class/struct/union/generic type
    2>          type is 'unknown-type'
    2>f:cocospdemoclassesgamescene.cpp(45): error C2065: 'zxsprite' : undeclared identifier
    2>f:cocospdemoclassesgamescene.cpp(49): warning C4996: 'cocos2d::Layer::setTouchMode': was declared deprecated
    2>          f:cocospdemococos2dcocos2dcclayer.h(181) : see declaration of 'cocos2d::Layer::setTouchMode'
    2>f:cocospdemoclassesgamescene.cpp(56): error C2511: 'bool GameScene::onTouchBegan(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
    2>          f:cocospdemoclassesgamescene.h(4) : see declaration of 'GameScene'
    2>f:cocospdemoclassesgamescene.cpp(57): warning C4996: 'cocos2d::CCPoint': was declared deprecated
    2>          f:cocospdemococos2dcocosdeprecatedccdeprecated.h(833) : see declaration of 'cocos2d::CCPoint'
    2>f:cocospdemoclassesgamescene.cpp(58): error C2065: 'zxsprite' : undeclared identifier
    2>f:cocospdemoclassesgamescene.cpp(58): error C2227: left of '->setPosition' must point to class/struct/union/generic type
    2>          type is 'unknown-type'
    2>f:cocospdemoclassesgamescene.cpp(64): error C2511: 'void GameScene::onTouchMoved(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
    2>          f:cocospdemoclassesgamescene.h(4) : see declaration of 'GameScene'
    2>f:cocospdemoclassesgamescene.cpp(70): error C2511: 'void GameScene::onTouchEnded(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
    2>          f:cocospdemoclassesgamescene.h(4) : see declaration of 'GameScene'
    2>f:cocospdemoclassesgamescene.cpp(75): error C2511: 'void GameScene::onTouchCancelled(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'GameScene'
    2>          f:cocospdemoclassesgamescene.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 {};  
     
     
     
     
     
     
     
     
     
     
     
     
     





  • 相关阅读:
    个人管理:提高你的搜商
    敏捷个人:提供更多文档下载,并转载一篇敏捷个人读书笔记
    个人管理: 激励你的一句话
    敏捷个人 从Scrum实践来思考如何导入价值观
    信息系统开发平台OpenExpressApp 如何解决ComboBox.TextProperty绑定带来问题的来龙去脉
    敏捷个人 敏捷个人价值观,欢迎提出你的意见和你的价值观
    使用VS2010的CodedUI来做自己的自动化测试框架
    .Net4下的MEF(Managed Extensibility Framework) 架构简介
    IronRuby - 快速在半小时学习Ruby基础知识
    敏捷个人 项目网站文档页签增加blog链接
  • 原文地址:https://www.cnblogs.com/skyhuangdan/p/5761653.html
Copyright © 2011-2022 走看看