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类,这个类控制着游戏的生命周期。除去构造函数和析构函数外,共同拥有三个方法。

  1. bool AppDelegate::applicationDidFinishLaunching() {
  2. // initialize director启动应用程序后将调用这种方法。默认的实现中已经包括了游戏启动后的必要准备
  3. CCDirector* pDirector = CCDirector::sharedDirector();//初始化游戏引擎控制器CCDirector。以便启动游戏引擎
  4. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
  5.  
  6. pDirector->setOpenGLView(pEGLView);
  7.  
  8. // turn on display FPS启用FPS显示
  9. pDirector->setDisplayStats(true);
  10.  
  11. // set FPS. the default value is 1.0/60 if you don't call this设置画图间隔。人眼的刷新频率为1/60秒。
  12. pDirector->setAnimationInterval(1.0 / 60);
  13.  
  14. // create a scene. it's an autorelease object创建一个场景
  15. CCScene *pScene = HelloWorld::scene();
  16.  
  17. // run执行场景
  18. pDirector->runWithScene(pScene);
  19.  
  20. return true;
  21. }
  22.  
  23. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  24. void AppDelegate::applicationDidEnterBackground() {//应用程序进入后台时。会调用这种方法。
  25.  
  26. CCDirector::sharedDirector()->stopAnimation();
  27.  
  28. // if you use SimpleAudioEngine, it must be pause
  29. // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  30. }
  31.  
  32. // this function will be called when the app is active again
  33. void AppDelegate::applicationWillEnterForeground() {//该方法与上个方法成对出现,应用程序返回前台时被调用。
  34. CCDirector::sharedDirector()->startAnimation();
  35.  
  36. // if you use SimpleAudioEngine, it must resume here
  37. // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  38. }

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

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

以下介绍一下:

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

  1. CCScene* HelloWorld::scene()
  2. {
  3. // 'scene' is an autorelease object
  4. CCScene *scene = CCScene::create();//创建场景
  5.  
  6. // 'layer' is an autorelease object
  7. HelloWorld *layer = HelloWorld::create();//创建子类层
  8.  
  9. // add layer as a child to scene
  10. scene->addChild(layer);将子类层加到主场景
  11.  
  12. // return the scene
  13. return scene;
  14. }

(2)bool init()初始化HelloWorld类

  1. bool HelloWorld::init()
  2. {
  3. //////////////////////////////
  4. // 1. super init first对父类进行初始化
  5. if ( !CCLayer::init() )
  6. {
  7. return false;
  8. }
  9.  
  10. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//
  11. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//
  12.  
  13. /////////////////////////////
  14. // 2. add a menu item with "X" image, which is clicked to quit the program
  15. // you may modify it.
  16. //创建菜单并加入到层
  17. // add a "close" icon to exit the progress. it's an autorelease object
  18. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  19. "CloseNormal.png",
  20. "CloseSelected.png",
  21. this,
  22. menu_selector(HelloWorld::menuCloseCallback));
  23. //设置关闭button位置
  24. pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
  25. origin.y + pCloseItem->getContentSize().height/2));
  26.  
  27. // create menu, it's an autorelease object
  28. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  29. pMenu->setPosition(CCPointZero);
  30. this->addChild(pMenu, 1);
  31.  
  32. /////////////////////////////
  33. // 3. add your codes below...
  34. //创建“Hello World”标签并加入到层中
  35. // add a label shows "Hello World"
  36. // create and initialize a label
  37.  
  38. CCLabelTTF* pLabel = CCLabelTTF::create("Yu xikuo", "Arial", 24);//创建的是yuxikuo的标签
  39.  
  40. // position the label on the center of the screen设置标签的位置
  41. pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
  42. origin.y + visibleSize.height - pLabel->getContentSize().height));
  43.  
  44. // add the label as a child to this layer创建的label到层中
  45. this->addChild(pLabel, 1);
  46.  
  47. // add "HelloWorld" splash screen"加入HelloWorld.png精灵到层中
  48. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  49.  
  50. // position the sprite on the center of the screen设置精灵在层中的位置
  51. pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  52.  
  53. // add the sprite as a child to this layer
  54. this->addChild(pSprite, 0);将精灵加入到层
  55.  
  56. return true;
  57. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Cocos2d-x学习笔记(1)的更多相关文章

  1. cocos2dx游戏开发——微信打飞机学习笔记(三)——WelcomeScene的搭建

    一.场景与层的关系: cocos2dx的框架可以说主要由导演,场景,层,精灵来构成: 1.其中导演,意如其名,就是操控整个游戏的一个单例,管理着整个游戏. 2.场景就像电影的一幕剧情,所以说,懂得如何 ...

  2. android cocos2d-x for Android安装和学习笔记(请用adt-bundle21.1或以上导入)

    引用:http://weimingtom.iteye.com/blog/1483566 (20121108)注意:这篇文章用cdt编译ndk工程的内容已过时(现在可以用adt-bundle,避免配置繁 ...

  3. [Cocos2d-x for WP8学习笔记] HelloWorld结构分析

    先来看一下目录结构: Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能 include:用于放置游戏头文件 Shaders:渲染器着色器文件(大雾) cocos2dorig. ...

  4. cocos2d-x实战 C++卷 学习笔记--第4章 字符串 __String类

    前言: <cocos2d-x实战C++卷>学习笔记.(cocos2d-x 是3.0版本) 介绍 cocos2d-x 通用的字符串类  __String . 使用cocos2d::__Str ...

  5. Cocos2d-x学习笔记(17)(TestCpp源代码分析-1)

    TestCpp源代码基于Cocos2d-x2.1.3版本号,部分资源来自红孩儿的游戏编程之路CSDN博客地址http://blog.csdn.net/honghaier/article/details ...

  6. 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴

    上一节学习了使用Cocos2d-x播放音乐的方法,但是那种方法一般只适合于播放较大的音乐,而一般比较短小的音乐(如游戏中的打斗.按键音效等)则要通过playEffect来播放.本节使用该方法以及之前学 ...

  7. 《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音

    <Cocos2d-x游戏开发实战精解>学习笔记1--在Cocos2d中显示图像 <Cocos2d-x游戏开发实战精解>学习笔记2--在Cocos2d-x中显示一行文字 之前的内 ...

  8. cocos2d-html5学习笔记(六)--alpha2中cc.Sequence.create中的bug

    cocos2d-html5学习笔记(六)--alpha2中cc.Sequence.create中的bug http://blog.csdn.net/allenice1/article/details/ ...

  9. 【cocos2d-x 3.x 学习笔记】对象内存管理

    内存管理 内存管理一直是一个不易处理的问题.开发人员必须考虑分配回收的方式和时机,针对堆和栈做不同的优化处理,等等.内存管理的核心是动态分配的对象必须保证在使用完成后有效地释放内存,即管理对象的生命周 ...

  10. ‎Cocos2d-x 学习笔记(20) ControlButton

    [Cocos2d-x 学习笔记 目录链接] 1. 简介 ControlButton实现了按钮功能,根据触摸的位置和移动的过程可识别9中EventType类型,执行对应的回调函数. 直接继承了Contr ...

随机推荐

  1. 别动我的奶酪:CSV文件数据丢零现象及对策

    CSV文件在读入EXCEL时,对于前面有零的数据项,比如电话号码,会自作聪明地丢掉那个零. 比如,我有一个北京客户,其号码为01059178888,如果这是通过CSV文件来的数据,在EXCEL中打开时 ...

  2. poj 3211 Washing Clothes(背包)

    很不错的01背包!!! 不过有点疑问!!!(注释) #include <algorithm> #include<stdio.h> #include<string.h> ...

  3. velocity中的velocityCounter不起作用的原因

    今天用org.springframework.ui.velocity.VelocityEngineFactoryBean 时,velocityCounter这个变量的时候死活不起作用,折腾了良久也不行 ...

  4. Swift - 使用storyboard创建表格视图(TableViewController)

    项目创建完毕后,默认是使用ViewController作为主界面视图.下面通过样例演示,如何使用TableViewController作为主界面视图,同时演示如何在storyboard中设置表格及内部 ...

  5. 终于懂了:Delphi消息的Result域出现的原因——要代替回调函数的返回值!(MakeObjectInstance不会帮助处理(接收)消息回调函数的返回值)

    MakeObjectInstance应该不会帮助处理(接收)消息回调函数的返回值,可是有时候又确实需要这个返回值,这可怎么办呢?我是看到这段文字的时候,想到这个问题的: 当WM_PAINT不是由Inv ...

  6. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

  7. 【PostgreSQL】PostgreSQL语法

    在阅读的过程中有不论什么问题.欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 一.PostgreSQL时间类型转换 --时间类型转成字符类型 select t ...

  8. HDU2602 Bone Collector 【01背包】

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. FZU1608(线段树)

    传送门:Huge Mission 题意:给定区间范围[0,N] (2 <= N <= 50000)和M个区间 (1 <= M <= 500000)和这些区间上的权值,求最终并区 ...

  10. Struts2 后台action接收 jsp页面中checkbox中的值

    如前端页面jsp中的标签为: <form action="myurl"> <input type="checkbox" name=" ...