cococos 2 d-x学習ノート(4)--CCLayer背景描画
本文はhttp://www.wenbanana.comわら人形ブログへようこそ!
Step 1:cococos 2 d-win 32アプリケーションを作成しsimpleGameと命名
Step 2:HelloWorldScene.hで新しいクラスLayerBlack、LayerBlue、LayerBackgroundを作成
class LayerBackground:public CCLayerColor
{
public:
LayerBackground();
~LayerBackground();
public:
virtual void onEnter();
};
class LayerCloud:public CCLayerColor
{
public:
LayerCloud();
~LayerCloud();
public:
virtual void onEnter();
};
class LayerHill:public CCLayerColor
{
public:
LayerHill();
~LayerHill();
public:
virtual void onEnter();
};
HelloWorldScene.cpp :
/************************************************************************/
/* class LayerCloud */
/************************************************************************/
LayerCloud::LayerCloud()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
this->initWithColor(ccc4(0,0,0,0));
CCSprite* pSpriteCloud = CCSprite::spriteWithFile("cloud.png");// CC_BREAK_IF(!pSpriteCloud); pSpriteCloud->setPosition(ccp(size.width/2,size.height/2)); addChild(pSpriteCloud);
}
LayerCloud::~LayerCloud()
{
}
void LayerCloud::onEnter()
{
CCLayer::onEnter();
}
/************************************************************************/
/* class LayerHill */
/************************************************************************/
LayerHill::LayerHill()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
this->initWithColor(ccc4(0,0,0,0)); CCSprite* pSpriteHill = CCSprite::spriteWithFile("hill.png"); //CC_BREAK_IF(!pSpriteHill); pSpriteHill->setPosition(ccp(size.width/2,size.height/2)); addChild(pSpriteHill);
}
LayerHill::~LayerHill()
{
}
void LayerHill::onEnter()
{
CCLayer::onEnter();
}
/************************************************************************/
/* class LayerBackground */
/************************************************************************/
LayerBackground::LayerBackground ()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* label = CCLabelTTF::labelWithString(m_strTitle.c_str(),"Arial", 32); addChild(label);
label->setPosition(ccp(size.width /2, size.height/2));
this->initWithColor(ccc4(255,255,255,255));
}
LayerBackground ::~LayerBackground ()
{
}
void LayerBackground ::onEnter()
{
CCLayer::onEnter();
}
init() :
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::node();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
//HelloWorld *layer = HelloWorld::node();
LayerBackground* playerBack = new LayerBackground();
LayerCloud* playerCloud = new LayerCloud();
LayerHill* playerHill = new LayerHill();
CC_BREAK_IF(! playerHill);
CC_BREAK_IF(!playerCloud);
CC_BREAK_IF(!playerBack);
// add layer as a child to scene
//addChild ,LayerHill LayerCloud , 。
scene->addChild(playerHill, 2);
scene->addChild(playerCloud, 1);
scene->addChild(playerBack,0);
} while (0);
// return the scene
return scene;
}
, , , (Layer) ;
:
2つの背景の組み合わせで
最終的に以下の効果を得る
プログラムを実行すると、次の画面が表示されます.
ソースコードのダウンロードアドレス:http://download.csdn.net/download/wen294299195/4525792