How to create your own game using Cocos2d-x and BlackBerry Native SDK

Enemy Collision Detection

EnemyCollisionDetectionQ10

There are two types of collisions we need to handle: player mine projectiles with the enemy ship and enemy laser projectiles with the player ship. The implementation is very similar to the asteroid and player collision detection we implemented earlier. So let’s get straight down to the code.

18. In HelloWorldScene.cpp:

(a) Add the following collision detection code to the bottom of HelloWorld::updateGame(float dt) method:

        //Collision detection Player Mines<->Enemy
        for (int j = 0; j < MAX_PLAYER_MINES; ++j) {     //We only need to check collisions if the current Player projectile and Enemy are visible
        	if (_playerMines[j]->isVisible() && _enemy->isVisible()) {
        		if (_playerMines[j]->boundingBox().intersectsRect(_enemy->boundingBox())) {
        			_playerMines[j]->setVisible(false);
        			//Create an exploding ring particle effect for the Mine Explosion
        			createParticleEffect("Particles/ExplodingRing.plist",_playerMines[j]->getPositionX(),_playerMines[j]->getPositionY());
        			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explode.wav");
        			--_enemyHealth;
        			//Create Small Sun particle effect to signify Enemy damage
        			createParticleEffect("Particles/SmallSun.plist",_enemy->getPositionX(),_enemy->getPositionY());
        			if (_enemyHealth <= 0) {                 
                               //Destroy Enemy spaceship with a sound effect and a custom galaxy particle effect               
                                        _enemy->setVisible(false);;
        				ccColor4F startColor = {1.0f, 0.5f, 0.5f, 1.0f};
        				createParticleEffect("Particles/Galaxy.plist",_enemy->getPositionX(),_enemy->getPositionY(),startColor,1.0f,200.0f);
        				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explode_ship.wav");
        			}
        		}
        	}
        }


        //Collision detection Enemy Projectiles<->Player
        for (int j = 0; j < MAX_ENEMY_PROJECTILES; ++j) {    //We only need to check collisions if the current Enemy projectile and Player are visible
        	if (_enemyProjectiles[j]->isVisible() && _player->isVisible()) {
        		if (_enemyProjectiles[j]->boundingBox().intersectsRect(_player->boundingBox())) {
        			_enemyProjectiles[j]->setVisible(false);
        			--_playerHealth;
        			//Create Small Sun particle effect to signify Player damage
        			createParticleEffect("Particles/SmallSun.plist",_player->getPositionX(),_player->getPositionY());
        			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explode.wav");
        			if (_playerHealth <=0) {
        				_player->stopAllActions();
        				_player->setVisible(false);
        				//Destroy Player spaceship with a sound effect and a custom exploding ring particle effect
        				ccColor4F startColor = {0.4f, 0.5f, 1.0f, 1.0f};
        				createParticleEffect("Particles/ExplodingRing.plist",_player->getPositionX(),_player->getPositionY(),startColor,1.0f,100.0f);
        				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explode_ship.wav");
        			} else {
        				//Create a blinking effect signifying Player spaceship damage
        				CCBlink *blinkAction = CCBlink::create(1.0f,10);
        				CCShow *showAction = CCShow::create();
        				CCSequence *action = CCSequence::create(blinkAction,showAction,NULL);
        				_player->runAction(action);
        			}
        		}
        	}
        }

In the code above, we implemented the same collision detection algorithm as we implemented earlier for the asteroids. In the updateGame(…) method we added code to detect collision for player mines with enemy ship and enemy laser projectiles with the player ship.

Build/deploy the project and you should now be able to battle the enemy ship.

You can download the completed source code for this step from here.

One thought on “How to create your own game using Cocos2d-x and BlackBerry Native SDK”

Leave a comment