上次做了主角对象的加入今天要做的就是加入敌人,在每个游戏中都有敌人这个重要的游戏对象,没错我们的游戏中也需要这样的游戏对象,这样能增加游戏的趣味性,试想一下如果没有敌人,那游戏还有什么意义对吧。
游戏效果
好了下面直接来看看游戏中,敌人对象的代码
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;
}
对了加入敌人对象之后,的在游戏世界类里面加入自动产生敌人的方法,和存储敌人对象的数组,而在地图对象中得让敌人对象跟着地图一起上下运动,这样休息就看起来比较不错了。
在下次更新的时候我将加入主角的武器对象,敌人有了,主角当然也得有武器啦,下车更新不忙的话,在下周的时候进行更新,整个游戏大概还有两章的样子,希望大家继续努力,一起加油。