[cocos2d-x] 打飞机子弹敌机碰撞检测

时间:2023-02-08 17:26:46

自己做了一个简单的打飞机游戏,比较粗糙,下面把子弹和敌人的产生,移动,碰撞的代码拿出来给大家参考参考

在h文件里面申明以下:

    CCArray * allBullet;//存放所有子弹

   void newBullet(float t);//制造子弹

   void moveBullet(float t);//移动子弹

   CCArray * allfoe;//存放所有敌人

   void newFoe(float t);//制造敌人

   void moveFoe(float t);//移动敌人

   void update(float t);//检测碰撞


在cpp文件里面

init方法里

allBullet=CCArray::create();//创建 子弹容器
allBullet->retain();
//产生子弹
this->schedule(schedule_selector(GameScene::newBullet), 0.3);
//移动子弹
this->schedule(schedule_selector(GameScene::moveBullet), 0.001);


allfoe=CCArray::create();//创建 敌机容器
allfoe->retain();
//产生敌人机器
this->schedule(schedule_selector(GameScene::newFoe), 0.3);
//移动子弹
this->schedule(schedule_selector(GameScene::moveFoe), 0.005);


//碰撞检测更新
this->scheduleUpdate();

下面是子弹敌机以及碰撞相对应的方法 有比较详细的注释

void GameScene::update(float t){
//我的飞机tag是88,获取飞机用于检测是否和敌人碰撞
CCSprite * spritep=(CCSprite *)this->getChildByTag(88);
CCArray * foeToDelete=CCArray::create();//这个数组用于存放要被删除的敌人
foeToDelete->retain();
CCObject* bt,*foe;

//双层循环检测
CCARRAY_FOREACH(this->allfoe, foe)
{
CCSprite *tmpPlane =(CCSprite *)foe;
CCARRAY_FOREACH(this->allBullet, bt)
{
CCSprite *bullet =(CCSprite *)bt;
//假如子弹和敌人两个矩形碰撞
if (bullet->boundingBox().intersectsRect(tmpPlane->boundingBox()))
{
allfoe->removeObject(tmpPlane);
foeToDelete->addObject(tmpPlane);
this->removeChild(bullet);
allBullet->removeObject(bullet);
}
}
//假如敌人和自己的飞机碰撞
if (tmpPlane->boundingBox().intersectsRect(spritep->boundingBox()))
{
foeToDelete->addObject(tmpPlane);
CCDirector::sharedDirector()->getScheduler()->unscheduleAll();//停止所有的unschedule
this->unscheduleUpdate();//停止update
CCLabelTTF* over=CCLabelTTF::create("玩完,小武哥太强了", "宋体", 40);
over->setColor(ccc3(0,0,200));
over->setPosition(ccp(320, 480));
this->addChild(over);
}
}
//移除被击中的敌机
CCARRAY_FOREACH(foeToDelete, foe){
CCSprite *tmpPlane =(CCSprite *)foe;
this->removeChild(tmpPlane);
}

foeToDelete->release();//释放临时敌机数组
}
//创建敌机
void GameScene::newFoe(float t){
CCSprite *foe =CCSprite::create("diji.png");
foe->setAnchorPoint(ccp(0, 0));
foe->setScale(0.5);
foe->setPosition(ccp(arc4random()%620, 1100));
this->addChild(foe);
allfoe->addObject(foe);
}
//移动敌机
void GameScene::moveFoe(float t){
for (int i=0; i<allfoe->count(); i++) {
CCSprite* nowfoe=(CCSprite*)allfoe->objectAtIndex(i);
nowfoe->setPositionY(nowfoe->getPositionY()-8);
if (nowfoe->getPositionY()<20) {
this->removeChild(nowfoe);
allfoe->removeObject(nowfoe);
i--;
}
}
}
//创建子弹
void GameScene::newBullet(float t){
CCPoint bpoint;
//得到飞机的位置
CCSprite * spritep=(CCSprite *)this->getChildByTag(88);
bpoint.x=spritep->getPositionX();
bpoint.y=spritep->getPositionY();
CCSprite * bulletsprite=CCSprite::create("bullet3.png");
bulletsprite->setPosition(bpoint);
this->addChild(bulletsprite);
allBullet->addObject(bulletsprite);
}
//移动子弹
void GameScene::moveBullet(float t){
for (int i=0; i<allBullet->count(); i++) {
CCSprite* nowbullet=(CCSprite*)allBullet->objectAtIndex(i);
nowbullet->setPositionY(nowbullet->getPositionY()+15);
if (nowbullet->getPositionY()>1136) {
this->removeChild(nowbullet);
allBullet->removeObject(nowbullet);
i--;
}
}
}
[cocos2d-x] 打飞机子弹敌机碰撞检测

代码比较粗糙 仅供参考 自己纯手打 转载请申明 谢谢

让飞机移动可以参考我的博客文章::::

[cocos2d-x] 让精灵响应触摸 并把方向旋转到相对应的角度http://blog.csdn.net/yezibao1991/article/details/12771513