点击不起作用

时间:2013-12-04 11:54:50

标签: c++ cocos2d-iphone cocos2d-x

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

3 个答案:

答案 0 :(得分:1)

有两种触摸事件---- TargetedDelegateStandardDelegate

For 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()。您应该在addStandardDelegate()。{/ p>中致电init()

CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);

答案 1 :(得分:0)

OnTouchEnded是父类虚函数 你需要覆盖它 功能声明如:

virtual void onTouchEnded(Touch *touch,Event *event);

答案 2 :(得分:0)

您可以申请。

HelloWorld.h

virtual void registerWithTouchDispatcher(void);

virtual void ccTouchesBegan(CCSet * pTouches,CCEvent * pEvent);

HelloWorld.cpp

的HelloWorld ::的HelloWorld()

{

这 - > setTouchEnabled(真);

}

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);

//这是用于检查Sprite上的TouchEvent

         if(spriteName->boundingBox().containsPoint(location))
                CCLog("Sprite Hit Test");


}