单击不起作用
Click does not work
我的错是什么?HelloWorldScene.h 中有一种方法
void onTouchEnded(Touch *touch,Event *event);
HelloWorldScene.cpp文件我在Init()方法中写道
this->setTouchEnabled(true);
下面是方法的描述
void HelloWorld::onTouchEnded(Touch *touch,Event *event)
{
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
location = this->convertToNodeSpace(location);
CCLog("x = %f n y = %f n ---------------------",location.x,location.y);
}
但是点击Windows失败
触摸事件有两种——TargetedDelegate
和StandardDelegate
对于TargetedDelegate
virtual void registerWithTouchDispatcher();
virtual bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved (CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded (CCTouch *pTouch, CCEvent *pEvent);
在您的init()中:
this->setTouchEnabled(true);
并重写registerWithTouchDispatcher()
void Layer::registerWithTouchDispatcher(){
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}
对于StandardDelegate
virtual void ccTouchesBegan (CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved (CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded (CCSet *pTouches, CCEvent *pEvent);
以及在您的init()中:
this->setTouchEnabled(true);
如果您在此处重写registerWithTouchDispatcher()
。您应该在init()
中调用addStandardDelegate()
。
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
OnTouchEnded是父类虚拟函数你需要覆盖它函数声明,如:
virtual void onTouchEnded(Touch *touch,Event *event);
您可以应用它。
HelloWorld.h
虚拟void registerWithTouchDispatcher(void);
虚拟void ccTouchesBegan(CCSet*pTouches,CCEvent*pEvent);
HelloWorld.cpp
HelloWorld::HelloWorld()
{
this->setTouchEnabled(true);
}
void HelloWorld::registerWithTouchDispatcher(void)
{
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
}
void HelloWorld:ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCTouch *touch = (CCTouch *) pTouches->anyObject();
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
//这是为了检查精灵上的触摸事件
if(spriteName->boundingBox().containsPoint(location))
CCLog("Sprite Hit Test");
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- C++鼠标单击时的屏幕截图不起作用
- C++:矩阵高斯消除不起作用:使用单维数组来存储元素
- SDL2 + Win32 API 菜单栏单击事件不起作用
- 点击回车按钮后不起作用
- 自动点击器在游戏中 - 为什么不起作用?
- POCO C 简单表单提交样本不起作用
- Qt C++ QLabel 可点击鼠标事件不起作用
- 单击不起作用
- 将消息发布到隐藏表单第一次不起作用
- 为什么这种单例模式不起作用?
- 单例中的构造函数重载不起作用
- 如果通过单击任务栏按钮最小化程序,SW_SHOWNOACTIVATE不起作用
- 键盘输入不起作用,单击鼠标时工作
- 在鼠标移动/单击之前,SetCursor 不起作用 (Win32 API)