我正在使用Cocos2d-x并尝试检测我的HelloWorld项目中的触摸。虽然我没有运气。
·H
class HelloWorld : public CCLayer{
private:
CCSpriteBatchNode * _batchNode;
CCSprite *_turkey;
virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
.ccp
void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
CCLog("this");
}
但问题是,当我点击屏幕时,“这个”永远不会出现在日志中。我在这里缺少什么?
谢谢!
编辑,
我正在使用本教程。 http://www.raywenderlich.com/11338/cocos2d-x-for-ios-and-android-space-game
答案 0 :(得分:20)
您必须注册CCTouchDispatcher才能接收触摸:
将此内容写入init()
方法,以便接收内容:
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, 0);
我还建议您通过有针对性的触摸委托方法接收触摸事件:
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
为了调用这些方法,您必须向触摸调度程序注册一点不同:
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
修改强>
在新的cocos版本中,CCTouchDispatcher
位于CCDirector
:
看起来应该是这样的:
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
答案 1 :(得分:7)
所以非常简单,只需添加
this->setIsTouchEnabled(true);
到我的init();功能
答案 2 :(得分:2)
'this' never shows up in the log
提示您可能正在使用不同版本的Cocos2D库。请转到项目的cocos2d.h
并确认。 (样本写在1.0.1上)。如果您使用的是其他版本(猜测),则可能需要使用不同的ccTouchesBegan
签名和/或修复setIsTouchEnabled
以外的其他功能才能使其正常工作。我刚刚下载了示例,ccTouchesBegan
调用完美无缺,无需任何更改。
答案 3 :(得分:0)
在下面的方法中,我在Sprite上应用触摸,如果你想在TextField,Node,Background或任何组件中应用触摸事件,只需将ComponentType传递给这个方法,它就能工作....
好的,请开始!!!!
void YourClassName::YourListnerMethodName(cocos2d::Sprite* object)
{
auto listener = cocos2d::EventListenerTouchOneByOne::create();
listener->setSwallowTouches(false);
listener->onTouchBegan = [=](cocos2d::Touch* touch, cocos2d::Event* event)
{
auto target = event->getCurrentTarget();
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
// Suppose your sprite or any component is inside in any parent object then use this line instead of above line ...
//just uncomment below line and it will work fine in this case
//Point locationInNode = target->getParent()->convertToNodeSpace(touch->getLocation());
if (target->getBoundingBox().containsPoint(locationInNode)) {
// CODE FOR RESPONSE AFTER TOUCH
return true;
}
return false;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, object);
}
此处目标是您要在其上应用触控的组件
不要忘记根据您的要求从ctor或任何地方调用此方法
答案 4 :(得分:-1)
this->setTouchEnabled(true);
效果比CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
更好,不幸的是我的ccTouchMoved没有采摘任何东西...... :(
答案 5 :(得分:-1)
for cocos2d-x v3.0 ..
在你的' .h'中写下这个。文件
{bool onTouchBegan (cocos2d::Touch * touch, cocos2d::Event * event);}
将此内容写入您的init()' function
..
{
auto listner = EventListenerTouchOneByOne::create();
listner->setSwallowTouches(true);
listner->onTouchBegan = CC_CALLBACK_2(Gameplay::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listner, this);
}
并在' .cpp'中写下来文件..
bool "YOURCLASSNAME"::onTouchBegan(cocos2d::Touch* touch, cocos2dEvent* event)
{
CCLOG("this");
return true;
}