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

Accelerometer Input

SpaceShipControl

To control the player spaceship we will use accelerometer as the input mechanism. Since our game uses default (portrait) orientation, we will use the X-acceleration value to move the spaceship left or right based on the tilt of the device. Cocos2d-x isolates platform dependent accelerometer code which makes it fairly easy to handle accelerometer input.

6. In HelloWorldScene.h:

(a) Define the following constant for adjusting accelerometer input (I came up with this value through experimentation so feel free to play with it):

#define ACC_ADJUST_FACTOR 8.0f //Accelerometer Adjustment

(b) Under the public declarations add:

//Handle Accelerometer Input
virtual void didAccelerate(cocos2d::CCAcceleration* pAccelerationValue);

(c) Under the private declarations add:

//Accelerometer Values
double _aX;
double _aY;
double _aZ;

7. In HelloWorldScene.cpp:

(a) Under HelloWorld::init(), add before the schedule updateGame call:

this->setAccelerometerEnabled(true);

(b) Add the new method HelloWorld::didAccelerate(CCAcceleration *pAccelerationValue):

void HelloWorld::didAccelerate(CCAcceleration *pAccelerationValue) {
	_aX = pAccelerationValue->x;
	_aY = pAccelerationValue->y;
	_aZ = pAccelerationValue->z;

}

(c) Under HelloWorld::updateGame(float dt), add to the bottom after the background scrolling code:

    //Update player position based on accelerometer values
        CCPoint location;
        location = ccp(_player->getPositionX()+_aX * ACC_ADJUST_FACTOR,_player->getPositionY());

        //Bound the ship location to size of the screen width so we don't go off the screen
        if (location.x> (_origin.x + _player->getContentSize().width/2 * _player->getScaleX()) && location.x < (_winWidth - _player->getScaleX()*_player->getContentSize().width/2)){
            _player->setPosition(location);

        }

To handle the accelerometer input, we first added the call to setAccelerometerEnabled(…) method which tells Cocos2d-x to enable accelerometer input for our game so we can start receiving accelerometer updates. Next, we implemented the didAccelerate(…) method to handle accelerometer updates and store the accelerometer data. Finally, we used the X-acceleration value to update the position of the player spaceship in the updateGame(…) method. You might be wondering about the ACC_ADJUST_FACTOR and why we used this. There are numerous ways to optimize accelerometer input. To keep things simple here we are just using the X-acceleration values and multiplying the adjustment factor to optimize for responsiveness. This is just a value I came up with through experimentation and you can tweak it to your liking. However, note that even though this approach simplifies our implementation it won’t work in all cases. Since we are not taking Y and Z acceleration values into account this might not work for specific cases such as user lying down with device face down. We will look into optimizing this further in a future post.

Build/Deploy the project and you should be able to control the spaceship by tilting the device.

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