单击不起作用

Click does not work

本文关键字:不起作用 单击      更新时间:2023-10-16

我的错是什么?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失败

触摸事件有两种——TargetedDelegateStandardDelegate

对于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");

}