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. 【技术贴】jsp出现getOutputStream() has already been calle

    此错误经常在websphere6.x版本里出现:原因是jsp文件中的尖括号百分号里面有空行或者其他的什么原因,Servlet1.2规范规定了OutputStream只能获得一次,在jsp中实际上已经通 ...

  2. [topcoder]BinaryCards

    现在觉得有空时可以刷一下topcoder的DIV 2的Lvl 3的题目.感觉和刷LeetCode和WikiOi都是不一样的. http://community.topcoder.com/stat?c= ...

  3. 使用php-emoji类让网页显示emoji表情

    需要的材料: php-emoji类库的下载地址:https://github.com/iamcal/php-emoji 代码示例:(该代码来自官网) <?php include('emoji.p ...

  4. Android 获取图片资源的4种方式

    1. 图片放在sdcard中 Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard) 2. 图片在项 ...

  5. springboot + devtools(热部署)

    技术介绍 devtools:是boot的一个热部署工具,当我们修改了classpath下的文件(包括类文件.属性文件.页面等)时,会重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,如果发现 ...

  6. linux内核--进程地址空间(三)

    引言:上篇博文中,我们简单的介绍了Linux虚拟存储器的概念及组成情况,下面来分析分析进程的创建和终结及跟进程地址空间的联系. 这里首先介绍一个比较重要的概念:存储器映射 在Linux系统中,通过将一 ...

  7. Andstudio更新失败的解决办法。

    最近AndroidStudio0.60出来了,就急忙想升级,结果屡试屡败.不管架设国外VPN还是Goagent都不行.之前这个问题遇到过,怎么解决的就忘记了.这次又遇到,所以在这里记下,已备以后查阅使 ...

  8. Android Loader详解二:使用加载器

    一个使用装载器的应用会典型的包含如下组件: 一个Activity或Fragment. 一个LoaderManager的实例. 一个加载被ContentProvider所支持的数据的CursorLoad ...

  9. 大型邮箱smtp服务器及端口 收集

    各大型邮箱smtp服务器及端口收集: 新浪邮箱smtp服务器 外发服务器:smtp.vip.sina.com 收件服务器:pop3.vip.sina.com 新浪免费邮件 外发服务器:smtp.sin ...

  10. [辅助类]NHibernateHelper

    对于学习ORM的人来说,NHibernate简直就是福音啊,而且此中技术是相当成熟,在这里分享一个小东西 public class NHibernateHelper { //数据库连接字符串 publ ...