在多平台cocos2d-x应用程序中无法触摸工作 [英] Can't get touch to work in multi-platform cocos2d-x app

查看:172
本文介绍了在多平台cocos2d-x应用程序中无法触摸工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图创建一个简单的应用程序使用cocos2d-x最新的构建,由于某些原因不能让我的触摸连接。这是我的课程:

So I'm trying to create a simple app using cocos2d-x newest build and for some reason can't get my touch wired up. Here are my classes:

class GameLayer : public cocos2d::Layer
{
public:
    static cocos2d::Layer* createLayer();
    void update(float dt);
    virtual bool init();
    CREATE_FUNC(GameLayer);
private:
    bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
    void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
    void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
};

cocos2d::Layer* GameLayer::createLayer()
{
    GameLayer *layer = GameLayer::create();

    return layer;
}

bool GameLayer::init()
{
    if (!cocos2d::Layer::init())
    {
        return false;
    }

    this->schedule(schedule_selector(GameLayer::update));
    this->setTouchEnabled(true);

    return true;
}

void GameLayer::update(float dt)
{

}

bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
    cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);
    return true;
}
void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)
{

}
void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{

}

我注意到当我调用 setTouchEnabled 调用一个内部标志 _running 设置为false,因此它实际上不会注册我的触摸事件。然而,我似乎不知道为什么是这样的情况。

I noticed when I call into the setTouchEnabled call that an internal flag called _running is set to false so it doesn't actually register my touch events. However I can't seem to figure out why that is the case. Am I calling things incorrectly or in the wrong order?

推荐答案

目前,cocos2dx正在对图书馆进行重大翻修,许多事情已经改变,包括触摸注册和传播。以下是现在的工作原理:

Currently, cocos2dx is going through a major overhauling of the library and many things have changed including Touch registration and propagation. Here is how it works now:

GameLayer.h

class GameLayer : public cocos2d::Layer
{
public:
    static cocos2d::Layer* createLayer();
    void update(float dt);
    virtual bool init();
    CREATE_FUNC(GameLayer);

private:
    virtual void onEnter();
    virtual void onExit();

    bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
    void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
    void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
};

GameLayer.cpp

cocos2d::Layer* GameLayer::createLayer()
{
    GameLayer *layer = GameLayer::create();

    return layer;
}

bool GameLayer::init()
{
    if (!cocos2d::Layer::init())
    {
        return false;
    }

    this->schedule(schedule_selector(GameLayer::update));

    return true;
}

void GameLayer::onEnter()
{
    Layer::onEnter();

    // Register Touch Event
    auto dispatcher = Director::getInstance()->getEventDispatcher();
    auto listener = EventListenerTouchOneByOne::create();

    listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this);

    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void GameLayer::onExit()
{
    // You don't need to unregister listeners here as new API
    // removes all linked listeners automatically in CCNode's destructor
    // which is the base class for all cocos2d DRAWING classes

    Layer::onExit();
}

void GameLayer::update(float dt)
{

}

bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
    cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);
    return true;
}

void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)
{

}

void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{

}

希望它有帮助!

这篇关于在多平台cocos2d-x应用程序中无法触摸工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆