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. js获取域名的方法

    本文实例讲述了js获取域名的方法.分享给大家供大家参考.具体实现方法如下: 复制代码代码如下: <script>//获取域名var k_host = window.location.hos ...

  2. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  3. Stanford CoreNLP--Split Sentence

    分句功能参考 Stanford Tokenizer. 在edu.stanford.nlp.pipeline包中实现了一系列分词分句功能,其中SentenceAnnotator类实现了对文件分句功能. ...

  4. Druid :大数据实时处理的开源分布式系统(1)

    引言 Druid 是一个快速,近实时的查询海量只读数据的系统.Druid 的目标是可用性要达到100%,即使在部署新代码,或者某些节点 down 机的情况下. Druid 目前支持的单表查询方式和 D ...

  5. 【UVA 11865】 Stream My Contest (二分+MDST最小树形图)

    [题意] 你需要花费不超过cost元来搭建一个比赛网络.网络中有n台机器,编号0~n-1,其中机器0为服务器,其他机器为客户机.一共有m条可以使用的网线,其中第i条网线的发送端是机器ui,接收端是机器 ...

  6. binder

    Service与Android系统设计(7)--- Binder驱动 http://blog.csdn.net/21cnbao/article/details/8087354 Android Bind ...

  7. Spring-boot 配置Aop获取controller里的request中的参数以及其返回值

    首先在你的Maven的pom文件里加入aop的依赖: <dependency> <groupId>org.springframework.boot</groupId> ...

  8. C++ Prime:范围for语句

    C++11新标准引入了一种更简单的for语句,这种语句可以遍历容器或者其他序列的所有元素.范围for语句的语法形式是: for( declaration : expression) statement ...

  9. POJ_3616_Milking_Time_(动态规划)

    描述 http://poj.org/problem?id=3616 给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量. ...

  10. Hadoop RPC源码阅读-交互协议

    Hadoop版本Hadoop2.6 RPC主要分为3个部分:(1)交互协议(2)客户端 (3)服务端 (1)交互协议 协议:把某些接口和接口中的方法称为协议,客户端和服务端只要实现这些接口中的方法就可 ...