0

我已经开始使用 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__
4

5 回答 5

4

更改背景颜色的最佳方法是简单地执行以下操作:

glClearColor(1.0,1.0,1.0,1.0);

在您的场景 init() 方法期间。这样,您可以完全跳过 CCLayerColor 步骤,并获得更好的整体性能的奖励。干杯!

于 2014-05-31T05:40:34.513 回答
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)));
于 2013-12-27T06:21:26.947 回答
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 中

于 2013-07-29T10:21:29.790 回答
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;
}
于 2013-07-11T10:11:20.567 回答
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)

于 2013-11-24T08:39:05.613 回答