本文介绍了无法更改场景的背景cocos2dx机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用cocos2dx开始游戏开发。并开始与HelloWorld示例游戏。我能够运行这个样本游戏。但是,当我试图改变背景颜色,我得到错误

  ** ** HelloWorldScene.h
    类型'的HelloWorld必须实现继承纯虚方法的cocos2d :: CCRGBAProtocol :: setOpacity**变化**
一流的HelloWorld:公众的cocos2d :: CCLayerColor

和,可以在

  ** ** HelloWorldScene.cpp    参数无效
    考生:
    布尔initWithColor(常量的cocos2d :: _ ccColor4B&放大器;,?,?)
    布尔initWithColor(常量的cocos2d :: _ ccColor4B&安培;)
    
   **变化**
   CC_BREAK_IF(!CCLayerColor :: initWithColor(ccc4(255255255255)));

我是新的cocos2dx,也为C ++。有没有留下来包括或什么什么东西?
请帮我解决这个问题。
谢谢。

编辑:

HelloWorldScene.cpp

 的#includeHelloWorldScene.h使用cocos2d的命名空间;
使用命名空间CocosDenshion;CCScene *的HelloWorld ::场景()
{
    CCScene *场景= NULL;
    做{        //场景是一个自动释放对象
        // CCScene *场景= CCScene ::创建();
        现场= CCScene ::创建();
        CC_BREAK_IF(场景!);        //层是一个自动释放对象
        *的HelloWorld =层的HelloWorld:创建();
        CC_BREAK_IF(层!);
        //添加图层作为一个孩子的场景
        scene->的addChild(层);
    }而(0);
    //返回现场
    返回现场;
}//的初始化您需要初始化您的实例
布尔的HelloWorld ::的init()
{
    布尔BRET = FALSE;
    做{
        //////////////////////////////
        // 1.超初始化第一
        //如果(!CCLayer ::的init())
        // {
            //返回false;
        //}        // CC_BREAK_IF(CCLayer ::的init()!);
        CC_BREAK_IF(!CCLayerColor :: initWithColor(ccc4(255255255255)));
        /////////////////////////////
        // 2.添加一个菜单项以X的形象,这是点击退出程序
        //你可以修改它。        //添加一个关闭按钮,退出的进度。这是一个自动释放对象
        CCMenuItemImage * pCloseItem = CCMenuItemImage ::创建(
                CloseNormal.png
                CloseSelected.png
                这个,
                menu_selector(的HelloWorld :: menuCloseCallback));
        pCloseItem-> setPosition两种(CCP(CCDirector :: sharedDirector() - GT; getWinSize()宽 - 20,20));        //创建菜单,它是一个自动释放对象
        CCMenu * pMenu = CCMenu ::创建(pCloseItem,NULL);
        pMenu-> setPosition两种(CCPointZero);
        这 - >的addChild(pMenu,1);        /////////////////////////////
        // 3.添加$ C $以下CS ...        //添加一个标签显示Hello World的
        //创建和初始化标签
        CCLabelTTF * PLABEL = CCLabelTTF ::创建(GAME,吞武里,38);        //问导演窗口大小
        CCSize大小= CCDirector :: sharedDirector() - > getWinSize();        //标签定位在屏幕的中心
        pLabel-> setPosition两种(CCP(size.width / 2,size.height - 20));        //为孩子添加标签,这层
        这 - >的addChild(PLABEL,1);        //添加的HelloWorld闪屏
        CCSprite * pSprite = CCSprite ::创建(Untitled1.png);        //定位在屏幕的中心的子画面
        pSprite-> setPosition两种(CCP(size.width / 2,size.height / 2));        //为孩子添加精灵到这一层
        这 - >的addChild(pSprite,0);        CCSprite *玩家= CCSprite ::创建(Player.png,CCRectMake(0,0,27,50));
        播放器 - > setPosition两种(CCP(播放器 - 方式> getContentSize()宽/ 2,size.height / 2));
        这 - >的addChild(播放器);
        BRET =真;    }而(0);
    返回BRET;
}无效的HelloWorld :: menuCloseCallback(CCObject * pSender)
{
    CCDirector :: sharedDirector() - GT;结束();#如果(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    出口(0);
#万一
}

HelloWorldScene.h

 的#ifndef __HELLOWORLD_SCENE_H__
#定义__HELLOWORLD_SCENE_H__#包括cocos2d.h
#包括SimpleAudioEngine.h//类的HelloWorld:公众的cocos2d :: CCLayer
一流的HelloWorld:公众的cocos2d :: CCLayerColor
{
上市:
    //这是一个区别。方法在cocos2d-X'初始化'返回,而不是在cocos2d-iphone返回'ID'布尔,
    虚拟BOOL的init();    //有一个在CPP没有'身份证',所以我们要推荐返回正好类指针
    静态的cocos2d :: CCScene *场景();    //一个选择回调
    无效menuCloseCallback(CCObject * pSender);
    //手动实施静态节点()方法
    CREATE_FUNC(的HelloWorld);
};#ENDIF // __HELLOWORLD_SCENE_H__


解决方案

要改变背景颜色,最好的办法是简单地做:

  glClearColor(1.0,1.0,1.0,1.0);

你的场景中的init()方法。这样你就可以完全跳过CCLayerColor步骤,你会得到更好的整体性能的奖金。干杯!

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__
解决方案

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!

这篇关于无法更改场景的背景cocos2dx机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:52