Samurai Gunship website finally released!

Posted on

Working at Pamprepps Games in Fukuoka Japan is awesome!
Today the official website for the game I developed for Android (iOS is coming soon) has been released!

スクリーンショット 2015-09-14 11.45.37

Link →


Samurai Gunship – A retro flavour 2D casual shooter game!

Posted on Updated on

At long last I am ready to unveil a game I have been developing with Pamprepps Games in Japan for a few months. Thanks to my friend Lee Adams (a good friend from all the way back in the day when we used to play Beyblade and talk about Sonic the hedgehog) for composing the BGM!

An iOS is being released soon. Till then stay tuned!

DL link:

スクリーンショット 2015-09-02 14.17.38

◆◇◆Samurai Gunship◆◇◆
A simple retro flavour shooter game.
Destroy incoming enemy aliens to score points.

◆◇◆How to play◆◇◆
To destroy enemy targets, set your gunship into position by holding down the left or right arrow buttons. The fuel you carry doesn’t last forever, however you can extend the time you have left by collecting points and by moving through each wave.

“signal 11 (SIGSEGV), code 1” – (Cocos2dx, C++ with Android & Eclipse)

Posted on Updated on

The biggest drawback I’ve been having with Eclipse and cocos2dx (not sure if this also applies to Xcode …) I can think of is the annoying “Signal 11” error. But then I ran into this thread on StackOverFlow:

I find the perfect answer in

The solution is:

When you get errors like this on the LogCat, they are saved on $PROJECT_PATH/obj/local/armeabi where $PROJECT_PATH is the path to your cocos2d-x android project. To symbolicate the messages to something understandable, you can use the ndk-stack tool.

Open up the Terminal (or Cygwin, not sure though) and type in

adb logcat | ./ndk-stack -sym $PROJECT_PATH/obj/local/armeabi


$ANDROID_NDK is the path to your android NDK

$PROJECT_PATH is the path to your cocos2d-x android project

Wow! I just hit the jackpot! okay well time to switch over to my terminal and try this out. I then get the following crash dump:

********** Crash dump: **********
Build fingerprint: 'samsung/SC-04E/SC-04E:4.4.2/KOT49H/SC04EOMUFOB2:user/release-keys'
pid: 18875, tid: 18897, name: Thread-18051  >>> com.benji.etcetc <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
Stack frame #00  pc 00000000  <unknown>
Stack frame #01  pc 000de0b9  /data/app-lib/com.benji.etcetc/ (GameScene::onTouchBegan(cocos2d::Touch*, cocos2d::Event*)+232): Routine GameScene::onTouchBegan(cocos2d::Touch*, cocos2d::Event*) at /Users/benji0110/Downloads/cocos2d-x-3.3/etcetc/
Crash dump is completed

Awesome! Finally according to my crash dump I was able to locate where my app crashes at line 355 .. it turns out I had deleted a game object at runtime and read that objects size, but because it was now destroyed the object no longer existed causing the program to terminate.

What a treat to end an awesome Friday^^

How to traverse through a 2D CCArray – Cocos2dx, C++ on Android (and 5 month workout progress)

Posted on Updated on

Couldn’t find anything on making a multidimensonal CCArray anywhere so I’m leaving this here so I don’t forget later.

Generate an array which holds an array that holds a sprite.

Adding into our 2D CCArray:
Lets open our text editor and write some code!

Somewhere in our header file

	// ... 
    Sprite*                     _spriteImage;
    CCArray*                    _arr_x;
    CCArray*                    _arr_y;
	// ...

Now in our implementation file:
cpp file:

 * add sprites into a 2D CCArray
void Hoge::addIntoMultiCCArray() {
    _arr_x = CCArray::create();

    for (int i = 0; i < ARRAY_SIZE; i++) {
        for (int j = 0; j < ARRAY_SIZE; j++) {
            _arr_y = CCArray::create();

				int randomized_tag  = rand();                                  // generate a random tag to identify which element
				int pos_x           = i * 100 / 1.5f;                          
				int pos_y           = j * 100 / 1.5f;                          
                _spriteImage   	   = MySprite::create();
                _spriteImage        -> setPosition(Point(visibleSize.width / 4 + pos_x, visibleSize.height / 2 + pos_y));
                _spriteImage   	  -> setTag(randomized_tag);
                _arr_y         	  -> addObject(_spriteImage);
                this           	  -> addChild(_spriteImage);

                _arr_x   -> addObject(_arr_y);            //  Where the magic happens
    _arr_x -> retain();
    _arr_y -> retain();

Adding into a multi CCArray is quite straight forward. For each element in the first CCArray you just need to create another CCArray to produce that multidimensional array.

Now we want to do something else besides adding.

Why the traditional way is a bad idea:
The traditional way of traversing through a 2D array is with for loops:

for (int x = 0; x < ARRAY_SIZE; x++) {
	for (int y = 0; y < ARRAY_SIZE; y++) {
		// do something

Lets say we want to edit or delete something. We can’t just do the traditional way like above.

Compiling the code using this method is okay, but at runtime this will produce unexpected behavior and our app will crash due to memory leaks. So to counter this, we need to use CCArrays CCCARRAY_FOREACH method.


// ... somewhere below our implementation file

    // traverse through our multi CCArray
    CCObject*   obj_x = NULL;
    CCObject*   obj_y = NULL;

    CCARRAY_FOREACH(_arr_x, obj_x) {
        CCArray* _obj_x_arr = (static_cast<CCArray*>(obj_x));

        CCARRAY_FOREACH(_obj_x_arr, obj_y) {
            Sprite* _spriteImage_y  = (static_cast<Sprite*>(obj_y));
            Rect rect_a         = IntersectionPoint_A -> getBoundingBox();
            Rect rect_b               = _spriteImage_y  -> getBoundingBox();

            if (rect_b.intersectsRect(rect_a)) {                
                // maybe delete this element from _arr_y?
                _obj_x_arr -> removeObject(_spriteImage_y);
                this       -> removeChildByTag(_spriteImage_y -> getTag());	


Hopefully this will help someone out there. Maybe my coding still is weird (and if it is I would love some feedback on how to write better code^^)

Oh yeah before I forget, here is a recent picture of my arm after 5 months of workout!

Not bad eh? Gotta keep eating and working!
Happy coding!

Lambda type Multithreading to process out of scope parameters (Cocos2dx, Android, C++)

Posted on Updated on

Lets pretend you have a vector containing int data, and you also have a class object which you need to pass into a Thread for some heavy duty processing. Lets also pretend that even if this process isn’t heavy at all but the program requires the order for everything to be processed in in exact logical order.

How do we do that? A thread in Cocos2dx with C++ looks like this:

    auto thread = std::thread([](){
        CCLog("start thread");
        // code goes here
            CCLog("finish another thread");

The problem:
Great! except our requirement was to pass in some data (a vector containing several int type variables and some class object)

With the above code we can’t do that because a Lambda doesn’t allow you to pass in data from outside of its scope even if it is of the same class. So what can we do about it?

The solution:
The solution is to use std::function. Lets make an example.

Header file:

template                <typename T, typename O>
std::function<T()>      MyThreadClass(T int_vector, O class_object);

I use a template for my variable types for 2 reasons:

1) With templates, You don’t need to worry about what data type goes in, everything is automated for you in the background.

2) I’m lazy (actually, I had a deadline writing this and I couldn’t be bothered to check)

Now in our implementation class we can write something like this:

 * thread class
template <typename T, typename M, typename B>
std::function<T()> MyThreadClass(T int_vector, O class_object) {
    CCLog("start thread");
    auto thread = std::thread([int_vector, class_object](){

        for (std::vector<int>::const_iterator itl = int_vector.begin(); itl != int_vector.end(); ++itl) {
            switch(*itl) {
				case 0: class_object.addNumbers(); 		break;
				case 1: class_object.subtractNumbers(); break;
				case 2: class_object.multiplyNumbers(); break;
				case 4: class_object.divideNumbers(); 	break;      

        Director::getInstance()->getScheduler()->performFunctionInCocosThread([int_vector, class_object);
    return 0;

Might look a bit intimidating but its actually quite simple to understand.
In our Lambda we have to ‘capture’ data in order to process them. This is where we want to pass data into our thread. But Lambda won’t let us because it cannot see anything outside of its scope. So we pass data into our function which then is captured using something like below:

auto thread = std::thread([int_vector, class_object](){
	// ...
	// ...
Director::getInstance()->getScheduler()->performFunctionInCocosThread([int_vector, class_object);
	// ...

If we have no data to pass in from the outside, we can just remove int_vector and class_object.

Happy coding!

Generate array of sprites (Cocos2dx, C++, Android)

Posted on Updated on

alright before i forget i better write this down (being an idiot that i am i’ll forget this later)

i have this image:

which when fired at, a single block explodes gracefully like a new born baby taking its first breath outside of a womans-anyway having 1 massive image like the one above to hold several dozen block images isn’t a good idea.

A better way to do this is to have each block:

stored in an array or vector, loop over then before the loop is finished, add each element into our layer.

In code this would look something like this:

Block header file .h

#ifndef __BLOCK_H__
#define __BLOCK_H__
#include "cocos2d.h"
class Block : public cocos2d::Sprite
// ....
    Sprite*                 block;
    Block();                                                    //  constructor
    Sprite*                 generateBlock(int pos);             //  generate a block
#endif // __BLOCK_H__

Block Implementation file.cpp

// ... 
Sprite* Block::generateBlock(int pos) {
    Block        _block_obj;            // instantiate our block class
// ...
    block      = Sprite::create("block.png");
    block     -> setPosition(Vec2(visibleSize.width/2 + pos, visibleSize.height));
    return block;
// ...

Now in our game scene we can do something like this:

Game scene header file.h

// ...
std::array<cocos2d::Sprite*, 10> _blockArray;
void    generateBlocksThenAddToArray();                              // generate a block then add into our array
// ...

implementation file .cpp

 *  generate a block then add into our array
void Hoge::generateBlocksThenAddToArray() {
    for (int i = 0; i < 10; i++) {
        Sprite* _generatedBlock  = _block_obj.generateBlock((i * 100) / 2);
        _blockArray[i]        = _generatedBlock;
        this                 -> addChild(_generatedBlock);

The magic happens in the for loop in Hoge::generateBlocksThenAddToArray() method.
We loop to a suitable range (10 in this case),generate a block from the block class, set its x position by multiplying it by 100 per iteration using i, then add the instantiated block object into our _blockArray with the loop count as its address, then finally add the generated block into our game scene.

There’s probably another (better) way to do this, and I’d love some input from other talented programmers out there so, please give me a shout in the comment section below!

Collision between enemy characters and bullets shot – (Cocos2dx, C++, Android)

Posted on Updated on

Last weekend I got really stuck with something for a game I am building that had to do with collisions. The link is here on Stackoverflow:

Would greatly appreciate it if anybody has a few ideas on how I can work around this idea ..


My problem is the collision detection that i wrote for my enemy character objects and bullet objects.

The symtoms:
When 1 enemy character spawns and i shoot it with my bullets, the enemy character is destroyed as normal. But, if 2 or more enemy characters spawn and i shoot the first enemy that was spawned it does nothing. The last character that was spawned and shot is destroyed without issue.

How i tried to fix it:
I decided to give each enemy character a unique ID by creating an int variable and randomize it each time an enemy is spawned. This ensures me that each enemy character object will definately have a unique identifier so that if i shoot them, I can somehow match that unique ID with my bullet.

However, I dont know how to work around that. The code below just deletes every enemy character associated with any randomized tags

My question:
How can I set my bullet objects to hold every unique ID of my enemy characters, so that when the bullet hits one (doesn’t matter which specific order) it will search then compare the unique IDs it has to the enemy character ID it has shot?

Apologies if my question is unclear or if my coding style is horrible.


 * @param ENEMY_SPAWN_FREQUENCY value is 0.002, set to make them spawn automatically every few seconds
void GameScene::updateScheduler() {
    this -> scheduleUpdate();
    this -> schedule(schedule_selector(GameScene::spawnEnemyCharacter),      ENEMY_SPAWN_FREQUENCY * visibleSize.width);

// ...

bool GameScene::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) {

    // fire button
    auto target = (Sprite*)this -> getChildByTag(FIRE_BUTTON_TAG);
    Rect rect   = target        -> getBoundingBox();
    Point p     = touch         -> getLocation();

    // initialize bullets
    if (rect.containsPoint(p)) {
        GameScene::spawnBullets(PLAYER_TAG, playerCharacter->getPositionX(), playerCharacter->getPositionY()+100, 0, REFERENCE_SIZE_Y);
        return true;

    // ...

    return false;

// ... somewhere below my GameScene.cpp class

void GameScene::spawnBullets(int shooter_id, int x, int y, int p_x, int p_y) {

    // load bullet sprites. 
    SpriteFrameCache::getInstance() -> addSpriteFramesWithFile("bullets.plist");

    // ... 

    auto bulletBody   = PhysicsBody::createBox(bullet_sprite -> getContentSize());
    bulletBody       -> setDynamic(true);
    bulletBody       -> setGravityEnable(false);
    bulletBody       -> setCollisionBitmask(BULLET_COLLISION_BITMASK);
    bulletBody       -> setContactTestBitmask(true);

    bullet_sprite    -> setPhysicsBody(bulletBody);
    bullet_sprite    -> setPosition(Point( x, y));
    bullet_sprite    -> setScale(0.2f);
    bullet_sprite    -> setTag(BULLET_TAG);
    this             -> addChild(bullet_sprite);

    // animate bullet object so that it shoots
    bullet_obj.fireBullet(bullet_sprite, p_x, p_y);

// ... somewhere further down this class i define my enemy characters to be spawned as enemy objects in the below method

void GameScene::spawnEnemyCharacter(float dt) {
    int random_tag = rand();        // ensure that each enemy character object has a unique random id as its tag
    enemyCharacter_obj.spawnEnemy(this, random_tag);

// ... destroy enemy characters if hit by bullet:

bool GameScene::onContactBegin( cocos2d::PhysicsContact &contact ) {
    PhysicsBody *a = contact.getShapeA() -> getBody();
    PhysicsBody *b = contact.getShapeB() -> getBody();

    if (BULLET_COLLISION_BITMASK == a->getCollisionBitmask() && ENEMY_COLLISION_BITMASK == b-> getCollisionBitmask() ||
        BULLET_COLLISION_BITMASK == b->getCollisionBitmask() && ENEMY_COLLISION_BITMASK == a-> getCollisionBitmask() ) {

        // delete enemys with random tag
        for (std::vector<int>::iterator itl = enemy_tags.begin(); itl != enemy_tags.end(); ++itl) {
            enemyCharacter_obj.destroyEnemyCharacter(this, *itl);
        this -> removeChild(bullet_sprite);

    return true;


void Bullet::fireBullet(Sprite* bullet_sprite, int x, int y) {
    auto bulletAction  = Sequence::create( MoveBy::create(BULLETSPEED, Point(x, y)), CallFuncN::create( CC_CALLBACK_1(Bullet::doRemoveFromParentAndCleanup, this, true)), NULL);
    bullet_sprite     -> runAction(bulletAction);

And now my enemy character class:

void EnemyCharacter::spawnEnemy(cocos2d::Layer* layer, int random_tag) {

    // データ読み込み
    SpriteFrameCache::getInstance() -> addSpriteFramesWithFile("enemies.plist");

    // 敵キャラクター
    enemyCharacter   = Sprite::createWithSpriteFrameName("enemy_1.png");
    auto enemyCharacterBody  = PhysicsBody::createBox(enemyCharacter -> getContentSize() / 5);

    // ...

    auto enemyCharacterPosition = (random * visibleSize.height ) + ( enemyCharacter -> getContentSize().height / 2);
    enemyCharacterBody         -> setDynamic(false);
    enemyCharacterBody         -> setCollisionBitmask(ENEMY_COLLISION_BITMASK);
    enemyCharacterBody         -> setContactTestBitmask(true);
    enemyCharacter             -> setScale(0.3f);
    enemyCharacter             -> setTag(random_tag);
    enemyCharacter             -> setPhysicsBody(enemyCharacterBody);
    enemyCharacter             -> setPosition( Point( ((random * visibleSize.width) + (enemyCharacter->getContentSize().width / 2)), visibleSize.height ) );
    layer                 -> addChild(enemyCharacter);

    // ...

// .. further down this class 

void EnemyCharacter::destroyEnemyCharacter(cocos2d::Layer* layer, int randomized_tag) {
    layer-> removeChild(layer -> getChildByTag(randomized_tag));