cocos2d-x SimpleGame(3)如何发射子弹
2013-01-12 18:29
330 查看
现在我们要让主角开枪干掉敌人,添加如下代码让图层支持触摸事件。
这样我们就可以接收到屏幕触摸事件了。
在HelloWorldScene.h声明屏幕触摸事件的回调函数
在HelloWorldScene.cpp文件内进行实现
OK,编译并运行,点击屏幕,试试效果
附本章源码下载:http://download.csdn.net/detail/lpkkk/4997331
文章所写的东西只是我自己的理解,如果哪里出现错误,敬请斧正。
您可以留言、微博私聊我或Email我
我的微博:http://weibo.com/u/2007282737
我的Email:pengkailiao@gmail.com
// 设置图层支持触摸事件 this->setTouchEnabled(true);
这样我们就可以接收到屏幕触摸事件了。
在HelloWorldScene.h声明屏幕触摸事件的回调函数
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);以及注册使Layer处理Touch事件
void registerWithTouchDispatcher();
在HelloWorldScene.cpp文件内进行实现
bool HelloWorld::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent ) { // 获得点击屏幕的坐标 CCPoint location = pTouch->locationInView(); // 把坐标从屏幕坐标系转为OpenGL坐标系 location = CCDirector::sharedDirector()->convertToGL(location); // 获得屏幕尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); // 获得子弹精灵 CCSprite *projectile = CCSprite::create("Projectile.png"); // 设置子弹的初始位置 projectile->setPosition( ccp(20, winSize.height/2) ); // 计算点击屏幕位置和子弹初始位置在X轴的距离和Y轴的距离 int offX = location.x - projectile->getPosition().x; int offY = location.y - projectile->getPosition().y; // 如果点击位置在子弹初始位置的左边则退出不显示子弹 if (offX <= 0) return true; // 把子弹加入图层 this->addChild(projectile); // 计算子弹飞出屏幕后的位置(用相似三角形的性质算出) int realX = winSize.width + (projectile->getContentSize().width/2); float ratio = (float)offY / (float)offX; int realY = (realX * ratio) + projectile->getPosition().y; CCPoint realDest = ccp(realX, realY); // 计算子弹飞出屏幕后的位置和子弹初始位置的距离,设置速度为480像素/秒 int offRealX = realX - projectile->getPosition().x; int offRealY = realY - projectile->getPosition().y; float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY)); float velocity = 480/1; // 480pixels/1sec // 计算子弹飞完整段路程所需时间 float realMoveDuration = length/velocity; // 启动动画,动画完成后的回调函数是spriteMoveFinished(),我们已经实现过 projectile->runAction( CCSequence::actions( CCMoveTo::create(realMoveDuration, realDest), CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) ); return true; } void HelloWorld::registerWithTouchDispatcher() { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); }
OK,编译并运行,点击屏幕,试试效果
附本章源码下载:http://download.csdn.net/detail/lpkkk/4997331
文章所写的东西只是我自己的理解,如果哪里出现错误,敬请斧正。
您可以留言、微博私聊我或Email我
我的微博:http://weibo.com/u/2007282737
我的Email:pengkailiao@gmail.com
相关文章推荐
- cocos2d-x SimpleGame(4)如何检测碰撞
- cocos2d-x SimpleGame(1)如何添加一个精灵
- cocos2d-x SimpleGame(2)如何移动一个精灵
- cocos2d-x SimpleGame(5)如何播放背景音乐和音效
- 如何用cocos2d-x来开发简单的Uphone游戏:(三) 射击子弹 & 碰撞检测
- cocos2d-x入门(4)-英雄发射子弹和碰撞检测
- (译)如何使用cocos2d制作一个Slide Image Game:第一部分
- IOS 如何在cocos2d Game中添加 AdMob 广告条
- Cocos2D-X SimpleGame & 初学者教程
- raywenderlich-Rotating Turrets: How To Make A Simple iPhone Game with Cocos2D Part 2
- cocos2d-x simpleGame 1 --管理背景音乐和旋转炮台
- (6) cocos2d-x发射子弹
- Cocos2d-x《雷电大战》(3)-子弹无限发射
- cocos2d-x-2.2/samples/Cpp/SimpleGame/ 问题
- 第四章——如何发射子弹
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第十步---发射子弹&怪物受伤---所有源码和资源完结下载
- How To Make A Simple Android Game with Cocos2D
- [转注自官网]Cocos2d-x Tutorial 4 - 如何放出子弹(Glede Edition for 2.0.3)
- How To Make A Simple WoPhone Game With Cocos2d-x : How To Run Hello World
- IOS 如何在cocos2d Game中添加 AdMob 广告条