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->set