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

Scene init() メソッド中。このようにして、CCLayerColor の手順を完全にスキップでき、全体的なパフォーマンスが向上するというボーナスを得ることができます。乾杯!

于 2014-05-31T05:40:34.513 に答える