onTouchBegan不工作cocos2dx

时间:2017-07-07 17:40:45

标签: c++ events cocos2d-x

尝试使用cpp:

来使用cocos2dx

这是Header文件:

#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将显示该消息,但似乎在日志窗口和logLabel中都没有看到消息改变。

我错过了什么?

注意

如果重要的话,我按照以下代码加载前一个场景:

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

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

1 个答案:

答案 0 :(得分:1)

您需要向事件调度程序添加/注册事件侦听器:

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

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

    addEventListenerWithFixedPriority(EventListener* listener, Node* node)