Cocos2d-x屏幕点击事件

我按官方文档上介绍的方法写的,代码如下:1//cppwithcocos2d-x2this->setTouchEnabled(true);orthis->setIsTouc... 我按官方文档上介绍的方法写的,代码如下:
1// cpp with cocos2d-x
2this->setTouchEnabled(true); or this->setIsTouchEnabled(true);
1// cpp with cocos2d-x
2void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
3{
4 // Choose one of the touches to work with
5 CCTouch* touch = (CCTouch*)( touches->anyObject() );
6 CCPoint location = touch->locationInView();
7 location = CCDirector::sharedDirector()->convertToGL(location);
8
9 // Set up initial location of projectile
10 CCSize winSize = CCDirector::sharedDirector()->getWinSize();
11 CCSprite *projectile = CCSprite::create("Projectile.png",
12 CCRectMake(0, 0, 20, 20));
13 projectile->setPosition( ccp(20, winSize.height/2) );
14
15 // Determinie offset of location to projectile
16 int offX = location.x - projectile->getPosition().x;
17 int offY = location.y - projectile->getPosition().y;
18
19 // Bail out if we are shooting down or backwards
20 if (offX <= 0) return;
21
22 // Ok to add now - we've double checked position
23 this->addChild(projectile);
24
25 // Determine where we wish to shoot the projectile to
26 int realX = winSize.width
27 + (projectile->getContentSize().width/2);
28 float ratio = (float)offY / (float)offX;
29 int realY = (realX * ratio) + projectile->getPosition().y;
30 CCPoint realDest = ccp(realX, realY);
31
32 // Determine the length of how far we're shooting
33 int offRealX = realX - projectile->getPosition().x;
34 int offRealY = realY - projectile->getPosition().y;
35 float length = sqrtf((offRealX * offRealX)
36 + (offRealY*offRealY));
37 float velocity = 480/1; // 480pixels/1sec
38 float realMoveDuration = length/velocity;
39
40 // Move projectile to actual endpoint
41 projectile->runAction( CCSequence::create(
42 CCMoveTo::create(realMoveDuration, realDest),
43 CCCallFuncN::create(this,
44
45 callfuncN_selector(HelloWorld::spriteMoveFinished)),
46 NULL) );
47}

没有错误,可以运行,可是点击屏幕始终没有反应,这是什么原因啦,是不是还掉了其他的声明啊,求解!!(我用的是2.12版本)
展开
 我来答
tq0cdxa
推荐于2016-01-11 · TA获得超过123个赞
知道小有建树答主
回答量:210
采纳率:0%
帮助的人:214万
展开全部

你需要重载virtual void registerWithTouchDispatcher();函数

实现

Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this, Menu::HANDLER_PRIORITY - 1, true);

我这是3.0的

2.x是

CCDirector* pDirector = CCDirector::sharedDirector();

    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
追问
在哪里重载啊,init()里面吗,需不需要重载ccTouchBegan()函数,还是新手,麻烦详细一些,谢谢啦
追答
你直接下到H头文件了 实现大CPP里
示例文件里就有
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式