void LayerEnemy::addSmallEnemy(float dt) { Enemy *smallPlane = Enemy::create(); smallPlane->bindEnemySprite(CCSprite::createWithSpriteFrameName("enemy1.png"), SMALL_MAXLIFE); m_psmallArray->addObject(smallPlane); addChild(smallPlane); //! 随机产生0.0~1.0的数 float x = CCRANDOM_0_1()*(winSize.width - smallPlane->getBoundingBox().size.width) + smallPlane->getBoundingBox().size.width /2; float y = winSize.height + smallPlane->boundingBox().size.height / 2; CCPoint smallBirth = ccp(x, y); smallPlane->setPosition(smallBirth); CCMoveTo *to = CCMoveTo::create(3, ccp( smallBirth.x, smallBirth.y - winSize.height - smallPlane->getBoundingBox().size.height /2 )); CCCallFuncN *actionDone = CCCallFuncN::create(this, callfuncN_selector(LayerEnemy::smallEnemyMoveFinishedFCallBack)); CCSequence *sequence = CCSequence::create(to, actionDone, NULL); smallPlane->runAction(sequence); //CCLog("%p", smallPlane); //CCLog("before count = %d", m_psmallArray->count()); }
void RaiseTheDeadSpell::checkCollisionsWithEnemies(const sf::FloatRect* boundingBox) { // this method is overridden to guarantee that the spell only hits once // and that the iterator is not invalidated (we change the enemy vector size on the fly) for (auto& go : *m_enemies) { if (!go->isViewable()) continue; Enemy* enemy = dynamic_cast<Enemy*>(go); if (enemy != nullptr && (enemy->getBoundingBox()->intersects(*boundingBox))) { enemy->onHit(this); break; } } }
void CheckEnemyHits(){ for(int i=0;i < enemies.size(); i++){ hgeRect* enemyBox; Enemy* enemy = enemies.at(i); enemyBox = enemy->getBoundingBox(); for(int j=0; j < players.size(); j++){ hgeRect* playerBox; Player* player = players.at(j); if (!player->getInvulnerable()) { playerBox = player->getBoundingBox(); if (enemyBox->Intersect(playerBox)){ gameActive = false; } } } } }
void FightingLayer::update(float dt) { _playDuration += dt; Rect rect = Rect(0, 0, _ground->getContentSize().width, _ground->getContentSize().height); // Update bullets for (std::vector<Bullet *>::size_type i = 0; i != _bullets->size(); i++) { Bullet *bullet = _bullets->at(i); bullet->update(dt); if (!rect.containsPoint(bullet->getPosition())) { bullet->kill(); _bullets->erase(_bullets->begin() + i); i--; continue; } for (std::vector<Enemy *>::size_type j = 0; j != _enemies->size(); j++) { Enemy *enemy = _enemies->at(j); // Check if bullet hits if (enemy->getBoundingBox().containsPoint(bullet->getPosition())) { _score += 10; // Remove enemy //enemy->removeFromParent(); enemy->kill(); _enemies->erase(_enemies->begin() + j); j--; // REmove bullet bullet->kill(); _bullets->erase(_bullets->begin() + i); i--; updateScore(); break; } } } for (std::vector<Smoke *>::size_type i = 0; i != _smokes->size(); i++) { Smoke *smoke = _smokes->at(i); Point position = smoke->getPosition(); if (position.x < 0) { position.x = 0; smoke->reverse(); } else if (position.x > rect.size.width) { position.x = rect.size.width; smoke->reverse(); } } for (std::vector<Enemy *>::size_type i = 0; i != _enemies->size(); i++) { Enemy *enemy = _enemies->at(i); // Check if outside bool isOutside = (enemy->getDirection() == -1 && enemy->getPosition().x < 0) || (enemy->getDirection() == 1 && enemy->getPosition().x > _ground->getContentSize().width); if (isOutside) { enemy->removeFromParent(); _enemies->erase(_enemies->begin() + i); i--; continue; } // Check if hits player if (_player->getBoundingBox().intersectsRect(enemy->getBoundingBox()) && _player->isAlive()) { _player->kill(); CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("die_sound.wav"); this->gameDidEnd(); break; } } if (_player->isAlive()) { Point playerPosition = _player->getPosition(); if (playerPosition.x < _player->getContentSize().width) { playerPosition.x = _player->getContentSize().width; _player->setPosition(playerPosition); } else if (playerPosition.x > _ground->getContentSize().width -_player->getContentSize().width) { _player->setPositionX(_ground->getContentSize().width -_player->getContentSize().width); } } }