cocos2dx碰撞检测实现
2013-11-01 09:35
281 查看
本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=146
在此场景中,我们假设有很多敌人和子弹,其中子弹可以击中敌人,并且碰撞后子弹和敌人都会随之消失。
首先,我们需要去追踪这些敌人和子弹。
我们可以为这两种物体添加tag,在这里我们给敌人添加tag=1,给子弹添加tag=2,。因为CCSprite继承自CCNode,因为已经有了setTag和getTag方法;我们可以通过这种方式来区分不同的精灵。
添加两个成员对象在HelloWorldScene.h中。用来分别存储存在的敌人和子弹。
在cocos2dx中,在构造函数中初始化这两个变量,在init()函数中new,在析构函数中release。
现在更改addTarget()来添加一个新的敌人到targets数组中,并且把它的tag设置为1.
更改一下ccTouchesEnded()来添加一个新的子弹到bullets数组中,并且设置它的tag为2.
接下来,更改如下的spriteMoveFinished()函数。这里我们将从相应的数组中移除一些精灵。
下面的函数update()将会每帧被执行到一次,一般用来检测碰撞检测,可以移除掉碰撞的子弹和敌人。
在HelloWorldScene.h中声明它,在HelloWorldScene.cpp.定义它。
好了,最后的事情我们将要做的就是添加update()到计时器中,来让它每帧被调用一次,一般在onEnter()函数中添加此行代码。
一个简单的碰撞检测模型就建立好啦~
在此场景中,我们假设有很多敌人和子弹,其中子弹可以击中敌人,并且碰撞后子弹和敌人都会随之消失。
首先,我们需要去追踪这些敌人和子弹。
我们可以为这两种物体添加tag,在这里我们给敌人添加tag=1,给子弹添加tag=2,。因为CCSprite继承自CCNode,因为已经有了setTag和getTag方法;我们可以通过这种方式来区分不同的精灵。
添加两个成员对象在HelloWorldScene.h中。用来分别存储存在的敌人和子弹。
protected: cocos2d::CCArray *_targets; cocos2d::CCArray *_projectiles;
在cocos2dx中,在构造函数中初始化这两个变量,在init()函数中new,在析构函数中release。
// 初始化 _targets = new CCArray; _projectiles = new CCArray; HelloWorld::~HelloWorld() { if (_targets) { _targets->release(); _targets = NULL; } if (_projectiles) { _projectiles->release(); _projectiles = NULL; } // cpp不需要调用super释放,虚析构函数将会做这些事 } HelloWorld::HelloWorld() :_targets(NULL) ,_projectiles(NULL) { }
现在更改addTarget()来添加一个新的敌人到targets数组中,并且把它的tag设置为1.
// 添加到数组中 target->setTag(1); _targets->addObject(target);
更改一下ccTouchesEnded()来添加一个新的子弹到bullets数组中,并且设置它的tag为2.
// 添加到_projectiles数组中 projectile->setTag(2); _projectiles->addObject(projectile);
接下来,更改如下的spriteMoveFinished()函数。这里我们将从相应的数组中移除一些精灵。
void HelloWorld::spriteMoveFinished(CCNode* sender) { CCSprite *sprite = (CCSprite *)sender; this->removeChild(sprite, true); if (sprite->getTag() == 1) // target { _targets->removeObject(sprite); } else if (sprite->getTag() == 2) // projectile { _projectiles->removeObject(sprite); } }
下面的函数update()将会每帧被执行到一次,一般用来检测碰撞检测,可以移除掉碰撞的子弹和敌人。
在HelloWorldScene.h中声明它,在HelloWorldScene.cpp.定义它。
void HelloWorld::update(float dt) { CCArray *projectilesToDelete = new CCArray; CCArray* targetsToDelete =new CCArray; CCObject* it = NULL; CCObject* jt = NULL; CCARRAY_FOREACH(_bullet, it) { CCSprite *projectile = dynamic_cast<CCSprite*>(it); CCRect projectileRect = CCRectMake( projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCARRAY_FOREACH(_target, jt) { CCSprite *target = dynamic_cast<CCSprite*>(jt); CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (projectileRect.intersectsRect(targetRect)) { targetsToDelete->addObject(target); projectilesToDelete->addObject(projectile); } } } CCARRAY_FOREACH(targetsToDelete, jt) { CCSprite *target = dynamic_cast<CCSprite*>(jt); _target->removeObject(target); this->removeChild(target, true); } CCARRAY_FOREACH(projectilesToDelete, it) { CCSprite* projectile = dynamic_cast<CCSprite*>(it); _bullet->removeObject(projectile); this->removeChild(projectile, true); } projectilesToDelete->release(); targetsToDelete->release(); }
好了,最后的事情我们将要做的就是添加update()到计时器中,来让它每帧被调用一次,一般在onEnter()函数中添加此行代码。
void HelloWorld::onEnter() { CCLayer::onEnter(); this->schedule( schedule_selector(HelloWorld::update) ); }
一个简单的碰撞检测模型就建立好啦~
相关文章推荐
- cocos2dx碰撞检测实现
- cocos2dx3.x物理引擎的碰撞检测
- 学习 "使用cocos2d-x3.0和物理引擎实现碰撞检测" 中出现的一个问题
- Cocos2dx 3.1.1 之 监听器、特效切换场景、动作、检测碰撞、回调函数
- Ogre1.8.1射线实现碰撞检测例子源代码
- 游戏里实现碰撞检测方法
- cocos2dx 3.x 物理引擎接触检测掩码、碰撞掩码和类别掩码问题解析
- Open Inventor实现碰撞检测
- 快速教程:在2D空间中使用四叉树实现碰撞检测
- JavaScript实现碰撞检测(分离轴定理)
- Unity2D - 2. 实现基本的人物移动控制和碰撞检测
- Cocos2dx 像素判断碰撞检测
- canvas 记录绘画对象实现交互能力 简单碰撞检测
- cocos2dx 精灵的碰撞检测和消灭(3)
- cocos2dx物理引擎和碰撞检测(五)框架Fixtures
- 【Cocos2dx】飘字特效与碰撞检测
- COCOS2DX,cocostudio::ColliderDetector 简单介绍 骨骼动画绑定碰撞区域进行碰撞检测
- cocos2d滑动碰撞检测的实现
- cocos2dx重力感应中的碰撞检测以及对墙角模型的完善检测
- iOS实现浮动泡泡功能,悬浮泡泡,windows系统屏幕保护程序的气泡功能(碰撞检测)