gpt4 book ai didi

c++ - onTouchBegan 不工作 cocos2dx

转载 作者:行者123 更新时间:2023-11-28 05:05:16 25 4
gpt4 key购买 nike

试着用 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 将显示消息,但似乎在日志窗口中也没有看到任何消息logLabel 也没有改变。

我错过了什么?

注意事项

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

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

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

最佳答案

你需要添加/注册你的事件监听器到事件调度器:

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

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

    addEventListenerWithFixedPriority(EventListener* listener, Node* node)

关于c++ - onTouchBegan 不工作 cocos2dx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com