Cocos2d-x HelloWorld初见

本文详细介绍了如何使用Cocos2d-x(C++版)进行游戏开发,包括下载源码、Mac环境下安装、Xcode项目创建、运行游戏、项目结构解析以及基本游戏场景实现。

1.从官方网站的下载页面 http://www.cocos2d-x.org/download  下载最新的Cocos2d-x(C++版) 源码。

2.Mac环境下安装Cocos2d-x,启动终端进入install-templates-xcode.sh所在的目录,然后输入sudo ./install-templates-xcode.sh”命令开始执行安装命令,Xcode便有了Cocos2d-x的模版。

3.打开Xcode新建一个cocos2dx项目,可见如下界面:


4.运行这个项目在Iphone模拟器上,可以看到一个“Hello World”界面。

5.查看工程目录如下:

其中Resource是资源文件夹,主要存放游戏中需要的图片、音频和配置等资源文件。

ios文件夹中包含main函数和初始化界面控制器的文件,主要做了创建窗口、设置全屏、设置屏幕转向等方法,还包含cocos2dx的整个生命周期。

libs文件夹是cocos2dx引擎及其扩展的源代码。

Classes目录中放置我们最主要的程序。"AppDelegate.h""AppDelegate.cpp"文件是Cocos2d-x游戏的通用入口文件。

6.打开"AppDelegate.cpp", 在 bool applicationDidFinishLaunching() 这个方法中,默认的实现了游戏启动后的必要准备:

    // 初始化游戏引擎控制器CCDirector,以便启动游戏引擎
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // 启用FPS显示
    pDirector->setDisplayStats(true);

    // 设置绘制间隔
    pDirector->setAnimationInterval(1.0 / 60);

    // 创建一个场景,它是自动释放的对象
    CCScene *pScene = HelloWorld::scene();

    // 运行
    pDirector->runWithScene(pScene);

7. "HelloWorldScene.h" "HelloWorldScene.cpp" 这两个文件定义了 HelloWorld 项目中默认的游戏场景。 Cocos2d 的游戏结构可以简单地概括为场景、层、精灵,而这两个文件就是 Hello World 场景的实现文件。每个游戏组件都可以添加到另一个组件中,形成层次关系,例如场景中可以包含多个层,层中可以包含多个精灵。

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /////////////////////////////
    // 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
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

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

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

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

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

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

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值