onTouchBegan 不工作 cocos2dx

onTouchBegan not working cocos2dx

试着用 cpp 玩 cocos2dx:

这是头文件:

#ifndef FirstScene_h
#define FirstScene_h

#include "cocos2d.h"
class FirstScene: public cocos2d::Scene
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    void menuCloseCallback(cocos2d::Ref* pSender);
    CREATE_FUNC(FirstScene);

    bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
    bool onTouchMoved(cocos2d::Touch *touch, cocos2d::Event * event);
    bool onTouchEnd(cocos2d::Touch *touch, cocos2d::Event *event);

private:
    cocos2d::Label *logLabel;  
};
#endif /* FirstScene_h */

这是 cpp 文件:

#include "FirstScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
Scene* FirstScene::createScene(){
    return FirstScene::create();
}

bool FirstScene::init(){
    if(!Scene::init()){
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto label = Label::createWithTTF("this is the first scene", "fonts/Marker Felt.ttf", 24);
    label->setPosition(visibleSize.width/2,visibleSize.height/2);

    this->addChild(label,1);

    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(visibleSize.width/3,visibleSize.height/3);

    logLabel = Label::createWithTTF("Log holder", "fonts/Marker Felt.ttf", 24);

    logLabel->setPosition(visibleSize.width/2 + 10.0f, visibleSize.height/2 + 10.0f);
    this->addChild(logLabel,3);

    this->addChild(sprite,0);

    auto listener = EventListenerTouchOneByOne::create();

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

    return true;
}

bool FirstScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
    CCLOG("touch at x=%f, y=%f", touch->getLocation().x, touch->getLocation().y);
    std::string s = "";
    s += "touch at x=";
    s += touch->getLocation().x;
    s += "y=";
    s += touch->getLocation().y;
    logLabel->setString(s);
    return true;
}

bool FirstScene::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) {

    std::string s = "";
    s += "touch moved at x=";
    s += touch->getDelta().x;
    s += " y=";
    s += touch->getDelta().y;
    CCLOG("touch moved at x=%f, y=%f", touch->getDelta().x, touch->getDelta().y);
    logLabel->setString(s);
    return true;
}

bool FirstScene::onTouchEnd(cocos2d::Touch *touch, cocos2d::Event *event) {
    std::string s="";
    s += "touch ended at x=";
    s += touch->getLocation().x;
    s += " y=";
    s += touch->getLocation().y;


    CCLOG("touch ended at x=%f, y=%f", touch->getLocation().x, touch->getLocation().y);
    logLabel->setString(s);
    return true;
}

我想要的是当我在屏幕上触摸和移动时,会显示日志并且 logLabel 会显示消息,但似乎在日志中也没有看到任何消息 window logLabel 也没有改变。

我错过了什么?

备注

如果重要的话,我会通过以下代码通过之前的一个场景加载这个场景:

void HelloWorld::menuCloseCallback(Ref* pSender)
{ 
    auto firstScene = FirstScene::createScene();
    Director::getInstance()->replaceScene(firstScene);
}

真是cocos2dx和cpp的新手,在此先感谢。

你需要add/register你的事件监听器到事件调度器:

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
  • 如果要为场景图优先级的指定事件添加事件监听:

    addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
    
  • 否则想为具有固定优先级的指定事件添加事件侦听器。

    addEventListenerWithFixedPriority(EventListener* listener, Node* node)