首頁 > 資料庫 > mysql教程 > Cocos2d-x学习笔记(1)

Cocos2d-x学习笔记(1)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2016-06-07 15:01:12
原創
1076 人瀏覽過

Cocos2d-x的原型为Cocos2d,基于Cocos2d-iPhone,跨平台。 Hello Workd分析: 1、“resource”文件夹 该文件夹主要用于存放游戏中需要的图片、音频和配置等资源文件,可在其中创建子文件夹。“resource”文件夹可以为默认游戏运行时目录。 2、“include”和

        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游戏的通用入口文件,类似于Windows工程中主函数所在的文件。打开”AppDelegate.cpp“可以看到系统自动添加的代码,实现了AppDelegate类,这个类控制着游戏的生命周期,除去构造函数和析构函数外,共有三个方法。

        

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

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的一个子类,可以在子类中添加各种精灵和逻辑处理代码。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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类

1

 

登入後複製

       

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
cocos2d-x - 在MVC模式中如何設定計時器?
來自於 1970-01-01 08:00:00
0
0
0
objective-c - 如何在cocos2d中建立定時器?
來自於 1970-01-01 08:00:00
0
0
0
c++ - cocos2d沒辦法下載?求助! ! !
來自於 1970-01-01 08:00:00
0
0
0
objective-c - Cocos2d中如何實現zoom效果?
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板