工具下载和配置:
cocos2d-x 2.2.3官网下载http://www.cocos2d-x.org/download
工具的配置手册http://www.cocos2d-x.org/wiki/Getting_Started_with_Cocos2d-x
Mac环境下创建工程:
Jason:project-creator shengju$ ./create_project.py
Usage: create_project.py -project PROJECT_NAME -package PACKAGE_NAME -language PROGRAMING_LANGUAGE
Options:
-project PROJECT_NAME Project name, for example: MyGame
-package PACKAGE_NAME Package name, for example: com.MyCompany.MyAwesomeGame
-language PROGRAMING_LANGUAGE Major programing lanauge you want to used, should be [cpp | lua | javascript]
Sample 1: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame
Sample 2: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language javascript
Jason:project-creator shengju$ ./create_project.py -project Hello -package com.jason.hello -language cpp
proj.ios : Done!
proj.android : Done!
proj.win32 : Done!
proj.winrt : Done!
proj.wp8 : Done!
proj.mac : Done!
proj.blackberry : Done!
proj.linux : Done!
proj.marmalade : Done!
proj.tizen : Done!
proj.wp8-xaml : Done!
New project has been created in this path: /Users/shengju/Downloads/cocos2d-x-2.2.3/projects/Hello
Have Fun!
Jason:project-creator shengju$
Jason:projects shengju$ cd Hello/
Jason:Hello shengju$ ls
Classes proj.androidproj.ios proj.macproj.tizen proj.winrtproj.wp8-xaml
Resources proj.blackberryproj.linux proj.marmaladeproj.win32 proj.wp8
HelloWorld工程分析:
int main(int argc,char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
int retVal = UIApplicationMain(argc, argv,nil, @"AppController");
[poolrelease];
return retVal;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
window = [[UIWindowalloc] initWithFrame: [[UIScreenmainScreen] bounds]];
// 初始化EAGLView
EAGLView *__glView = [EAGLView viewWithFrame: [windowbounds]
pixelFormat: kEAGLColorFormatRGB565
depthFormat: GL_DEPTH24_STENCIL8_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
// 设置glView为根视图
viewController = [[RootViewControlleralloc] initWithNibName:nilbundle:nil];
viewController.wantsFullScreenLayout =YES;
viewController.view = __glView;
// 设置窗口的根视图控制器
if ( [[UIDevicecurrentDevice].systemVersionfloatValue] < 6.0)
{
// iOS 6以下使用addSubView
[window addSubview: viewController.view];
}
else
{
//IOS 6以上设置窗口的rootViewController属性
[windowsetRootViewController:viewController];
}
[windowmakeKeyAndVisible];
[[UIApplicationsharedApplication] setStatusBarHidden:true];
cocos2d::CCApplication::sharedApplication()->run();
return YES;
}
int CCApplication::run()
{
if (applicationDidFinishLaunching())
{
[[CCDirectorCallersharedDirectorCaller] startMainLoop];
}
return 0;
}
boolAppDelegate::applicationDidFinishLaunching() {
// 初始化导演
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// 开启fps显示
pDirector->setDisplayStats(true);
// 设置fps,如果没有调用改方法,默认值1.0/60,也就是每秒60帧
pDirector->setAnimationInterval(1.0 /60);
// 创建HelloWord场景,这是个自动释放的对象
CCScene *pScene = HelloWorld::scene();
// 运行场景
pDirector->runWithScene(pScene);
return true;
}
class HelloWorld :public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
我们看到HelloWorld类有四个方法,一个是初始化虚函数,一个是类方法用于创建场景,一个菜单的回调处理函数,最后一个方法是通过宏来封装的。/**
* define a create function for a specific type, such as CCLayer
* @__TYPE__ class type to add create(), such as CCLayer
*/
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1.调用父类初始化函数
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin();
/
// 2. 添加菜单项,点击后退出程序
//
// 为菜单项设置图标,点击后退出
CCMenuItemImage *pCloseItem =CCMenuItemImage::create(
"CloseNormal.png", //正常情况下的图标
"CloseSelected.png",//选中状态下的图标
this, //接收菜单点击事件的目标
menu_selector(HelloWorld::menuCloseCallback)); //菜单项点击后的回调函数
//设置菜单项的位置为右下角
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
//创建菜单
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,1);
/
// 3. add your codes below...
// 添加标签显示"Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel,1);
// 在屏幕中间设置精灵
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
//添加精灵到Layer下
this->addChild(pSprite,0);
return true;
}