cocos2d-x学习笔记(2)--向游戏中添加角色(sprite)

 本文出自http://www.wenbanana.com稻草人博客,欢迎访问!

 

step1:首先创建一个cocos2d-win32 application,并命名为addSprite

Step2:然后把图片添加到工程文件夹下的resource文件夹下。

 

Step3:打开HelloWorldScene.cpp文件,将init()函数修改成如下代码

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {

        CC_BREAK_IF(! CCLayer::init());

			/************************************************************************/
			/* 此处为添加关闭按钮                                                                     */
			/************************************************************************/
        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);

        this->addChild(pMenu, 1);
		///////////////////////////////////////////////////////////////////////////////////

         /************************************************************************/
         /* 添加角色                                                                        */
         /************************************************************************/
        CCSprite* pSprite = CCSprite::spriteWithFile("grossini.png");
        CC_BREAK_IF(! pSprite);

		CCSize  size = CCDirector::sharedDirector()->getWinSize();

        // 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;
}

修改完后,编译运行程序,出现如下画面,表示添加成功

cocos2d-x学习笔记(2)--向游戏中添加角色(sprite)_第1张图片

 

 

 

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

你可能感兴趣的:(游戏,null,application,menu,layer)