Question

can't change the color of a background i have this simple class :

here is the c++ file :

#include "HelloWorldScene.h"

USING_NS_CC;

HelloWorld::HelloWorld() 
{
    ;
}
Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

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

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first

    if ( !LayerColor::initWithColor(Color4B(20,0,0,255)) )
    {
        return false;
    }
    winSize = Director::getInstance()->getWinSize();
    visibleSize = Director::getInstance()->getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 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
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

    closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);

    schedule( schedule_selector(HelloWorld::tick) );

    return true;
}

void HelloWorld::onExit()
{
    LayerColor::onExit();

}
void HelloWorld::onEnter()
{
    LayerColor::onEnter();
    auto cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("interface/sprites.plist", "interface/sprites.png");   
    SpriteBatchNode* batch = SpriteBatchNode::create("interface/sprites.png");
    this->addChild(batch,BATCH_Z);

    auto listener = EventListenerTouchAllAtOnce::create();

    listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
    listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
    listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}



void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event  *event)
{
    for( auto& touch : touches)
    {


    }



}

void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event  *event)
{
    for( auto& touch : touches)
    {


    }
}

void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event  *event)
{

   for( auto& touch : touches)
   {  

      startAnim = true;  

   };

}

void HelloWorld::tick(float dt)
{

     if(startAnim)
     {





     }


}



void HelloWorld::draw()
{
    LayerColor::draw();


}



HelloWorld::~HelloWorld()
{
    // Removes Touch Event Listener
    _eventDispatcher->removeEventListener(_touchListener);

}
void HelloWorld::menuCloseCallback(Object* pSender)
{
    Director::getInstance()->end();

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

and the h file :

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class GameObj;
class ReelGameObj;

class HelloWorld : public cocos2d::LayerColor
{
public:

     HelloWorld();
    ~HelloWorld();
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // a selector callback
    void menuCloseCallback(Object* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);

    void tick(float dt);
    virtual void draw();
    virtual void onEnter();
    virtual void onExit();

    void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);
    void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);
    void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);




  protected:
    cocos2d::CustomCommand _customCommand;
    void onDraw();
 private:
    cocos2d::EventListenerTouchOneByOne* _touchListener;
    cocos2d::Size winSize;
    cocos2d::Size visibleSize;
    cocos2d::Point origin;

    GameObj* pMainWindowObjCenter;
    ReelGameObj* pReelGameObj;
    bool startAnim;


};

#endif // __HELLOWORLD_SCENE_H__

and nothing no color in the background , why ? im working on windows with VC 2012

Était-ce utile?

La solution

I think your color is too dark. Try changing the values to (255,25,255,255) and check the result.

I created a sample project (in Beta 2), and only changed these lines:

.h:

class HelloWorld : public cocos2d::LayerColor

.cpp:

if( !LayerColor::initWithColor(Color4B(255,255,255,255)) )

The result is a white background.

Autres conseils

I cleaned up and updated your code (I use cocos2dx 2.2.1) and I could change the layer's color to red.

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayerColor
{
public:

    HelloWorld();
    ~HelloWorld();
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);

    virtual void onEnter();
    void draw();
private:
    cocos2d::CCSize winSize;
    cocos2d::CCSize visibleSize;
    cocos2d::CCPoint origin;

    bool startAnim;
};

#endif // __HELLOWORLD_SCENE_H__

The cpp file

#include "HelloWorldScene.h"

USING_NS_CC;

HelloWorld::HelloWorld()
{}

HelloWorld::~HelloWorld()
{}


CCScene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = CCScene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

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

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first

    ccColor4B c = ccc4(255,0,0,255);
    if ( !CCLayerColor::initWithColor(c) )
    {
        return false;
    }

    return true;
}

void HelloWorld::onEnter()
{
    CCLayerColor::onEnter();
}

void HelloWorld::draw()
{
    CCLayerColor::draw();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top