无法在cocos2dx android中更改场景的背景

时间:2013-07-11 07:31:50

标签: android cocos2d-x

我已经开始使用cocos2dx进行游戏开发。并从HelloWorld示例游戏开始。我能够运行这个示例游戏。但是当我尝试更改背景颜色时,我的错误

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

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

以及

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

我是cocos2dx的新手,也是C ++的新手。还有什么东西要包括什么? 请帮我解决这个问题。 谢谢。

修改

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__

5 个答案:

答案 0 :(得分:4)

更改背景颜色的最佳方法是:

glClearColor(1.0,1.0,1.0,1.0);

在Scene init()方法中。这样你就可以完全跳过CCLayerColor步骤,获得更好的整体表现奖励。干杯!

答案 1 :(得分:1)

上面的代码是正确的,但有一点需要注意,更高版本的cocos 2dx使用CCLayerColor,而对于较低版本,CCLayer用于HelloWorld.h和HelloWorld.cpp 同时延长线

class HelloWorld :public cocos2d::CCLayerColor
HelloWorld.h中的

CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255,255,255,255)));
HelloWorld.cpp中的

答案 2 :(得分:1)

试试这个:

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

答案 3 :(得分:0)

我已尝试使用您的更改运行HelloWorld示例,我没有收到任何错误,我可以更改背景颜色。我在这里粘课。我希望它会对你有所帮助。另请查看我在cocos2dx上的博客,它主要针对的是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;
}

答案 4 :(得分:0)

对于遇到相同问题的人,您确实正在更改颜色,但图层的不透明度设置为0。 试试这个:

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

这对我有用。 (我很抱歉,如果上面的代码中存在语法错误,我使用JSB而不是cpp)