Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Native Development

Reply
Regular Contributor
pigling
Posts: 71
Registered: ‎02-05-2012
My Carrier: Singtel

[Coco2d-x] how to simply integrate Box2D to HelloWorld sample in Cocos2d-x

Hi,

I am trying to use Cocos2d-x 2.02 for BB10 development. I have built and deployed HelloWorld sample to Alpha device. Then I try to add Box2D componet to the code but it fails:

HelloWorld.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Box2D/Box2D.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
	HelloWorld();
	~HelloWorld();
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);

private:
    b2World* world;
};

#endif // __HELLOWORLD_SCENE_H__

 HelloWorld.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

HelloWorld::HelloWorld()
{
	b2Vec2 gravity;
	gravity.Set(0.0f, -10.0f);

	world = new b2World(gravity);

	world->setAllowSleeping(true);

	world->setContinuousPhysics(true);
}

HelloWorld::~HelloWorld()
{
	if (world)
		delete world;
}

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
    if (CCApplication::sharedApplication()->getTargetPlatform() == kTargetIphone)
    {
        pCloseItem->setPosition(ccp(visibleSize.width - 20 + origin.x, 20 + origin.y));
    }
    else 
    {
        pCloseItem->setPosition(ccp(visibleSize.width - 40 + origin.x, 40 + origin.y));
    }

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

    // position the label on the center of the screen
    pLabel->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height - 50 + origin.y));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

 Of course I have add the including and library path in C/C++ settings.

But there are errors:

Description	Resource	Path	Location	Type
'class b2World' has no member named 'setAllowSleeping'	HelloWorldScene.cpp	/HelloCpp/Classes	line 12	C/C++ Problem
'class b2World' has no member named 'setContinuousPhysics'	HelloWorldScene.cpp	/HelloCpp/Classes	line 14	C/C++ Problem

 Search through Google and see other codes but can't find a reason for my problem. Anyone also faces the same issue? Thanks.

 

regards,

pigling

Please use plain text.
Regular Contributor
pigling
Posts: 71
Registered: ‎02-05-2012
My Carrier: Singtel

Re: [Coco2d-x] how to simply integrate Box2D to HelloWorld sample in Cocos2d-x

If I temp create a pointer to b2World in ::init() like this:

b2World* test = new b2World();
test->setAllowSleeping();

 it will not promt error this new code.

Please use plain text.