cocos2d-x ——HelloWorld

其他部分不需要修改,只需要改变HelloWorldScene.cpp的init()方法即可

#include "HelloWorldScene.h"

using namespace cocos2d;

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();
        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 
    {
        //////////////////////////////////////////////////////////////////////////
        // 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.

		CCSprite* closeSprite = CCSprite::spriteWithFile("CloseNormal.png");
		closeSprite->setPosition(ccp(50,50));
		this->addChild(closeSprite);//不加这一行的话,只是创建了精灵,并没有将精灵显示到屏幕上


		/**
		*
		  CCLabelTTF::labelWithString("cocos2d-x for win32","Thonburi",40)的3个参数的含义分别为:
		  需要显示的文字,字体,大小

		  setPosition(ccp(helloCocos2d->getContentSize().width/2,100)); :ccp()的第一个参数为所要显示的内容的中心点

		  addChild(helloCocos2d,1);:第二个参数可以不加。不加的话默认为0.大的会覆盖小的。相同的情况下,按添加的先后顺序
		*/
		CCLabelTTF* helloCocos2d = CCLabelTTF::labelWithString("cocos2d-x for win32","Thonburi",40);
		helloCocos2d->setPosition(ccp(helloCocos2d->getContentSize().width/2,100));
		this->addChild(helloCocos2d,1);

		/**
		 以下介绍的是通过label来显示文本,单击之后会放大文本

		  CCMenu::menuWithItems(labelItem,NULL);:可以向menu中添加多个item,最后一个参数为NULL
		*/
		CCMenuItemLabel* labelItem = CCMenuItemLabel::itemWithLabel(CCLabelTTF::labelWithString("new Game","Thonburi",40));
		labelItem->setPosition(ccp(200,200));
		CCMenu* menu = CCMenu::menuWithItems(labelItem,NULL);
		menu->setPosition(0,0);
		this->addChild(menu);


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


你可能感兴趣的:(cocos2d-x ——HelloWorld)