step1:创建一个cocos2d-win32 application,并命名为simpleGame

step2:在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中添加如下代码:

#include "HelloWorldScene.h"



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

    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        // 2. Add a label shows "Hello World".

        // Create a label and initialize with string "Hello World".
        CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Thonburi", 64);
        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 20));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1);

        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

/************************************************************************/
/* class LayerCloud                                                                     */
/************************************************************************/
LayerCloud::LayerCloud()
{
  m_strTitle = "";
}
LayerCloud::~LayerCloud()
{

}

void LayerCloud::onEnter()
{
  CCLayer::onEnter();

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

}

/************************************************************************/
/* class LayerHill                                                                    */
/************************************************************************/

LayerHill::LayerHill()
{
  m_strTitle = "";
}


LayerHill::~LayerHill()
{

}

void LayerHill::onEnter()
{
  CCLayer::onEnter();

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

/************************************************************************/
/* class LayerBackground                                                                    */
/************************************************************************/

LayerBackground::LayerBackground ()
{
  m_strTitle = "";
}


LayerBackground ::~LayerBackground ()
{

}

void LayerBackground ::onEnter()
{
  CCLayer::onEnter();

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

}

由于游戏中一个场景中的背景可能含有很多内容,若将全部内容放在同一个背景下,就会导致管理上的不方便,因此可以通过组合多个背景(Layer)来形成一个画面;来管理一个游戏场景。

示意图:

通过两个背景的组合

最终得到如下效果

运行程序后会见到如下的画面:

源代码下载地址:http://download.csdn.net/download/wen294299195/4525792

打赏

发表评论

您的电子邮箱地址不会被公开。