无法在多平台 cocos2d-x 应用程序中工作
Can't get touch to work in multi-platform cocos2d-x app
所以我正在尝试使用 cocos2d-x 最新版本创建一个简单的应用程序,但由于某种原因无法连接我的触摸。 以下是我的课程:
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,因此它实际上并没有注册我的触摸事件。 但是,我似乎无法弄清楚为什么会这样。 我是否调用错误或顺序错误?
目前,cocos2dx正在对库进行重大改革,许多事情都发生了变化,包括触摸注册和传播。这是它现在的工作原理:
游戏层.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);
};
游戏层.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)
{
}
希望对您有所帮助!
相关文章:
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- .exe应用程序在windows10中创建新模块时抛出错误,但在windows7中工作正常
- OpenGL应用程序在不同的计算机上的工作方式不同
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- Visual Studio 中的套接字 c++ 应用程序无法通过两台计算机之间的以太网连接工作
- 如何将子窗口(MFC 应用程序)的工作区保存到图像
- 为什么我需要运行一个应用程序作为Papi库工作的根源
- 从 WebAPI 项目运行静态类时出现 StackOverflow 异常 - 从控制台应用程序运行时工作正常
- 当我跳到应用程序时,中断无法工作(STM32)
- 如何使用工作线程正确退出Qt应用程序
- 运行在DLL中的工作线程在应用程序关闭时被终止,然后才能正常关闭它们
- 套接字应用程序无法正常工作
- C++使用Qt库的应用程序在屏幕保护程序启动时停止工作
- 在我的Win32应用程序中使TAB键起工作
- 什么可能导致应用程序在工作几个小时后锁定获取 SIGABRT 失败
- 如何使用特定的工作目录运行/调试Netbeans平台应用程序
- 绘制移动图标,它一直是最顶端的鼠标光标和工作的全屏应用程序
- SQL SP在SSMS中正常工作,但在VC++应用程序中失败
- boost::asio 中的未经请求的消息使应用程序崩溃,没有 SSL 它可以正常工作,为什么
- MacOSX C++带有命令行应用程序的工作目录