添加不同的怪物,这个是我从其他地方实现的。
添加Monster.h和Monster.cpp
开始的时候我是继承的CCSprite,但是一直出问题。在网上发现可以继承CCNode,就尝试了一下。
1 // 2 // Monster.h 3 // cocos2d-x_simpleGame_20131104 4 // 5 // Created by 张学院 on 14-1-13. 6 // 7 // 8 9 #ifndef __cocos2d_x_simpleGame_20131104__Monster__10 #define __cocos2d_x_simpleGame_20131104__Monster__11 12 #include13 #include "cocos2d.h"14 class Monster:public cocos2d::CCNode{15 public :16 int _curHp;17 int minMove;18 int maxMove;19 cocos2d::CCSprite * m_sprite;20 public :21 Monster();22 ~Monster();23 void bindSprite(cocos2d::CCSprite * sprite);24 25 int gethp();26 void sethp(int hp);27 int getminMove();28 void setminMove(int minMove);29 int getmaxMove();30 void setmaxMove(int maxMove);31 32 };33 class WeakAndFastMoveMonster:public Monster{34 35 public:36 static WeakAndFastMoveMonster * monster();37 };38 class StrongAndSlowMonster:public Monster{39 public :40 41 static StrongAndSlowMonster * monster();42 43 44 };45 #endif /* defined(__cocos2d_x_simpleGame_20131104__Monster__) */
1 // 2 // Monster.cpp 3 // cocos2d-x_simpleGame_20131104 4 // 5 // Created by 张学院 on 14-1-13. 6 // 7 // 8 9 #include "Monster.h"10 Monster::Monster(){11 _curHp=0;12 minMove=0;13 maxMove=0;14 m_sprite=NULL;15 }16 17 Monster::~Monster(){18 19 }20 void Monster::bindSprite(cocos2d::CCSprite * sprite){21 m_sprite=sprite;22 this->addChild(m_sprite);23 }24 void Monster::sethp(int hp){25 this->_curHp=hp;26 27 }28 void Monster::setmaxMove(int maxMove){29 this->maxMove=maxMove;30 31 }32 void Monster::setminMove(int minMove){33 this->minMove=minMove;34 35 }36 37 int Monster::gethp(){ return _curHp;38 }39 40 int Monster::getmaxMove(){ return maxMove;}41 int Monster::getminMove(){ return minMove;}42 43 WeakAndFastMoveMonster * WeakAndFastMoveMonster::monster(){44 WeakAndFastMoveMonster * target= new WeakAndFastMoveMonster();45 46 target->bindSprite(cocos2d::CCSprite::create("Target.png"));47 48 target->sethp(1);49 target->setmaxMove(5);50 target->setminMove(3);51 52 53 54 return target;55 }56 StrongAndSlowMonster * StrongAndSlowMonster::monster(){57 StrongAndSlowMonster * target= new StrongAndSlowMonster();58 59 target->bindSprite(cocos2d::CCSprite::create("Target2.png"));60 target->sethp(3);61 target->setmaxMove(6);62 target->setminMove(12);63 64 65 return target;66 }
修改HelloWorldScene.cpp 中的addTarget方法
1 void HelloWorld::addTarget(){ 2 3 //CCSprite *target= CCSprite::create("Target.png",CCRectMake(0, 0, 27, 40) ); 4 //Monster *monster; 5 Monster *target; 6 7 if((rand()%2==0)){ 8 target=WeakAndFastMoveMonster::monster(); 9 }else{10 target=StrongAndSlowMonster::monster();11 }12 13 target->setTag(1);14 _targets->addObject(target);15 16 CCSize winSize = CCDirector::sharedDirector()->getWinSize();17 18 19 int minY= target ->getContentSize().height/2;20 int maxY=winSize.height-target->getContentSize().height/2;21 int rangeY= maxY-minY;22 //随机生成y坐标23 int actualY = (rand() %rangeY)+minY;24 target->setPosition(ccp(winSize.width+(target->getContentSize().width/2),actualY));25 this->addChild(target);26 27 int minDuration = target->getminMove();28 int maxDuration = target->getmaxMove();29 // Determine speed of the target30 // int minDuration = (int)3.0;31 // int maxDuration = (int)5.0;32 int rangeDuration = maxDuration - minDuration;33 // srand( TimGetTicks() );34 //随机生产移动速度。35 int actualDuration = ( rand() % rangeDuration )36 + minDuration;37 38 // Create the actions39 40 CCFiniteTimeAction* actionMove =41 CCMoveTo::create( (float)actualDuration,42 ccp(0 - target->getContentSize().width/2, actualY) );43 // 增加回掉函数,收回内存44 CCFiniteTimeAction* actionMoveDone =45 CCCallFuncN::create( this,46 callfuncN_selector(HelloWorld::spriteMoveFinished));47 48 CCLOG("before runAction");49 //target执行动画50 target->runAction( CCSequence::create(actionMove,actionMoveDone, NULL) );51 52 CCLOG("after runAction");53 54 }
修改碰撞函数update方法
1 void HelloWorld::update(float dt){ 2 monsterHit=false; 3 CCArray *projectilesToDelete = new CCArray; 4 CCArray* targetsToDelete =new CCArray; 5 CCObject* it = NULL; 6 CCObject* jt = NULL; 7 8 CCARRAY_FOREACH(_projectiles, it) 9 {10 CCSprite *projectile = dynamic_cast(it);11 12 CCRect projectileRect = CCRectMake(13 projectile->getPosition().x - (projectile->getContentSize().width/2),14 projectile->getPosition().y - (projectile->getContentSize().height/2),15 projectile->getContentSize().width,16 projectile->getContentSize().height);17 18 CCARRAY_FOREACH(_targets, jt)19 {20 Monster *target = dynamic_cast (jt);21 CCRect targetRect = CCRectMake(22 target->getPosition().x - (target->getContentSize().width/2),23 target->getPosition().y - (target->getContentSize().height/2),24 target->getContentSize().width,25 target->getContentSize().height);26 27 if (projectileRect.intersectsRect(targetRect))28 {29 // targetsToDelete->addObject(target);30 monsterHit=true;31 target->_curHp--;32 if(target->gethp()<=0){33 targetsToDelete->addObject(target);34 }35 36 projectilesToDelete->addObject(projectile);37 break;38 }39 }40 }41 42 CCARRAY_FOREACH(targetsToDelete, jt)43 {44 Monster *target = dynamic_cast (jt);45 46 _targets->removeObject(target);47 _projectilesDestroyed++;48 if (_projectilesDestroyed >= 20)49 {50 GameOverScene *gameOverScene = GameOverScene::create();51 gameOverScene->getLayer()->getLabel()->setString("You Win!");52 CCDirector::sharedDirector()->replaceScene(gameOverScene);53 }54 this->removeChild(target, true);55 }56 57 CCARRAY_FOREACH(projectilesToDelete, it)58 {59 60 CCSprite* projectile = dynamic_cast (it);61 _projectiles->removeObject(projectile);62 this->removeChild(projectile, true);63 64 }65 66 projectilesToDelete->release();67 targetsToDelete->release();68 }
对了,在HelloWorldScene.cpp中添加了一个bool monsterHit.