Now, we want to let the hero fire some bullets to kill the enemies, add the codes below to set the layer touch-enabled.

1// cpp with cocos2d-x
2this->setTouchEnabled(true); or this->setIsTouchEnabled(true);

Then we could receive the touch event now.
Declare the callback function "void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);" in HelloWorldScene.h, and implement the function in HelloWorldScene.cpp.

 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}

Ok, build and run, touch the screen(on the emulator? click the screen!), and enjoy the effect.
PS: To keep identical with the Object-C codes, there would be some warnings of conversion from 'float' to 'int', don't care about them.

Win32

Chapter 4 - How to Fire some Bullets的更多相关文章

  1. Chapter 5 - How to Detect the Collisions

    Chapter 5 - How to Detect the Collisions Our hero can fire bullets now, but the bullets are only vis ...

  2. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. HDU1045 Fire Net(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  4. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  5. Fire Net

    Fire Net Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  6. JavaScript- The Good Parts Chapter 5 Inheritance

    Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...

  7. HDU-1045 Fire Net

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Me ...

  8. Chapter 6 - How to Play Music and Sound Effect

    In this chapter, we would add background music to the game and play sound effect when the hero fires ...

  9. hdoj 1045 Fire Net

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. bzoj 1138: [POI2009]Baj 最短回文路 dp优化

    1138: [POI2009]Baj 最短回文路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 161  Solved: 48[Submit][Sta ...

  2. the apply of backbone

    http://www.developer.com/print/lang/jscript/creating-a-javascript-driven-online-notebook-with-backbo ...

  3. OpenIOC

    http://wenku.it168.com/d_926300.shtml OpenIOC http://safe.it168.com/a2015/1208/1790/000001790446.sht ...

  4. 【POJ3169 】Layout (认真的做差分约束)

    Layout   Description Like everyone else, cows like to stand close to their friends when queuing for ...

  5. 再探CRC

    之前写了CRC16的程序,虽说能用,却不知其所心然,现在要用CRC32,重温一遍,一下就通了.笔记如下 CRC我没记错的话是Cyclic Redundancy Code,Cyclic和Redundan ...

  6. IPV6 实现

    看代码实现前,请先保证了解ipv6的概念,可以先看ipv6介绍一文.code extract . 在文件 net/ipv6/af_inet6.c 中包含了ipv6协议初始化的主函数. static i ...

  7. bzoj1835

    最近状态太差,先补补结题报告吧这是一道好题设f[i,j]表示到第j个位置建了i个基站且第j个位置建了基站的最小花费不难得到f[i,j]=min(f[i-1,k]+cost[k+1,j])+c[j];首 ...

  8. Oracle坑之-空字符串与NULL

    空字符串与NULL 首先有如下代码 SELECT * FROM Pdc_DataDomain DD INNER JOIN Pdc_DD_Table DDT ON DD.DataDomainID = D ...

  9. WordPress Bradesco Gateway插件‘falha.php’跨站脚本漏洞

    漏洞名称: WordPress Bradesco Gateway插件‘falha.php’跨站脚本漏洞 CNNVD编号: CNNVD-201309-451 发布时间: 2013-09-26 更新时间: ...

  10. BZOJ2105: 增强型LCP

    2105: 增强型LCP Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 366  Solved: 86[Submit][Status] Descrip ...