目录结构:
Box2D | 物理引擎Box2D的相关文件 |
Chipmunk | 物理引擎Chipmunk的相关文件 |
cocos2dx | cocos2d-x引擎的核心,存放引擎的大部分源文件 |
CocosDenshion | 音频模块相关源文件 |
Debug.win32 | 在Windows上的调试输出目录 |
Doxygen | 生成doxygen项目文档时需要的配置文件 |
HelloWorld | HelloWorld的源代码 |
Hellolua | lua的示例代码 |
Lua | lua脚本支持的源码 |
Js | Js脚本支持的源码 |
Licences | 许可文件的目录 |
Template | 包括编译Ios和Android平台开发时的配置文件 |
testjs | cocos2d-x引擎js语言的API示例代码 |
tests | cocos2d-x引擎的所有API示例代码 |
Tools | 包括“Tolua的配置文件”和“Xcode4的模版生成工具” |
1.类AppDelegate //控制游戏生命周期
类AppDelegate继承了CCApplication,定义了三个方法:
virtualbool applicationDidFinishLaunching(); //响应窗口启动完成后的工作
virtualvoid applicationDidEnterBackground(); //响应窗口进入后台的工作
virtualvoid applicationWillEnterForeground(); //响应窗口从后台恢复的工作
I 启动时初始化的工作
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize Director
CCDirector* pDirector =CCDirector::sharedDirector(); // 场景管理器
CCEGLView* pEGLView =CCEGLView::sharedOpenGLView(); //创建视口
pDirector->setOpenGLView(pEGLView); //设置OpenGL视口
// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height, kResolutionNoBorder); //设置分辨率大小
CCSize frameSize =pEGLView->getFrameSize();
vector<string> searchPath; //资源路径
//在不同的平台下加载不同的资源
if (frameSize.height> mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height,largeResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is larger than the height of small resource size, select mediumresource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height,mediumResource.size.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium resource size, select smallresource.
else
{
searchPath.push_back(smallResource.directory);
pDirector->setContentScaleFactor(MIN( smallResource.size.height/designResolutionSize.height , smallResource.size.width/designResolutionSize.width ) );
}
//设置资源文件路径
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn ondisplay FPS
pDirector->setDisplayStats(true); //开启显示FPS
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 /60); // 设置帧速率 ,最好不要低于30帧
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); //调用静态方法创建一个场景,HelloWorld中会负责场景的实现
pDirector->runWithScene(pScene); //导演调用,运行HelloWorld中的场景。
return true;
}
II 暂停动作
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->stopAnimation(); //暂停活动
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //暂停背景音乐
}
III 恢复动作
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->startAnimation(); // 恢复活动
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // 恢复背景音乐
}
2.HelloWorld
CCScene*HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); //创建场景
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //创建图层
// add layer as achild to scene
scene->addChild(layer); //添加图层作为节点
// return the scene
return scene;
}
bool HelloWorld::init()
{
// 1. super initfirst
if (!CCLayer::init() ) //创建图层
{
return false;
}
CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize(); //获取大小
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin(); //获取原点,原点(origin.x , origin.y)在左下角
CCMenuItemImage *pCloseItem =CCMenuItemImage::create( "CloseNormal.png, "CloseSelected.png", this,
menu_selector( HelloWorld::menuCloseCallback ) ); //创建关闭按钮 ,在这里的图片路径如果不是在/Resources 目录下,则图片不能使用。 如果要在Resources目下放置文件夹,则需要在字符串中加入路径名称,如”/raster-32x32/ 32x32.png“
//设置按钮位置,可以看出 Cocos2d-x的坐标原点是左下角
pCloseItem->setPosition(ccp(origin.x+ visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y +pCloseItem->getContentSize().height/2));
// visibleSize.width 屏幕的宽度
// pCloseItem->getContentSize().width/2 是图片宽度的1/2
// pCloseItem->getContentSize().height/2 是图片高度的1/2
// pCloseItem->setPosition(ccp(origin.x , origin.y ) ); // pCloseItem的坐标设置是以默认锚点为坐标中心,即锚点在(0,0),此时按钮只能显示1/4
//创建菜单,将关闭按钮加入到菜单项
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero); // 设置菜单的位置
this->addChild(pMenu,1); // 将菜单加入到HelloWorld图层中
//创建 HelloWorld 文本
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",TITLE_FONT_SIZE);
// position thelabel on the center of the screen
pLabel->setPosition(ccp(origin.x +visibleSize.width/2,
origin.y +visibleSize.height - pLabel->getContentSize().height)); //设置文本(Label)的位置 ,坐标是字符串中心的坐标,并不是最开始的位置
// add the labelas a child to this layer
this->addChild(pLabel,1); // 将文本(Label)加入到HelloWorld图层中
//创建精灵图片
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position thesprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y)); // 设置图片精灵的位置
// add the spriteas a child to this layer
this->addChild(pSprite,0); // 将图片精灵加入到HelloWorld图层中 (第二个参数是Z轴坐标:0在底层,1在上层 。 Z轴坐标系参考笛卡尔右手坐标系(正方向:x轴向右,y轴向上,z轴向外)
//设置为相同的Z轴坐标时,按加载顺序显示,先加载的先显示,后加载的后显示
return true;
}
IIII 点击按钮结束的回调函数
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif