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

被折叠的 条评论
为什么被折叠?



