Cocos2d-x HelloWorld初见

来源:互联网 发布:明事理 辨是非 知荣辱 编辑:程序博客网 时间:2024/05/19 21:59

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 instancebool 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}

0 0
原创粉丝点击