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


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◆◇◆

“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＾＾


Posted on Updated on


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();

            _arr_y = CCArray::create();

				int pos_x           = i * 100 / 1.5f;                          
				int pos_y           = j * 100 / 1.5f;                          
                _spriteImage   	   = MySprite::create();
                _spriteImage   	  -> setTag(randomized_tag);
                _arr_y         	  -> addObject(_spriteImage);
                this           	  -> addChild(_spriteImage);

    _arr_x -> retain();
    _arr_y -> retain();


Now we want to do something else besides adding.

Why the traditional way is a bad idea:

		// do something




// ... 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_FOREACH(_obj_x_arr, 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);




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>
    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:

	// ...
	// ...
	// ...

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

Happy coding!


Posted on Updated on


i have this image:


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


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
#endif // __BLOCK_H__

Block Implementation file.cpp

// ... 
Sprite* Block::generateBlock(int pos) {
    Block        _block_obj;            // instantiate our block class
// ...
    block      = Sprite::create("block.png");
    return block;
// ...

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

Game scene header file.h

// ...
// ...

implementation file .cpp

 *  generate a block then add into our array
void Hoge::generateBlocksThenAddToArray() {
        _blockArray[i]        = _generatedBlock;
        this                 -> addChild(_generatedBlock);



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));