首先在上一个案例飞机大战的项目中增加一个敌人类Enemy,继承至CCNode,该类定义如下方法和属性:
static Enemy* createEnemy(EnemyType type, float m_speed);
CCSize getShowContentSize();
CCRect getCollisionArea()获取敌人的碰撞区域;
void hurt(int hurtnum);
int m_hp;
float m_speed;
CCSprite *m_showSprite;
bool initEnemy(EnemyType type, float m_speed);
void move();
void remove()。
敌人飞机所拥有的功能和Plane类类似,依次实现敌人飞机的移动、获取碰撞区域等方法。hurt方法实现是被子弹击中后的效果。
道具和子弹一样拥有自己的速度、碰撞区域等所以将道具也封装成一个类,在飞机大战的项目中增加一个道具类Star,继承至CCNode,该类定义如下方法和属性:
CREATE_FUNC(Star);
CCSize getShouContentSize();
CCRect getCollisionArea();
void willRemove();
CC_SYNTHESIZE(bool, m_isWillRemove, IsWillRemove);
CCSprite *m_showSprite;
float m_speed;
bool init();
void move();
void remove()。
最后在精灵层中添加敌人和道具,然后进行子弹和敌人飞机的碰撞检测、玩家飞机和道具的碰撞检测,并实现相应的业务逻辑。
实现此案例需要按照如下步骤进行。
步骤一:创建敌人类
首先在上一个案例飞机大战的项目中增加一个敌人类Enemy,继承至CCNode,代码如下所示:
- typedef enum {
- TYPE_1,
- TYPE_2,
- TYPE_3,
- TYPE_4,
- TYPE_COUNT
- }EnemyType;
- class Enemy : public CCNode {
- public:
- static Enemy* createEnemy(EnemyType type, float m_speed);
- CCSize getShowContentSize();
- //获取敌人的碰撞区域
- CCRect getCollisionArea();
- //伤害
- void hurt(int hurtnum);
- private:
- int m_hp;
- float m_speed;
- CCSprite *m_showSprite;
- bool initEnemy(EnemyType type, float m_speed);
- void move();
- void remove();
- };
其次实现敌人的创建方法和初始化方法,代码如下所示:
- Enemy* Enemy::createEnemy(EnemyType type, float speed) {
- Enemy *enemy = new Enemy;
- if (enemy && enemy->initEnemy(type, speed)) {
- enemy->autorelease();
- return enemy;
- }
- CC_SAFE_DELETE(enemy);
- return NULL;
- }
然后实现敌人的初始化方法initEnemy,在初始化方法中开启定时器调实现敌人的移动,代码如下所示:
- bool Enemy::initEnemy(EnemyType type, float speed) {
- if(!CCNode::init()) return false;
- const char* imageName;
- switch ((int)type) {
- case TYPE_1:
- imageName = "enem1.png";
- m_hp = 5;
- break;
- case TYPE_2:
- imageName = "enem2.png";
- m_hp = 10;
- break;
- case TYPE_3:
- imageName = "enem3.png";
- m_hp = 15;
- break;
- case TYPE_4:
- imageName = "enem4.png";
- m_hp = 20;
- break;
- }
- m_showSprite = CCSprite::create(imageName);
- this->addChild(m_showSprite);
- m_speed = speed;
- this->schedule(schedule_selector(Enemy::move));
- return true;
- }
接着实现定时器的回调方法move,实现敌人的移动,代码如下所示:
- void Enemy::move() {
- this->setPositionY(this->getPositionY() - m_speed);
- if (this->getPositionY() <= - m_showSprite->getContentSize().height * 0.5) {
- this->remove();
- }
- }
再实现敌人移除的方法和伤害的方法,在敌人被移除之前会有闪烁的粒子效果,代码如下所示:
- //敌人移除的方法
- void Enemy::remove() {
- SpriteLayer *layer = (SpriteLayer*)this->getParent();
- layer->getEnemys()->removeObject(this);
- this->removeFromParent();
- }
- //被子弹击中的效果
- void Enemy::hurt(int hurtnum) {
- m_hp -= hurtnum;
- if (m_hp <= 0) {
- CCParticleSystemQuad *particle = CCParticleSystemQuad::create("particle_boom.plist");
- SpriteLayer *layer = (SpriteLayer*)this->getParent();
- layer->addChild(particle);
- particle->setPosition(this->getPosition());
- //粒子生命结束后自动释放
- particle->setAutoRemoveOnFinish(true);
- this->remove();
- }else {
- m_showSprite->stopActionByTag(100);
- CCBlink *blink = CCBlink::create(0.8, 3);
- blink->setTag(100);
- m_showSprite->runAction(blink);
- }
- }
最后实现获取敌人碰撞区域的方法getCollisionArea,代码如下所示:
- //获取敌人的碰撞区域
- CCRect Enemy::getCollisionArea() {
- CCRect rect = m_showSprite->boundingBox();
- //飞机节点坐标系下矩形原点位置
- CCPoint origin_sprite = rect.origin;
- //屏幕坐标系下的原点位置(由于精灵层没有改变过位置,所以现在精灵层的坐标系和屏幕坐标系是吻合)
- CCPoint origin = this->convertToWorldSpace(origin_sprite);
- return CCRectMake(origin.x, origin.y, rect.size.width, rect.size.height);
- }
步骤二:创建道具类
由于道具和子弹一样拥有自己的速度、碰撞区域等所以将道具也封装成一个类,创建一个道具类Star,继承至CCNode,代码如下所示:
- class Star : public CCNode {
- public:
- CREATE_FUNC(Star);
- CCSize getShouContentSize();
- //获取星星的碰撞区域
- CCRect getCollisionArea();
- //将要移除的方法(做动画)
- void willRemove();
- //是否将要移除
- CC_SYNTHESIZE(bool, m_isWillRemove, IsWillRemove);
- private:
- CCSprite *m_showSprite;
- float m_speed;
- bool init();
- void move();
- void remove();
- };
然后依次实现道具的初始化方法init,代码如下所示:
- bool Star::init() {
- if (!CCNode::init()) return false;
- m_speed = 10;
- m_isWillRemove = false;
- m_showSprite = CCSprite::create("star.png");
- this->addChild(m_showSprite);
- this->schedule(schedule_selector(Star::move));
- return true;
- }
接下来实现道具移动的方法move,以及获取道具碰撞区域的方法,代码如下所示:
- //道具移动的方法
- void Star::move() {
- this->setPositionY(this->getPositionY() - m_speed);
- if (this->getPositionY() <= -m_showSprite->getContentSize().height * 0.5) {
- this->remove();
- }
- }
- //获取星星的碰撞区域
- CCRect Star::getCollisionArea() {
- CCRect rect = m_showSprite->boundingBox();
- //星星节点坐标系下矩形原点位置
- CCPoint origin_sprite = rect.origin;
- //屏幕坐标系下的原点位置(由于精灵层没有改变过位置,所以现在精灵层的坐标系和屏幕坐标系是吻合)
- CCPoint origin = this->convertToWorldSpace(origin_sprite);
- return CCRectMake(origin.x, origin.y, rect.size.width, rect.size.height);
- }
最后实现道具移除的方法remove以及移除之前的动画方法willRemove,代码如下所示:
- //将要移除的方法(做动画)
- void Star::willRemove() {
- m_isWillRemove = true;
- //停止星星的移动
- this->unschedule(schedule_selector(Star::move));
- CCAnimation *animation = CCAnimation::create();
- //
- for (int i = 0; i < 4; i++) {
- CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("eat_item%d.png", i)->getCString());
- animation->addSpriteFrame(frame);
- }
- animation->setDelayPerUnit(0.05);
- CCAnimate *animate = CCAnimate::create(animation);
- CCCallFunc *callFunc =CCCallFunc::create(this, callfunc_selector(Star::remove));
- CCSequence *seq = CCSequence::create(animate, callFunc,NULL);
- m_showSprite->runAction(seq);
- }
- //移除
- void Star::remove(){
- SpriteLayer *layer = (SpriteLayer*)this->getParent();
- layer->getStars()->removeObject(this);
- this->removeFromParent();
- }
步骤三:在精灵层中添加敌人和道具
首先在精灵层类中增加如下属性和方法:
CC_SYNTHESIZE_READONLY(CCArray*, m_stars, Stars)存放道具对象的数组;
CC_SYNTHESIZE_READONLY(CCArray*, m_enemys, Enemys)存放敌人的数组;
void addStar()添加星星的方法;
void addEnemy()添加敌人的方法。
首先在精灵层的初始化方法中设置定时器添加敌人,并且实现定时器的回调方法addEnemy方法,代码如下所示:
- bool SpriteLayer::initSpriteLayer(int Level) {
- if(!CCLayer::init())return false;
- //设置随机算子 time(0)当前系统时间
- srand(time(0));
- this->loadRosources();
- m_stars = CCArray::create();
- CC_SAFE_RETAIN(m_stars);
- m_bullets = CCArray::create();
- CC_SAFE_RETAIN(m_bullets);
- m_removebullets = CCArray::create();
- CC_SAFE_RETAIN(m_removebullets);
- m_enemys = CCArray::create();
- CC_SAFE_RETAIN(m_enemys);
- m_buttle_level = 1;
- //创建云池对象
- m_cloudPool = CloudPool::creatPool(5);
- CC_SAFE_RETAIN(m_cloudPool);
- //往精灵层中添加云
- this->schedule(schedule_selector(SpriteLayer::addCloud), 2);
- //创建玩家飞机
- m_plane = Plane::createPlane(0);
- this->addChild(m_plane);
- m_plane->setPosition(SCREEN_POSITION(0.5, 0.2));
- //创建敌机
- this->schedule(schedule_selector(SpriteLayer::addEnemy), 2);
- //开启触摸
- this->setTouchEnabled(true);
- //设置触摸模式
- this->setTouchMode(kCCTouchesOneByOne);
- return true;
- }
- //添加敌人
- void SpriteLayer::addEnemy() {
- int type = rand() % TYPE_COUNT;
- float speed = rand() % 5 + 3;
- Enemy *enemy = Enemy::createEnemy((EnemyType)type, speed);
- enemy->setPosition(CCRANDOM_0_1() * SCREEN.width, SCREEN.height + enemy->getShowContentSize().height * 0.5);
- this->addChild(enemy);
- m_enemys->addObject(enemy);
- }
其次在精灵层的初始化方法中设置定时器添加道具,并且实现定时器的回调方法addStar方法,代码如下所示:
- bool SpriteLayer::initSpriteLayer(int Level) {
- if(!CCLayer::init())return false;
- //设置随机算子 time(0)当前系统时间
- srand(time(0));
- this->loadRosources();
- m_stars = CCArray::create();
- CC_SAFE_RETAIN(m_stars);
- m_bullets = CCArray::create();
- CC_SAFE_RETAIN(m_bullets);
- m_removebullets = CCArray::create();
- CC_SAFE_RETAIN(m_removebullets);
- m_enemys = CCArray::create();
- CC_SAFE_RETAIN(m_enemys);
- m_buttle_level = 1;
- //创建云池对象
- m_cloudPool = CloudPool::creatPool(5);
- CC_SAFE_RETAIN(m_cloudPool);
- //往精灵层中添加云
- this->schedule(schedule_selector(SpriteLayer::addCloud), 2);
- //添加星星道具
- this->schedule(schedule_selector(SpriteLayer::addStar), 4);
- //创建玩家飞机
- m_plane = Plane::createPlane(0);
- this->addChild(m_plane);
- m_plane->setPosition(SCREEN_POSITION(0.5, 0.2));
- //创建敌机
- this->schedule(schedule_selector(SpriteLayer::addEnemy), 2);
- //开启触摸
- this->setTouchEnabled(true);
- //设置触摸模式
- this->setTouchMode(kCCTouchesOneByOne);
- return true;
- }
- //添加星星
- void SpriteLayer::addStar() {
- Star *star = Star::create();
- star->setPosition(CCRANDOM_0_1() * SCREEN.width, SCREEN.height + star->getShouContentSize().height * 0.5);
- this->addChild(star);
- m_stars->addObject(star);
- }
然后在精灵层的初始化方法中设置定时器添加道具,并且实现定时器的回调方法addStar方法,代码如下所示:
- bool SpriteLayer::initSpriteLayer(int Level) {
- if(!CCLayer::init())return false;
- //设置随机算子 time(0)当前系统时间
- srand(time(0));
- this->loadRosources();
- m_stars = CCArray::create();
- CC_SAFE_RETAIN(m_stars);
- m_bullets = CCArray::create();
- CC_SAFE_RETAIN(m_bullets);
- m_removebullets = CCArray::create();
- CC_SAFE_RETAIN(m_removebullets);
- m_enemys = CCArray::create();
- CC_SAFE_RETAIN(m_enemys);
- m_buttle_level = 1;
- //创建云池对象
- m_cloudPool = CloudPool::creatPool(5);
- CC_SAFE_RETAIN(m_cloudPool);
- //往精灵层中添加云
- this->schedule(schedule_selector(SpriteLayer::addCloud), 2);
- //添加星星道具
- this->schedule(schedule_selector(SpriteLayer::addStar), 4);
- //创建玩家飞机
- m_plane = Plane::createPlane(0);
- this->addChild(m_plane);
- m_plane->setPosition(SCREEN_POSITION(0.5, 0.2));
- //创建敌机
- this->schedule(schedule_selector(SpriteLayer::addEnemy), 2);
- //开启触摸
- this->setTouchEnabled(true);
- //设置触摸模式
- this->setTouchMode(kCCTouchesOneByOne);
- return true;
- }
- //添加星星
- void SpriteLayer::addStar() {
- Star *star = Star::create();
- star->setPosition(CCRANDOM_0_1() * SCREEN.width, SCREEN.height + star->getShouContentSize().height * 0.5);
- this->addChild(star);
- m_stars->addObject(star);
- }
步骤四:在精灵层中进行碰撞检测
首先在精灵层的初始化方法中设置定时器scheduleUpdate,进行碰撞检测,代码如下所示:
- bool SpriteLayer::initSpriteLayer(int Level) {
- if(!CCLayer::init())return false;
- //设置随机算子 time(0)当前系统时间
- srand(time(0));
- this->loadRosources();
- m_stars = CCArray::create();
- CC_SAFE_RETAIN(m_stars);
- m_bullets = CCArray::create();
- CC_SAFE_RETAIN(m_bullets);
- m_removebullets = CCArray::create();
- CC_SAFE_RETAIN(m_removebullets);
- m_enemys = CCArray::create();
- CC_SAFE_RETAIN(m_enemys);
- m_buttle_level = 1;
- //创建云池对象
- m_cloudPool = CloudPool::creatPool(5);
- CC_SAFE_RETAIN(m_cloudPool);
- //往精灵层中添加云
- this->schedule(schedule_selector(SpriteLayer::addCloud), 2);
- //创建玩家飞机
- m_plane = Plane::createPlane(0);
- this->addChild(m_plane);
- m_plane->setPosition(SCREEN_POSITION(0.5, 0.2));
- //创建敌机
- this->schedule(schedule_selector(SpriteLayer::addEnemy), 2);
- //开启触摸
- this->setTouchEnabled(true);
- //设置触摸模式
- this->setTouchMode(kCCTouchesOneByOne);
- //设置定时器检测碰撞
- this->scheduleUpdate();
- return true;
- }
其次实现定时器的回调方法Update,Update方法中需要对道具和飞机的碰撞进行检测以及子弹与敌人的碰撞进行检测,代码如下所示:
- void SpriteLayer::update(float time) {
- //检测玩家飞机是否与星星碰撞
- this->planeCollisionStar();
- //检测子弹是否与敌人碰撞
- this->bulletCollisionEnemy();
- //释放子弹
- this->removeBullet();
- }
然后实现飞机和道具的碰撞方法planeCollisionStar,代码如下所示:
- //遍历星星是否与飞机产生碰撞
- void SpriteLayer::planeCollisionStar() {
- for (int i = 0; i < m_stars->count(); i++) {
- Star *star = (Star*)m_stars->objectAtIndex(i);
- //如果星星不是将要移除的才做碰撞检测
- if (!star->getIsWillRemove()) {
- if (star->getCollisionArea().intersectsRect(m_plane->getCollisionArea())) {
- //星星进入将要销毁的状态
- star->willRemove();
- //换子弹
- if (m_buttle_level < 35) {
- m_buttle_level++;
- }
- }
- }
- }
- }
最后实现子弹和敌人的碰撞方法bulletCollisionEnemy,代码如下所示:
- //遍历子弹是否与敌人产生碰撞
- void SpriteLayer::bulletCollisionEnemy() {
- for (int i = 0; i < m_bullets->count(); i ++) {
- Bullet *bullet = (Bullet*)m_bullets->objectAtIndex(i);
- for (int j = 0; j < m_enemys->count(); j++) {
- Enemy *enemy = (Enemy*)m_enemys->objectAtIndex(j);
- //isRunning() 如果该节点在父节点中显示 return true
- if (bullet->isRunning()) {
- //判断子弹是否打中了敌人
- if (bullet->getCollisionArea().intersectsRect(enemy->getCollisionArea())) {
- //敌人伤害
- enemy->hurt(bullet->getAttack());
- //子弹销毁
- bullet->remove();
- }
- }
- }
- }
- }
本案例中,SpriteLayer.h文件中的完整代码如下所示:
- #ifndef __Plane__SpriteLayer__
- #define __Plane__SpriteLayer__
- #include<stdio.h>
- #include "cocos2d.h"
- #include "Plane.h"
- #include "CloudPool.h"
- #include "Bullet.h"
- #include "Star.h"
- #include "Enemy.h"
- USING_NS_CC;
- class SpriteLayer : public CCLayer {
- public:
- ~SpriteLayer();
- static SpriteLayer* createSpriteLayer(int Level);
- //发射子弹
- void shootBullet();
- //保存星星的数组
- CC_SYNTHESIZE_READONLY(CCArray*, m_stars, Stars);
- //保存子弹的数组
- CC_SYNTHESIZE_READONLY(CCArray*, m_bullets, Bullets);
- //将要移除子弹的数组
- CC_SYNTHESIZE_READONLY(CCArray*, m_removebullets, RemoveBullets);
- //保存敌人的数组
- CC_SYNTHESIZE_READONLY(CCArray*, m_enemys, Enemys);
- private:
- int m_buttle_level;
- //加载资源 (加载plist)
- void loadRosources();
- Plane *m_plane;
- CloudPool *m_cloudPool;
- bool initSpriteLayer(int Level);
- //添加云
- void addCloud();
- //添加星星
- void addStar();
- //添加敌人
- void addEnemy();
- //遍历星星是否与飞机产生碰撞
- void planeCollisionStar();
- //遍历子弹是否与敌人产生碰撞
- void bulletCollisionEnemy();
- CCPoint m_tempTouchPos;
- virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
- virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
- virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
- void update(float time);
- //移除不需要的子弹
- void removeBullet();
- };
- #endif /* defined(__Plane__SpriteLayer__) */
隐藏
本案例中,SpriteLayer.cpp文件中的完整代码如下所示:
- #include "SpriteLayer.h"
- #include "Tools.h"
- #include "Cloud.h"
- SpriteLayer::~SpriteLayer() {
- CC_SAFE_RELEASE(m_cloudPool);
- CC_SAFE_RELEASE(m_stars);
- CC_SAFE_RELEASE(m_bullets);
- CC_SAFE_RELEASE(m_removebullets);
- CC_SAFE_RELEASE(m_enemys);
- }
- SpriteLayer* SpriteLayer::createSpriteLayer(int Level){
- SpriteLayer *layer = new SpriteLayer;
- if (layer && layer->initSpriteLayer(Level)) {
- layer->autorelease();
- return layer;
- }
- CC_SAFE_DELETE(layer);
- return NULL;
- }
- bool SpriteLayer::initSpriteLayer(int Level) {
- if(!CCLayer::init())return false;
- //设置随机算子 time(0)当前系统时间
- srand(time(0));
- this->loadRosources();
- m_stars = CCArray::create();
- CC_SAFE_RETAIN(m_stars);
- m_bullets = CCArray::create();
- CC_SAFE_RETAIN(m_bullets);
- m_removebullets = CCArray::create();
- CC_SAFE_RETAIN(m_removebullets);
- m_enemys = CCArray::create();
- CC_SAFE_RETAIN(m_enemys);
- m_buttle_level = 1;
- //创建云池对象
- m_cloudPool = CloudPool::creatPool(5);
- CC_SAFE_RETAIN(m_cloudPool);
- //往精灵层中添加云
- this->schedule(schedule_selector(SpriteLayer::addCloud), 2);
- //添加星星道具
- this->schedule(schedule_selector(SpriteLayer::addStar), 4);
- //创建玩家飞机
- m_plane = Plane::createPlane(0);
- this->addChild(m_plane);
- m_plane->setPosition(SCREEN_POSITION(0.5, 0.2));
- //创建敌机
- this->schedule(schedule_selector(SpriteLayer::addEnemy), 2);
- //开启触摸
- this->setTouchEnabled(true);
- //设置触摸模式
- this->setTouchMode(kCCTouchesOneByOne);
- this->scheduleUpdate();
- return true;
- }
- void SpriteLayer::loadRosources() {
- CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Game/sp_all.plist");
- CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Game/bullet_lv.plist");
- CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Game/effect0.plist");
- }
- void SpriteLayer::addCloud() {
- Cloud *cloud = m_cloudPool->getCloud();
- if (cloud) {
- this->addChild(cloud);
- }
- }
- //添加星星
- void SpriteLayer::addStar() {
- Star *star = Star::create();
- star->setPosition(CCRANDOM_0_1() * SCREEN.width, SCREEN.height + star->getShouContentSize().height * 0.5);
- this->addChild(star);
- m_stars->addObject(star);
- }
- //添加敌人
- void SpriteLayer::addEnemy() {
- int type = rand() % TYPE_COUNT;
- float speed = rand() % 5 + 3;
- Enemy *enemy = Enemy::createEnemy((EnemyType)type, speed);
- enemy->setPosition(CCRANDOM_0_1() * SCREEN.width, SCREEN.height + enemy->getShowContentSize().height * 0.5);
- this->addChild(enemy);
- m_enemys->addObject(enemy);
- }
- bool SpriteLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
- m_tempTouchPos = pTouch->getLocation();
- return true;
- }
- void SpriteLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {
- // pTouch->getLocation();
- // pTouch->getLocationInView();
- CCPoint movePoint = pTouch->getLocation() - m_tempTouchPos;
- m_plane->setPosition(m_plane->getPosition() + movePoint);
- m_tempTouchPos = pTouch->getLocation();
- }
- void SpriteLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {}
- //发射子弹
- void SpriteLayer::shootBullet() {
- Bullet *bullet = Bullet::createBullet(m_plane->getPosition(), m_buttle_level);
- this->addChild(bullet, -1);
- m_bullets->addObject(bullet);
- }
- //遍历星星是否与飞机产生碰撞
- void SpriteLayer::planeCollisionStar() {
- for (int i = 0; i < m_stars->count(); i++) {
- Star *star = (Star*)m_stars->objectAtIndex(i);
- //如果星星 不是将要移除的 才做碰撞检测
- if (!star->getIsWillRemove()) {
- if (star->getCollisionArea().intersectsRect(m_plane->getCollisionArea())) {
- //星星进入将要销毁的状态
- star->willRemove();
- //换子弹
- if (m_buttle_level < 35) {
- m_buttle_level++;
- }
- }
- }
- }
- }
- //遍历子弹是否与敌人产生碰撞
- void SpriteLayer::bulletCollisionEnemy() {
- for (int i = 0; i < m_bullets->count(); i ++) {
- Bullet *bullet = (Bullet*)m_bullets->objectAtIndex(i);
- for (int j = 0; j < m_enemys->count(); j++) {
- Enemy *enemy = (Enemy*)m_enemys->objectAtIndex(j);
- //isRunning() 如果该节点在父节点中显示 return true
- if (bullet->isRunning()) {
- //判断子弹是否打中了敌人
- if (bullet->getCollisionArea().intersectsRect(enemy->getCollisionArea())) {
- //敌人伤害
- enemy->hurt(bullet->getAttack());
- //子弹销毁
- bullet->remove();
- }
- }
- }
- }
- }
- void SpriteLayer::update(float time) {
- //检测玩家飞机是否与星星碰撞
- this->planeCollisionStar();
- //检测子弹是否与敌人碰撞
- this->bulletCollisionEnemy();
- //释放子弹
- this->removeBullet();
- }
- //移除不需要的子弹
- void SpriteLayer::removeBullet() {
- for (int i = 0; i < m_removebullets->count(); i++) {
- Bullet *removeBullet = (Bullet*)m_removebullets->objectAtIndex(i);
- m_bullets->removeObject(removeBullet);
- }
- m_removebullets->removeAllObjects();
- }
隐藏
本案例中,Enemy.h文件中的完整代码如下所示:
- #ifndef __Plane__Enemy__
- #define __Plane__Enemy__
- #include<stdio.h>
- #include "cocos2d.h"
- USING_NS_CC;
- typedef enum {
- TYPE_1,
- TYPE_2,
- TYPE_3,
- TYPE_4,
- TYPE_COUNT
- }EnemyType;
- class Enemy : public CCNode {
- public:
- static Enemy* createEnemy(EnemyType type, float m_speed);
- CCSize getShowContentSize();
- //获取敌人的碰撞区域
- CCRect getCollisionArea();
- //伤害
- void hurt(int hurtnum);
- private:
- int m_hp;
- float m_speed;
- CCSprite *m_showSprite;
- bool initEnemy(EnemyType type, float m_speed);
- void move();
- void remove();
- };
- #endif /* defined(__Plane__Enemy__) */
隐藏
本案例中,Enemy.cpp文件中的完整代码如下所示:
- #include "Enemy.h"
- #include "SpriteLayer.h"
- Enemy* Enemy::createEnemy(EnemyType type, float speed) {
- Enemy *enemy = new Enemy;
- if (enemy && enemy->initEnemy(type, speed)) {
- enemy->autorelease();
- return enemy;
- }
- CC_SAFE_DELETE(enemy);
- return NULL;
- }
- bool Enemy::initEnemy(EnemyType type, float speed) {
- if(!CCNode::init()) return false;
- const char* imageName;
- switch ((int)type) {
- case TYPE_1:
- imageName = "enem1.png";
- m_hp = 5;
- break;
- case TYPE_2:
- imageName = "enem2.png";
- m_hp = 10;
- break;
- case TYPE_3:
- imageName = "enem3.png";
- m_hp = 15;
- break;
- case TYPE_4:
- imageName = "enem4.png";
- m_hp = 20;
- break;
- }
- m_showSprite = CCSprite::create(imageName);
- this->addChild(m_showSprite);
- m_speed = speed;
- this->schedule(schedule_selector(Enemy::move));
- return true;
- }
- void Enemy::move() {
- this->setPositionY(this->getPositionY() - m_speed);
- if (this->getPositionY() <= - m_showSprite->getContentSize().height * 0.5) {
- this->remove();
- }
- }
- CCSize Enemy::getShowContentSize() {
- return m_showSprite->getContentSize();
- }
- //获取敌人的碰撞区域
- CCRect Enemy::getCollisionArea() {
- CCRect rect = m_showSprite->boundingBox();
- //飞机节点坐标系下 矩形原点位置
- CCPoint origin_sprite = rect.origin;
- //屏幕坐标系下的原点位置(由于精灵层没有改变过位置,所以现在精灵层的坐标系和屏幕坐标系是吻合)
- CCPoint origin = this->convertToWorldSpace(origin_sprite);
- return CCRectMake(origin.x, origin.y, rect.size.width, rect.size.height);
- }
- //伤害
- void Enemy::hurt(int hurtnum) {
- m_hp -= hurtnum;
- if (m_hp <= 0) {
- CCParticleSystemQuad *particle = CCParticleSystemQuad::create("particle_boom.plist");
- SpriteLayer *layer = (SpriteLayer*)this->getParent();
- layer->addChild(particle);
- particle->setPosition(this->getPosition());
- //粒子生命结束后自动释放
- particle->setAutoRemoveOnFinish(true);
- this->remove();
- }else {
- m_showSprite->stopActionByTag(100);
- CCBlink *blink = CCBlink::create(0.8, 3);
- blink->setTag(100);
- m_showSprite->runAction(blink);
- }
- }
- void Enemy::remove() {
- SpriteLayer *layer = (SpriteLayer*)this->getParent();
- layer->getEnemys()->removeObject(this);
- this->removeFromParent();
- }
隐藏
本案例中,Star.h文件中的完整代码如下所示:
- #ifndef __Plane__Star__
- #define __Plane__Star__
- #include<stdio.h>
- #include "cocos2d.h"
- USING_NS_CC;
- class Star : public CCNode {
- public:
- CREATE_FUNC(Star);
- CCSize getShouContentSize();
- //获取星星的碰撞区域
- CCRect getCollisionArea();
- //将要移除的方法 (做动画)
- void willRemove();
- //是否将要移除
- CC_SYNTHESIZE(bool, m_isWillRemove, IsWillRemove);
- private:
- CCSprite *m_showSprite;
- float m_speed;
- bool init();
- void move();
- void remove();
- };
- #endif /* defined(__Plane__Star__) */
隐藏
本案例中,Star.cpp文件中的完整代码如下所示:
- #include "Star.h"
- #include "SpriteLayer.h"
- bool Star::init() {
- if (!CCNode::init()) return false;
- m_speed = 10;
- m_isWillRemove = false;
- m_showSprite = CCSprite::create("star.png");
- this->addChild(m_showSprite);
- this->schedule(schedule_selector(Star::move));
- return true;
- }
- void Star::move() {
- this->setPositionY(this->getPositionY() - m_speed);
- if (this->getPositionY() <= -m_showSprite->getContentSize().height * 0.5) {
- this->remove();
- }
- }
- CCSize Star::getShouContentSize() {
- return m_showSprite->getContentSize();
- }
- //将要移除的方法 (做动画)
- void Star::willRemove() {
- m_isWillRemove = true;
- //停止星星的移动
- this->unschedule(schedule_selector(Star::move));
- CCAnimation *animation = CCAnimation::create();
- //
- for (int i = 0; i < 4; i++) {
- CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("eat_item%d.png", i)->getCString());
- animation->addSpriteFrame(frame);
- }
- animation->setDelayPerUnit(0.05);
- CCAnimate *animate = CCAnimate::create(animation);
- CCCallFunc *callFunc =CCCallFunc::create(this, callfunc_selector(Star::remove));
- CCSequence *seq = CCSequence::create(animate, callFunc,NULL);
- m_showSprite->runAction(seq);
- }
- //移除
- void Star::remove(){
- SpriteLayer *layer = (SpriteLayer*)this->getParent();
- layer->getStars()->removeObject(this);
- this->removeFromParent();
- }
- //获取星星的碰撞区域
- CCRect Star::getCollisionArea() {
- CCRect rect = m_showSprite->boundingBox();
- //星星节点坐标系下 矩形原点位置
- CCPoint origin_sprite = rect.origin;
- //屏幕坐标系下的原点位置(由于精灵层没有改变过位置,所以现在精灵层的坐标系和屏幕坐标系是吻合)
- CCPoint origin = this->convertToWorldSpace(origin_sprite);
- return CCRectMake(origin.x, origin.y, rect.size.width, rect.size.height);
- }