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

Asteroids

Asteroids

Having the ability to shoot is no fun if there is nothing to shoot. So let’s introduce some asteroids.

12. In HelloWorldScene.h:

(a) Define the following constants:

#define NUM_ASTEROIDS 30 //Max Number of Asteroids
#define ASTEROID_SPAWN_INTERVAL 0.2f //Interval delay for creating new Asteroids
#define ASTEROID_SPAWN_START 3.0f //Game time when we start spawning the Asteroids
#define ASTEROID_SPAWN_END 20.0f //Game time when we stop spawning the Asteroids

(b) Under the private declarations add:

//Keep track of current Asteroid
int _asteroidCount;

//Time since last Asteroid spawn
float _asteroidSpawnTime;
//Asteroid sprites
cocos2d::CCSprite* _asteroids[NUM_ASTEROIDS];

//Return a random number between min and max
float randomRange(float min, float max);

13. In HelloWorldScene.cpp
(a) Under HelloWorld::init() add right before the schedule updateGame call:

		//Initalize current Asteroid count and spawn time
		_asteroidSpawnTime = 0;
		_asteroidCount = 0;
		    
		//Initialize Asteroid sprites
		for (int i = 0; i < NUM_ASTEROIDS;++i){      _asteroids[i] = CCSprite::createWithSpriteFrameName("asteroid.png");        _asteroids[i]->setVisible(false);
		 
			//Position off screen at the top
			_asteroids[i]->setPosition(ccp(_winWidth,_winHeight+_asteroids[_asteroidCount]->getScaleY()*_asteroids[_asteroidCount]->getContentSize().height));
			this->addChild(_asteroids[i]);
		}

(b) Implement the HelloWorld::randomRange(float min, float max) method which we will use to randomize Asteroid spawning and shapes:

float HelloWorld::randomRange(float min, float max) {
	float randnum = (float) rand() / (float)RAND_MAX;
	return min + randnum * (max - min);
}

(c) Under HelloWorld::updateGame(float dt), add the following to the bottom of the method:

        //Spawn Asteroids
        _asteroidSpawnTime +=dt;
        if (_asteroidSpawnTime > ASTEROID_SPAWN_INTERVAL && _gameTime>ASTEROID_SPAWN_START && _gameTime<ASTEROID_SPAWN_END) {          
        	_asteroidSpawnTime = 0;             
        	//Scale the asteroid randomly before spawning it            
        	float randnum = randomRange(0.1f,0.25f);            
        	_asteroids[_asteroidCount]->setScaleX(randnum);
        	_asteroids[_asteroidCount]->setScaleY(randnum);
            	
        	//Spawn at a random X position based on the screen width
        	float positionX = randomRange(_asteroids[_asteroidCount]->getContentSize().width/2 * _asteroids[_asteroidCount]->getScaleX(),_winWidth-_asteroids[_asteroidCount]->getContentSize().width/2 * _asteroids[_asteroidCount]->getScaleX());
        	_asteroids[_asteroidCount]->stopAllActions();
        	_asteroids[_asteroidCount]->setPosition(ccp(positionX,(_winHeight+_asteroids[_asteroidCount]->getScaleY()*_asteroids[_asteroidCount]->getContentSize().height)));
        	_asteroids[_asteroidCount]->setVisible(true);
             
        	//Create a random time [2.0,8.0] seconds move action for the asteroid
        	_asteroids[_asteroidCount]->runAction(CCSequence::create(CCMoveTo::create(randomRange(2.0f,8.0f), ccp(_asteroids[_asteroidCount]->getPositionX(),_origin.y-_asteroids[_asteroidCount]->getContentSize().height)),CCCallFuncN::create(this,callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL));
        	++_asteroidCount;
             
        	//If have used up all available sprites reset the count to reuse the sprites
        	if (_asteroidCount>=NUM_ASTEROIDS) {
        		_asteroidCount = 0;
        	}
        }

In the code above, we first define the constants needed to create asteroids. Next we initialize the array of asteroid sprites with the same asteroid asset image. Next we implement the randomRange(…) method which we will be using to randomize the asteroids as well as some other pieces of the game. In the updateGame(…) method we spawn a new asteroid at fixed time interval with a random width/height, position and move action time. This creates an asteroid field consisting of asteroids of different sizes scrolling across the screen at different speeds.

Build/deploy the project and you should be able to see asteroids scrolling across the screen.

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