您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x学习笔记(1)

2015-07-05 13:29 531 查看
Cocos2d-x原型Cocos2d,基于Cocos2d-iPhone,跨平台。

Hello Workd分析:

1、“resource”目录

该目录主要用于存放游戏中须要的图片、音频和配置等资源文件。可在当中创建子目录。

“resource”目录能够为默认游戏执行时目录。

2、“include”和“source”目录

这两个目录用于放置游戏头文件和源码文件。项目模板中加入的main.h、main.cpp、resource.h是平台相关的。为Windows专有。

3、"AppDelegate.h“和”AppDelegate.cpp“

这个两个文件是Cocos2d-x游戏的通用入口文件。类似于Windowsproject中主函数所在的文件。打开”AppDelegate.cpp“能够看到系统自己主动加入的代码。实现了AppDelegate类,这个类控制着游戏的生命周期。除去构造函数和析构函数外,共同拥有三个方法。

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director启动应用程序后将调用这种方法。默认的实现中已经包括了游戏启动后的必要准备
CCDirector* pDirector = CCDirector::sharedDirector();//初始化游戏引擎控制器CCDirector。以便启动游戏引擎
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

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

// set FPS. the default value is 1.0/60 if you don't call this设置画图间隔。人眼的刷新频率为1/60秒。
pDirector->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object创建一个场景
CCScene *pScene = HelloWorld::scene();

// run执行场景
pDirector->runWithScene(pScene);

return true;
}

// 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();
}

// 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();
}


在第一个函数中能够加入pDirector->enableRetinaDisplay(true)用于开启高分辨率屏幕。

在HelloWorldScene.h与HelloWorldScene.cpp中定义了HelloWorld项目中默认的游戏场景。Cocos的游戏结构能够简单的概括为场景、层、精灵。HelloWorld类继承于CCLayer,因此HelloWorld本身是一个层,HelloWorld类包括一个静态函数和两个实例方法。

以下介绍一下:

(1)static cocos2d::CCScene* scene();是CCLayer的一个子类。能够在子类中加入各种精灵和逻辑处理代码。

CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();//创建场景

// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();//创建子类层

// add layer as a child to scene
scene->addChild(layer);将子类层加到主场景

// return the scene
return scene;
}
(2)bool init()初始化HelloWorld类

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first对父类进行初始化
if ( !CCLayer::init() )
{
return false;
}

CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//

/////////////////////////////
// 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));
//设置关闭button位置
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));

// 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...
//创建“Hello World”标签并加入到层中
// add a label shows "Hello World"
// create and initialize a label

CCLabelTTF* pLabel = CCLabelTTF::create("Yu xikuo", "Arial", 24);//创建的是yuxikuo的标签

// 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创建的label到层中
this->addChild(pLabel, 1);

// add "HelloWorld" splash screen"加入HelloWorld.png精灵到层中
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));

// add the sprite as a child to this layer
this->addChild(pSprite, 0);将精灵加入到层

return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: