Chapter 4 - How to Fire some Bullets
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的更多相关文章
- 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 ...
- hdu 1045:Fire Net(DFS经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- Fire Net
Fire Net Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- JavaScript- The Good Parts Chapter 5 Inheritance
Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...
- HDU-1045 Fire Net
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) Me ...
- 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 ...
- hdoj 1045 Fire Net
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- 【UVA 1411】 Ants (KM)
Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...
- [wikioi]多源最短路
http://wikioi.com/problem/1077/ Floyd算法.精华是三层循环,if (dist(i,k) + dist(k,j) < dist(i,j)) then dist( ...
- Delphi XML-RPC 中文乱码解决方法
http://download.csdn.net/user/csm2432/uploads/2
- Android Animation
Android中常用两种动画模式,tween animation和frame animation,即补间动画和帧动画,但在android3.0中又引入了一个新的动画系统:property animat ...
- wcf异常汇总
1.确保客户端可以接收到服务端的异常 2.部署wcf出错,http错误404.3 3.无法自动调试 未能调试远程过程.这通常说明未在服务器上启用调试 WCF 托管在IIS上 4.ChannelFact ...
- POJ_3666_Making_the_Grade_(动态规划)
描述 http://poj.org/problem?id=3666 给一串坡的高度,现在要调整某些点,使整个坡单调不降或单调不升.调整的花费为原高度与先高度的差的绝对值,问最小花费(可单增可单降). ...
- jBPM 4.4 数据库设计
1 存储流程定义相关的部署信息数据库 1.1 jbpm4_deployment 字段名 字段含义 类型 允许空值 键 DBID_ 流程模板标识 Bigint( ...
- Makefile自动生成头文件依赖
前言 Makefile自动生成头文件依赖是很常用的功能,本文的目的是想尽量详细说明其中的原理和过程. Makefile模板 首先给出一个本人在小项目中常用的Makefile模板,支持自动生成头文件依赖 ...
- JavaScript---网络编程(1)-介绍、变量、运算符与语句
JavaScript也是一种编程语言.并不是Java的分支哦. 可以直接在浏览器中运行的编程语言. JavaScript 的历史故事: 1.JavaScript语言与名称的由来(Netscape,Su ...
- ll 命令不好用了,ls 命令没有颜色了怎么办
可以通过以下命令来实现‘ll’的功能: echo "alias ll='ls -l'" >> ~/.bashrc 其实这个命令是给‘ls -l’命令起了个别名‘ll’, ...