cocos2d-x 3.2 |碰撞与分数
前情提要:飞机大战第五篇 实现碰撞与分数
思路:游戏里物体与物体的碰撞每帧都会检测 这里我们利用前面讲过的贪吃蛇移动原理 利用update方法来实时检测
如下: 新建类----->Game
第一步:Game类(实现主要的游戏逻辑模块)
Game.h
//引入需要用到的类
#include <stdio.h>
#include "cocos2d.h"
#include "cocos-ext.h"
#include "cocosGUI.h"
#include "Bullet.h"
#include "Enemy.h"
#include "Boom.h"
#include "Tool.h"USING_NS_CC;
using namespace cocos2d;
class Game:Layer
{
public:Vector<Bullet *> allBullet;//集合:子弹float startX,startY;CREATE_FUNC(Game);static Scene * CreateScene();bool init();virtual bool onTouchBegan(Touch *touch, Event *unused_event);//触摸检测 按下virtual void onTouchMoved(Touch *touch, Event *unused_event);<span style="font-family: Arial, Helvetica, sans-serif;">//触摸检测 移动</span>void newBullet(float t);//产生子弹void moveBullet(float t);//移动子弹Vector<Enemy *> allEnemy;//<span style="font-family: Arial, Helvetica, sans-serif;">集合:</span><span style="font-family: Arial, Helvetica, sans-serif;">敌机</span>void newEnemy(float t);//产生敌机void moveEnemy(float t);//移动敌机void update(float t);//游戏逻辑 碰撞检测int m_score;void reCallBack(Ref * obj);Vector<Tool *> allTool;//集合:道具
};

Game.cpp

#include "Game.h"
#include "BackGround.h"
#include "Plane.h"
#include "MainMenu.h"
#include "SimpleAudioEngine.h"
#include "cocosGUI.h"
#include "GameOver.h"
#include "Tool.h"
#include "Enemy.h"
using namespace ui;
using namespace CocosDenshion;
USING_NS_CC;
Scene * Game::CreateScene()
{auto scene=Scene::create();auto layer=Game::create();scene->addChild(layer);return scene;
}
bool Game::init()
{if (!Layer::init()){return false;}//创建 返回 按钮auto menurt=MenuItemLabel::create(Label::createWithSystemFont("返回", "", 32),CC_CALLBACK_1(Game::reCallBack, this));menurt->setAnchorPoint(Vec2::ZERO);menurt->setPosition(600-30,Director::getInstance()->getWinSize().height-36);menurt->setColor(Color3B::GREEN);//创建菜单auto menu=Menu::create(menurt,nullptr);menu->setPosition(Vec2::ZERO);this->addChild(menu,2);//添加背景auto bk=BackGround::create();this->addChild(bk);//添加飞机auto fly1=Plane::create();this->addChild(fly1,1);fly1->moveTo(100,100);fly1->setTag(10);//处理飞机的HPfor (int i=0; i<fly1->hp; i++){auto sphp=Sprite::create("hp_1.png");sphp->setTag(1000+i);this->addChild(sphp);sphp->setAnchorPoint(Vec2(0,1));sphp->setPosition(Vec2(i*sphp->getContentSize().width+3*i,Director::getInstance()->getWinSize().height));}//控制飞机飞行auto listener=EventListenerTouchOneByOne::create();//创建侦听listener->onTouchBegan=CC_CALLBACK_2(Game::onTouchBegan, this);//定义侦听的回调listener->onTouchMoved=CC_CALLBACK_2(Game::onTouchMoved, this);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);//将侦听添加到事件分发器中//产生子弹 计划任务this->schedule(schedule_selector(Game::newBullet),0.2);//每隔200毫秒产生一次子弹this->schedule(schedule_selector(Game::moveBullet),0.01);//产生敌机this->schedule(schedule_selector(Game::newEnemy),1);this->schedule(schedule_selector(Game::moveEnemy),0.01);//实现游戏逻辑this->scheduleUpdate();//分数显示器1this->m_score=0;auto tscore=TextAtlas::create("0000", "img_num_dis.png",17 , 22, "0");this->addChild(tscore,12);tscore->setTag(210);tscore->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,Director::getInstance()->getWinSize().height-64));//分数显示2
//    auto scoreLb = Label::createWithSystemFont(StringUtils::format("分数:%d",score), "", 32);
//    scoreLb->setAnchorPoint(Vec2::ZERO);
//    scoreLb->setColor(Color3B::GREEN);
//    scoreLb->setPosition(0,Director::getInstance()->getWinSize().height-36);
//    scoreLb->setTag(123);
//    this->addChild(scoreLb,2);return true;
}
//返回主菜单
void Game::reCallBack(Ref * obj)
{auto scene=MainMenu::CreateScene();Director::getInstance()->replaceScene(scene);
}//按下
bool Game::onTouchBegan(Touch *touch, Event *unused_event)
{this->startX=touch->getLocation().x;this->startY=touch->getLocation().y;return true;
}
//移动
void Game::onTouchMoved(Touch *touch, Event *unused_event)
{float mx=touch->getLocation().x-startX;float my=touch->getLocation().y-startY;auto fly1=(Plane *)this->getChildByTag(10);fly1->moveTo(fly1->px+mx, fly1->py+my);this->startX=touch->getLocation().x;this->startY=touch->getLocation().y;
}
void Game::newBullet(float t)//产生子弹
{auto fly1=(Plane *)this->getChildByTag(10);Bullet * bt=Bullet::newBullet(1, 0,fly1->px,fly1->py);allBullet.pushBack(bt);this->addChild(bt);//子弹音效SimpleAudioEngine::getInstance()->playEffect("launch_1.wav");
}void Game::moveBullet(float t)//移动子弹
{for (int i=0;i<allBullet.size();i++){Bullet * nowB=allBullet.at(i);nowB->moveTo(nowB->px, nowB->py+15);if (nowB->py>Director::getInstance()->getWinSize().height){allBullet.erase(i);this->removeChild(nowB);i--;}}}
void Game::newEnemy(float t)
{int type=random()%10;if (type < 3){type = 1;}else if (type > 8){type = 2;}else{type = 1;}int ex=random()%(int)Director::getInstance()->getWinSize().width;Enemy * news=Enemy::createEnemy(type, ex, Director::getInstance()->getWinSize().height+200);allEnemy.pushBack(news);//将新产生的敌机添加到集合this->addChild(news);   //在当前GAME图层添加敌机层
}void Game::moveEnemy(float t)
{for (int i=0; i<allEnemy.size(); i++){//获取第i架敌机Enemy * nowE=allEnemy.at(i);nowE->moveTo(nowE->ex, nowE->ey-5);if (nowE->ey<-nowE->eSprite->getContentSize().height){allEnemy.erase(i);this->removeChild(nowE);i--;}}
}
void Game::update(float t)
{//创建一个Label层 获取计数器标签//auto scoreLb = (Label *)this->getChildByTag(123);//碰撞检测if (allEnemy.size()==0 || allBullet.size()==0){return;}auto fly1=(Plane *)this->getChildByTag(10);//判断飞机和道具for (int j = 0; j<allTool.size(); j++){//得到第j架敌机Tool * nowTool=allTool.at(j);Rect br(fly1->px,fly1->py,fly1->getChildByTag(10)->getContentSize().width,fly1->getChildByTag(10)->getContentSize().height);//敌机的矩形大小Rect er(nowTool->tx,nowTool->ty,nowTool->sp->getContentSize().width,nowTool->sp->getContentSize().height);if (br.intersectsRect(er)){//碰撞检测到两个矩形相交//判断吃到哪种道具switch (nowTool->type) {case 1://hp{fly1->hp++;int i=fly1->hp-1;auto sphp=Sprite::create("hp_1.png");sphp->setTag(1000+i);this->addChild(sphp);sphp->setAnchorPoint(Vec2(0,1));sphp->setPosition(Vec2(i*sphp->getContentSize().width+3*i,Director::getInstance()->getWinSize().height));}break;case 2://炸雷{//所有敌机都销毁while (allEnemy.size()>0){Enemy * nowEnemy=allEnemy.at(0);Boom * boom=Boom::createBoom(2, nowEnemy->ex, nowEnemy->ey);this->addChild(boom,10);//爆炸一次 总增加分数this->m_score+=300;allEnemy.eraseObject(nowEnemy);nowEnemy->removeFromParentAndCleanup(true);}//播放音效SimpleAudioEngine::getInstance()->playEffect("launch_1.wav");//播放爆炸粒子Boom *  boom=Boom::createBoom(2,Director::getInstance()->getWinSize().width/2,Director::getInstance()->getWinSize().height/2);this->addChild(boom,15);}break;case 3://激光break;case 4://散弹break;}//敌机消失nowTool->unscheduleAllSelectors();nowTool->removeFromParentAndCleanup(true);allTool.eraseObject(nowTool);j--;}}for (int j = 0; j<allEnemy.size(); j++){//得到第j架敌机Enemy * nowEnemy=allEnemy.at(j);//子弹的矩形大小Rect br(fly1->px,fly1->py,fly1->getChildByTag(10)->getContentSize().width,fly1->getChildByTag(10)->getContentSize().height);//敌机的矩形大小 Rect er(nowEnemy->ex,nowEnemy->ey,nowEnemy->eSprite->getContentSize().width,nowEnemy->eSprite->getContentSize().height);if (br.intersectsRect(er)){//碰撞检测到两个矩形相交//敌机消失allEnemy.eraseObject(nowEnemy);nowEnemy->removeFromParentAndCleanup(true);j--;//掉血音效SimpleAudioEngine::getInstance()->playEffect("skill_1.wav");//我方Hp--if (fly1->hp>0){this->removeChildByTag(1000+fly1->hp-1);fly1->hp=fly1->hp-1;}if(fly1->hp<=0){//Game Over//创建一个场景auto scene=GameOver::createScene();//将场景覆盖到Game Over 并释放之前场景Director::getInstance()->replaceScene(scene);}}}//判断子弹 是否碰到敌机for (int i=0; i<allBullet.size(); i++){//得到第i颗子弹Bullet * nowBullet=allBullet.at(i);for (int j = 0; j<allEnemy.size(); j++){//得到第j架敌机Enemy * nowEnemy=allEnemy.at(j);//子弹的矩形大小Rect br(nowBullet->px,nowBullet->py,nowBullet->bt->getContentSize().width,nowBullet->bt->getContentSize().height);//敌机的矩形大小Rect er(nowEnemy->ex,nowEnemy->ey,nowEnemy->eSprite->getContentSize().width,nowEnemy->eSprite->getContentSize().height);if (br.intersectsRect(er)){//检测到两个矩形相交 子弹消失allBullet.eraseObject(nowBullet);nowBullet->removeFromParentAndCleanup(true);i--;nowEnemy->hp=nowEnemy->hp-1;if(nowEnemy->hp<=0){//爆炸if(nowEnemy->type==1){Boom * boom=Boom::createBoom(1, nowEnemy->ex, nowEnemy->ey);this->addChild(boom);//scoreLb->setString(StringUtils::format("分数:%d",score+=40));//增加分数this->m_score+=40;}else{Boom * boom=Boom::createBoom(2, nowEnemy->ex, nowEnemy->ey);this->addChild(boom);//scoreLb->setString(StringUtils::format("分数:%d",score+=60));this->m_score+=60;}//分数增加器auto score1=(TextAtlas *)this->getChildByTag(210);score1->setString(StringUtils::format("%d",m_score));//爆炸音效SimpleAudioEngine::getInstance()->playEffect("launch_2.wav");//道具处理if (nowEnemy->toolID>0) {//掉落道具auto tool=Tool::newTool(nowEnemy->toolID,nowEnemy->ex,nowEnemy->ey);this->addChild(tool);allTool.pushBack(tool);}allEnemy.eraseObject(nowEnemy);nowEnemy->removeFromParentAndCleanup(true);j--;}break;//退出这次循环}}}
}

总结:至此利用5个篇章简单实现了一下飞机大战的主要核心内容,希望对读者有帮

后期还有两个番外篇:游戏分数保存+安卓导出调试(因为安卓实在是让人蛋疼)
ps:游戏中用到的素材与音效请自行替换。

cocos2d-x 3.2 |飞机大战:碰撞与分数相关推荐

  1. pygame 飞机大战碰撞检查的运用(三)用sprite,实现完美碰撞检查

    目标:实现完美的碰撞检查 前面的只是普通的碰撞检查,用到了矩形框的范围.图像如果都不是矩形,用普通检查,两种已经相碰,但画面显示还未相碰,这就很尴尬了. sprite模块中,有个collide_mas ...

  2. 【游戏开发实战】使用Unity 2019制作仿微信小游戏飞机大战(七):主角飞机碰撞与爆炸

    文章目录 零.教程目录 一.前言 二.本篇目标 三.飞机机碰撞组件:BoxCollider2D.Rigidbody2D 四.添加Tag:Enemy 五.主角飞机碰撞处理:OnTriggerEnter2 ...

  3. 飞机大战-Cocos Creator 碰撞系统实践

    本文概要 本文主要是讲解Cocos Creator碰撞系统的简单实践,并且结合一个简单小项目-飞机大战,来一起熟悉碰撞系统的使用.demo效果如下图. ​ 关于Cocos Creator 碰撞基本介绍 ...

  4. cocos2d-x小游戏——飞机大战

    上周,我做了一个基于 cocos2d-x 的飞机大战的游戏,因为我刚学cocos2d-x没多久,所以这个飞机大战很多都是看着别人的教程,再加上自己的一些想法,来做的. 下面我想说一说我的思路. 飞机大 ...

  5. Cocos2d-x 3.2 lua飞机大战开发实例(三)道具的掉落,碰撞检测,声音,分数,爆炸效果,完善游戏的功能细节

           Cocos2d-X 3.2  lua语言飞机大战开发实例(三) 7.添加声音,更新分数,添加爆炸效果,道具的掉落.道具的碰撞检测等完善游戏功能 爆炸的效果添加 首先需要在GameData ...

  6. C++ 与cocos2d-x-4.0完成太空飞机大战 (四)

    C++ 与cocos2d-x-4.0完成太空飞机大战 (四) 动画演示 工具类编码:Util.cpp 工具类编码:Util.h 奖励精灵编码:RewardSprite.cpp 奖励精灵编码:Rewar ...

  7. C++ 与cocos2d-x-4.0完成太空飞机大战 (五)

    C++ 与cocos2d-x-4.0完成太空飞机大战 (五) 动画演示 敌人精灵编码:EnemySprite.cpp 敌人精灵编码:EnemySprite.h 敌人编码:Enemy.cpp 敌人编码: ...

  8. C++ 与cocos2d-x-4.0完成太空飞机大战 (一)

    C++ 与cocos2d-x-4.0完成太空飞机大战 (一) 动画演示 AppDelegate编码:AppDelegate.cpp AppDelegate编码:AppDelegate.h 关卡1场景编 ...

  9. C++ 与cocos2d-x-4.0完成太空飞机大战 (二)

    C++ 与cocos2d-x-4.0完成太空飞机大战 (二) 动画演示 飞机精灵编码:AircraftSprite.cpp 飞机精灵编码:AircraftSprite.h 飞机动画编码:Aircraf ...

最新文章

  1. oracle触发器 select into,Oracle触发器中selectinto报错no_data_found异常处理
  2. 牛客练习赛34 - C little w and Segment Coverage(思维、树状数组)
  3. 蓝桥杯2016省赛真题-剪邮票(dfs)
  4. 容器viewController添加或者删除子viewController
  5. ubuntu下python多版本切换问题
  6. 阿里巴巴卖空阿里巴巴入股新浪微博抑制投资者卖空行为
  7. python迭代器和生成器_python迭代器和生成器
  8. unity后期处理插件post process超级后期滤镜
  9. Python3-环境篇-01-Python3安装
  10. C语言科学计数法中的一个小问题
  11. mysql怎么用迅雷下载_MySQL安装详细步骤(附迅雷下载链接)
  12. Collecting package metadata (current_repodata.json)解决方法
  13. 统计推断——假设检验——检验的功效(势)
  14. OpenERP-指定动作视图
  15. QQ浏览器X5内核问题汇总
  16. JINI和java space入门
  17. 干货 | 如何确定是否属于关键信息基础设施?
  18. __builtin_offsetof()
  19. 计算机主机需要ccc,计算机3C认证怎么办理,要什么资料
  20. FusionCharts Demo

热门文章

  1. 2022年全球及中国舞台灯光系统行业头部企业市场占有率及排名调研报告
  2. 我看过的关于职业规划最好最全面的一篇文章(因为在另外一个人博客中他转载的字体太大了颜色太鲜艳不适宜阅读,所以就自己转载了)
  3. index.php.bak 颓废_18.phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613)
  4. H5 LayaAir实现复制微信号到剪切板功能
  5. 路由器WiFi密码更改及隐藏操作
  6. 【转载】生产订单完工确认(CO11N) BAPI : BAPI_PRODORDCONF_CREATE_TT
  7. 2021年全国职业院校技能大赛 “大数据技术与应用”—模拟赛题(三)
  8. 让鼠标漫天飞舞:在内核中实现鼠标的中断处理
  9. 手把手教你安装虚拟机16
  10. 提高禁毒意识,vr全景直观感受毒品危害