2014-02-20 18 views
1

不能改變背景的顏色我有這個簡單的類:的cocos2d-Xβ3 LayerColor不改變背景顏色

這裏是C++的文件:

#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 
} 

和H文件:

#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__ 

並沒有什麼顏色的背景下,爲什麼? 即時通訊與2012年的Windows工作

回答

3

我認爲你的顏色太黑。嘗試將值更改爲(255,25,255,255)並檢查結果。

我創建了一個示例項目(在Beta 2中),並且僅改變了這些行:

.H:

class HelloWorld : public cocos2d::LayerColor 

的.cpp:

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

結果是白色背景。

+0

沒有它仍是黑色的.. – user63898

+0

請嘗試製作一個新的HelloWorld項目和測試只有改變的代碼我上面描述。它可能是您的代碼中的其他內容,因爲這適用於beta 2。另外,您的標題中包含beta 3,但Cocos2d-x目前僅在beta 2中。 – TigerCoding

0

我清理並更新了您的代碼(我使用cocos2dx 2.2.1),並且我可以將圖層的顏色更改爲紅色。

#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__ 

cpp文件

#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(); 
}