cocos2d-x 3.0游戏开发xcode5帅印博客教学 005.[HoldTail]敌人的创建和敌人与主角的碰撞

上次做了主角对象的加入今天要做的就是加入敌人,在每个游戏中都有敌人这个重要的游戏对象,没错我们的游戏中也需要这样的游戏对象,这样能增加游戏的趣味性,试想一下如果没有敌人,那游戏还有什么意义对吧。

游戏效果






好了下面直接来看看游戏中,敌人对象的代码

SWGameEnemyPIG.h 和 SWGameEnemyPIG.cpp

SWGameEnemyPIG.h

//
//  SWGameEnemyPIG.h
//  Holdtail
//
//  Created by 帅 印 on 13-12-14.
//
//

#ifndef __Holdtail__SWGameEnemyPIG__
#define __Holdtail__SWGameEnemyPIG__

#include <iostream>
#include "cocos2d.h"

using namespace cocos2d;

class SWGameEnemyPIG :public cocos2d::Sprite{
public:
    //实例化函数  敌人图片  敌人类型
    static SWGameEnemyPIG *createEnemy(const char* fileName,float _enType);
    //敌人强度
    int scoreValue;
    //自定义矩形碰撞检测
    bool isRectCollision(Rect rect1,Rect rect2);
    
private:
    void enemyInit(const char *fileName,float _enType);
    //敌人动态表现
    void createAnimate(const char *fileName,int allCount);
    //敌人逻辑
    void update(float time);
    //是否已经运动
    bool isActed;
    
};
#endif /* defined(__Holdtail__SWGameEnemyPIG04__) */

SWGameEnemyPIG.cpp

//
//  SWGameEnemyPIG.cpp
//  Holdtail
//
//  Created by 帅 印 on 13-12-14.
//
//

#include "SWGameEnemyPIG.h"
#include "SWGameMap.h"
#include "SWGameWorld.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;
using namespace cocos2d;

SWGameEnemyPIG *SWGameEnemyPIG::createEnemy(const char * fileName,float _enType){
    SWGameEnemyPIG *enemy = new SWGameEnemyPIG();
    if(enemy && enemy->initWithFile(fileName)){
        enemy->autorelease();
        enemy->enemyInit(fileName,_enType);
        return enemy;
    }
    
    CC_SAFE_DELETE(enemy);
    return NULL;
}

//初始化
void SWGameEnemyPIG::enemyInit(const char *fileName,float _enType){
    
    //判断敌人的类型
    if(1 == _enType){
        //设置敌人强度
        scoreValue = 100;
        createAnimate(fileName, 3);
        Size size = CCDirector::getInstance()->getWinSize();
        //获取跟地图相对应的位置
        this->setPosition(Point(size.width+200,CCRANDOM_0_1()*size.height));
        //敌人逻辑
        this->scheduleUpdate();
    }
}

//所有敌怪的动态效果
void SWGameEnemyPIG::createAnimate(const char *fileName, int allCount){
    Animation *animation = Animation::create();
    Texture2D *texture = TextureCache::sharedTextureCache()->addImage(fileName);
    int eachWidth = this->getContentSize().width/allCount;
    for (int i = 0; i<allCount; i++) {
        animation->addSpriteFrameWithTexture(texture, Rect::Rect(i*eachWidth, 0, eachWidth, this->getContentSize().height));
    }
    animation->setDelayPerUnit(0.03f);//必须设置否则不会动态播放
    animation->setRestoreOriginalFrame(true);//是否回到第一个
    animation->setLoops(-1);//重复次数
    FiniteTimeAction *animate = Animate::create(animation);
    this->runAction(animate);
    
}

//移动逻辑
void SWGameEnemyPIG::update(float time){
    if(isActed){
        //删除当离开屏幕后
        //if(this->getPositionX()<-this->getContentSize().width){
        if(this->getPositionX()<0){
            //log("SSSSS");
            SWGameWorld::sharedWorld()->getArrayForEnemy()->removeObject(this);
            //从游戏中删除精灵
            SWGameWorld::sharedWorld()->removeChild(this);
        }
    }else{
        isActed = true;
        int speed = CCRANDOM_0_1()*10;
        if (speed <=3) {
            speed = speed+2;
        }
        
        Action* moveByup = MoveBy::create(speed, Point(-1500, 0));
        this->runAction(moveByup);
    }
    
    SWGamePlayer *player = SWGameWorld::sharedWorld()->getPlayer();
    //自定义矩形框判断是否碰撞
    if (this->isRectCollision(
                              Rect(player->getPositionX(),
                                         player->getPositionY(),
                                         player->getContentSize().width,
                                         player->getContentSize().height),
                              Rect(this->getPositionX()+this->getContentSize().width*0.5,
                                         this->getPositionY()+this->getContentSize().height*0.5,
                                         this->getContentSize().width*0.5,
                                         this->getContentSize().height*0.5))) {
                                  
        //添加爆炸粒子效果
        //爆炸粒子效果
        ParticleSystemQuad *particle = ParticleSystemQuad::create("particle_boom.plist");
        particle->setPosition(this->getPosition());
        particle->setAutoRemoveOnFinish(true);
        SWGameWorld::sharedWorld()->addChild(particle);
                                  
        //从数组中移除这个怪物
        SWGameWorld::sharedWorld()->getArrayForEnemy()->removeObject(this);
                                  
        //删除屏幕上面的敌怪
        SWGameWorld::sharedWorld()->removeChild(this, true);
                                  
        player->downHp(scoreValue);
    }
}

//AABB碰撞检测
bool SWGameEnemyPIG::isRectCollision(Rect rect1,Rect rect2){
    float x1 = rect1.origin.x;
    float y1 = rect1.origin.y;
    float w1 = rect1.size.width;
    float h1 = rect1.size.height;
    float x2 = rect2.origin.x;
    float y2 = rect2.origin.y;
    float w2 = rect2.size.width;
    float h2 = rect2.size.height;
    if(x1+w1*0.5<x2-w2*0.5){
        return false;
        
    }else if(x1-w1*0.5>x2+w2*0.5){
        return false;
        
    }else if(y1+h1*0.5<y2-h2*0.5){
        return false;
        
    }else if(y1-h1*0.5>y2+h2*0.5){
        return false;
        
    }
    return true;
}

对了加入敌人对象之后,的在游戏世界类里面加入自动产生敌人的方法,和存储敌人对象的数组,而在地图对象中得让敌人对象跟着地图一起上下运动,这样休息就看起来比较不错了。

在下次更新的时候我将加入主角的武器对象,敌人有了,主角当然也得有武器啦,下车更新不忙的话,在下周的时候进行更新,整个游戏大概还有两章的样子,希望大家继续努力,一起加油。



飞机直达源码下载

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shuaiyinoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值