Not able to change the background of the scene in cocos2dx android
Asked Answered
A

5

0

I have started Game Development using cocos2dx. And started with HelloWorld sample game. I am able to run this sample game. But when I try to change the Background Color, I am getting error in

 **HelloWorldScene.h**
    The type 'HelloWorld' must implement the inherited pure virtual method 'cocos2d::CCRGBAProtocol::setOpacity' 

**Changes:**
class HelloWorld : public cocos2d::CCLayerColor

and also in

    **HelloWorldScene.cpp**

    Invalid arguments '
    Candidates are:
    bool initWithColor(const cocos2d::_ccColor4B &, ?, ?)
    bool initWithColor(const cocos2d::_ccColor4B &)
    '
   **Changes:**
   CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255,255,255,255)));

I am new for cocos2dx and also for C++. Is there any thing left to include or what? Please help me to solve this issue. Thank You.

EDIT:

HelloWorldScene.cpp

#include "HelloWorldScene.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do {

        // 'scene' is an autorelease object
        //CCScene *scene = CCScene::create();
        scene = CCScene::create();
        CC_BREAK_IF(!scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(!layer);
        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do {


        //////////////////////////////
        // 1. super init first
        //if ( !CCLayer::init())
        //{
            //return false;
        //}

        //CC_BREAK_IF(!CCLayer::init());
        CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255,255,255,255)));
        /////////////////////////////
        // 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) );
        pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

        // 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("GAME", "Thonburi", 38);

        // ask director the window size
        CCSize size = CCDirector::sharedDirector()->getWinSize();

        // position the label on the center of the screen
        pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

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

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

        // position the sprite on the center of the screen
        pSprite->setPosition( ccp(size.width/2, size.height/2) );

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

        CCSprite* player = CCSprite::create("Player.png", CCRectMake(0, 0, 27, 50));
        player->setPosition(ccp(player->getContentSize().width/2, size.height/2));
        this->addChild(player);
        bRet = true;

    } while (0);
    return bRet;
}

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

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

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"

//class HelloWorld : public cocos2d::CCLayer
class HelloWorld : public cocos2d::CCLayerColor
{
public:
    // 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);


};

#endif // __HELLOWORLD_SCENE_H__
Alonzoaloof answered 11/7, 2013 at 7:31 Comment(0)
N
4

The best way to change the background colour is to simply do:

glClearColor(1.0,1.0,1.0,1.0);

during your Scene init() method. This way you can completely skip the CCLayerColor steps, and you get the bonus of better overall performances. Cheers!

Nivernais answered 31/5, 2014 at 5:40 Comment(0)
C
1

Above Code is correct but one thing is to be noted here for higher versions of cocos 2dx CCLayerColor is used and for lower versions CCLayer is used in HelloWorld.h and HelloWorld.cpp while extend the line

class HelloWorld :public cocos2d::CCLayerColor

in HelloWorld.h

and

CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255,255,255,255)));

in HelloWorld.cpp

Creighton answered 29/7, 2013 at 10:21 Comment(6)
CCLayer function have been upgraded to CCLayerColor in higher versions of Cocos 2dx.If you are using lower version write Class HelloWorld : public cocos2d::CCLayer NOT CCLayerColor. same with the cpp file.Creighton
I am using the version cocos2d-2.1rc0-x-2.1.3. & also checked cocos2d-2.1rc0-x-2.1.4Alonzoaloof
Try using these lines CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 255))); if(!CCLayerColor::initWithColor(ccc4(255,255,255,255) ) ) {return false;} instead of CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255,255,255,255))); in HelloWorld.cppCreighton
getting this error: Invalid arguments ' Candidates are: bool initWithColor(const cocos2d::_ccColor4B &, ?, ?) bool initWithColor(const cocos2d::_ccColor4B &) 'Alonzoaloof
check using namespace Cocos2d; line is not giving errors, Include Cocos2dxHome->Cocos2dx->include folder in additional include directories that come under properties.Creighton
Check this link It might be useful to you cocos2d-x.org/boards/6/topics/16983 and this one also cocos2d-x.org/boards/6/topics/1986 hope this helps.Creighton
C
1

Try with this:

HelloWorldScene.cpp

CCLayerColor* layer1 = CCLayerColor::create(ccc4(255, 255, 255, 255), x, y);

layer1->setPosition( ccp(x/2, y/2));

addChild(layer1);

layer1->runAction(CCRepeatForever::create(CCSequence::create(
                   CCTintTo::create(6, 255, 0, 255),
                   CCTintTo::create(6, 255, 255, 255),
                   CCDelayTime::create(1),
                    NULL)));
Choriocarcinoma answered 27/12, 2013 at 6:21 Comment(0)
P
0

I have tried running the HelloWorld example with your changes, i am not getting any errors and i am able to change the background color. I am pasting my classes here. I hope it will help you. Also check my blog on cocos2dx, it is mainly for the bigineers www.touchscreenstudio.com

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

#include "Box2D/Box2D.h"

#include "SimpleAudioEngine.h"

class HelloWorld : /*public cocos2d::CCLayer*/ public cocos2d::CCLayerColor
{
public:
    // 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);
    CCNode* getNode();
};

#endif  // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

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

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        //CC_BREAK_IF(! CCLayer::init());
        CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255,255,255,255)));
        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        // 2. Add a label shows "Hello World".

        // Create a label and initialize with string "Hello World".
        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));

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

        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

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

        CCNode* myNode = getNode();
        addChild(myNode, 3);
        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    removeChildByTag(10);
    CCDirector::sharedDirector()->end();
}

CCNode* HelloWorld::getNode()
{
    CCNode* n = CCNode::create();
    n->setTag(9);
    return n;
}
Provision answered 11/7, 2013 at 10:11 Comment(6)
Ya, I am beginner for this platform event also with game development. I have checked your code its really nice for beginners to start with. :) So, I have also the same code as you replied. But I am still getting the same errors as described above!! Should I give my whole file code?Alonzoaloof
Which version of cocos2d-x are you using? Also paste your code too.Provision
I am using cocos2d-2.1rc0-x-2.1.3 version for development & please check the Edit in question.Alonzoaloof
Hi, i successfully compiled the program using your classes on the cocos2dx version cocos2d-2.1rc0-x-2.1.3, try cleaning the solution and rebuilding it. Also download the attached file on my server, and check the HelloWorld example, try to run it and you can see the changed background color. Download link touchscreenstudio.com/blogs/TSS/cocos2d-2.1rc0-x-2.1.3.rarProvision
Sorry for wrong link , download from the following link blogs.touchscreenstudio.com/TSS/cocos2d-2.1rc0-x-2.1.3.rarProvision
Hi, I downloaded zip from your server, but unfortunately I am getting same error. Should there be any problem regarding something not included? Can you please check and help me with this?Alonzoaloof
T
0

For those experiencing same problem, you are changing the color indeed but the layer's opacity is set to 0. Try this:

this->setColor(color);//set color as you normally would
this->setOpacity(255);//set opacity to 255 to be completely visible or as needed

This did the trick for me. (My apologies if there is a syntax error in the above code, I use JSB and not cpp)

Tremann answered 24/11, 2013 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.