zoukankan      html  css  js  c++  java
  • cocos2d-x step by step(2) 鼠标事件,键盘事件,等等事件

    各种小控件加载进去了,那么问题来了,这些东西如何接受事件呢?

    good job,let us find the answer

    首先我们去看文档,官方尼玛有好多文档,而且大,全,详细,感觉还是不错的

    http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/event-dispatcher/zh.md

    喏,这个是事件的连接

    这里我不想copy人家代码,什么的,没意思,我只说一下我迁移时候遇到的问题。

    笔者c++都丢给体育老师了,一些基本东西都忘记了。

    #include "cocos2d.h"
    #include "ui/UIWidget.h"
    #include "base/CCEventKeyboard.h"
    using namespace cocos2d;
    class FishTestScence :public cocos2d::Scene
    {
    public:
        virtual bool init();
    
        static cocos2d::Scene* scene();
    
        void menuCloseCallback(Ref* sender);
    
        void onButtonClicked(Ref *pSender,ui::Widget::TouchEventType type);
        void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
        void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
        CREATE_FUNC(FishTestScence);
    };
    #endif

    我是拿键盘事件作为测试的,首先你要在头文件声明这两个函数,这个是官方提供的,应该不会有多大出入

        void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
        void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
    那么问题来了,我引用了
    #include "base/CCEventKeyboard.h"
    这个头文件
    然后我编译时候宁死过不了,我问了一下同事,c++不像c#哪样,你需要完整的路径,也就是说
    cocos2d::ventKeyboard::KeyCode
    或者加上
    using namespace cocos2d;
    这样才不报错

    在cpp文件内:
    bool FishTestScence::init()
    {
        if (!Scene::init())
        {
            return false;
        }
    
        auto visibleSize = Director::getInstance()->getVisibleSize();
        auto origin = Director::getInstance()->getVisibleOrigin();
    
    
        auto button = ui::Button::create("dangban.png", "ball.png");
    
        button->setPosition(Vec2(visibleSize / 2) + origin);
        button->setPressedActionEnabled(true);
        button->setTitleText("lou zhu sb");
    
        button->addTouchEventListener(CC_CALLBACK_2(FishTestScence::onButtonClicked, this));
        this->addChild(button);
    
        auto listener = EventListenerKeyboard::create();
        listener->onKeyPressed = CC_CALLBACK_2(FishTestScence::onKeyPressed, this);
        listener->onKeyReleased = CC_CALLBACK_2(FishTestScence::onKeyReleased, this);
    
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    
    
        return true;
    }
    
    
    // 键位响应函数原型
    void FishTestScence::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
    {
    
    }
    
    void FishTestScence::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
    {
    
    }
    
    void FishTestScence::onButtonClicked(Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
    {
    
    }

    加上断点,就能够接收到事件了。

    感觉其实不难,但是有时候有些坑如果卡着就悲剧了

    共勉之。

  • 相关阅读:
    【论文阅读笔记】《StarGAN》
    【数据分析入门】泰坦尼克号生存率预测(一)
    【DLPytorch】Optimizer(一)
    【论文阅读笔记】《Pix2Pix》
    【论文阅读笔记】《DCGAN》
    【论文阅读笔记】《Conditional Generative Adversarial Nets》
    【论文阅读笔记】Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks
    【Datawhale】Python从入门到放弃
    【数字图像处理】 直方图的均衡与规定化
    GAN学习入门篇(一)
  • 原文地址:https://www.cnblogs.com/fish124423/p/4390018.html
Copyright © 2011-2022 走看看