战斗:
1.触摸层:
_hotZone = LayerColor::create(Color4B(128,128,128)); _hotZone->setPosition(Vec2(600,0)); //由于本游戏要求子弹不能向后射,所以位置设置在这 _hotZone->setTouchEnabled(true); _hotZone->setContentSize(visibleSize); addChild(_hotZone,0);2单点触摸
/**单点触摸**/ EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(Battle::onPress,this); listener->onTouchMoved = CC_CALLBACK_2(Battle::onMove,this); listener->onTouchEnded = CC_CALLBACK_2(Battle::onRelease,this); _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this); _eventDispatcher->setPriority(listener,1);//第二个参数是优先级,数值越小优先级越高3. 事件侦听
EventListenerCustom* bossShootListener = EventListenerCustom::create(BOSS_SHOOT,CC_CALLBACK_1(Battle::bossShoot,this)); _eventDispatcher->addEventListenerWithFixedPriority(bossShootListener,1);4. 碰撞
1).子弹与士兵的碰撞
版本1:
Vector<Bullet*>::iterator itb = _bulletVec.begin(); while (itb != _bulletVec.end()) { Bullet* itbullet = (*itb); Vector<Soldier*>::iterator its = _soldierVec.begin(); bool flag = false; while (its != _soldierVec.end()) { Soldier* itsoldier = (*its); if (itsoldier->hit(itbullet->getPosition())) { <span style="white-space:pre"> </span>flag = true; int nowlife = itsoldier->hurt(); if (nowlife <= 0) { itsoldier->die(); its = _soldierVec.erase(its); } itbullet->resetBullet(); itb = _bulletVec.erase(itb); break; } else { its++; } } if (!flag) { ++itb; } }版本2:
for (Node* a : _bulletVec) { Bullet* bullet = (Bullet*)a; Rect bulletRect = bullet->getRect(); for (Node* b : _soldierVec) { Soldier* soldier = (Soldier*)b; Rect soldierRect = soldier->getRect(); if (bulletRect.intersectsRect(soldierRect)) { if (!bullet->getHurtObj()) { if (soldier->hurt(bullet->getDamage()))//在hurt函数中判断是否死亡 { bullet->setHurtObj(soldier); _soldierVec.eraSEObject(soldier); return; } bullet->setHurtObj(soldier); } else { if (bullet->getHurtObj() != soldier) { if (soldier->hurt(bullet->getDamage())) { _soldierVec.eraSEObject(soldier); } bulletBoom(bullet); //爆炸效果 return; } } } } }2)子弹和boss碰撞(与上述的类似)
for (Node* a : _bulletVec) { Bullet* bullet = (Bullet*)a; Rect bulletRect = bullet->getRect(); for (Node* b : _bossVec) { Boss* boss = (Boss*)b; Rect bossRect = boss->getRect(); if (bulletRect.intersectsRect(bossRect)) { if (boss->hurt(bullet->getDamage())) { _bossVec.eraSEObject(boss); } bulletBoom(bullet); _bulletVec.eraSEObject(bullet); return; } } }3)地方子弹与武将碰撞
for (Node* a : _enemyBulletVec) { Bullet* bullet = (Bullet*)a; Rect bulletRect = bullet->getRect(); Rect bossRect = _general->getRect(); if (bulletRect.intersectsRect(bossRect)) { _general->hurt(bullet->getDamage()); bulletBoom(bullet); _enemyBulletVec.eraSEObject(bullet); return; } }