可运行的代码可以说明一切问题。

环境需安装VS201x + Python2.7 + Cocos2d-x-2.2.5。(Linux下参考链接:http://www.cocos2d-x.org/wiki/How_to_run_cpp-tests_on_Linux)

1. 点击cocos2d-win32.vc201x.sln,打开后,编译(可以查看一下,例子项目的依赖项);

2. TestCpp中,查看各种特性的例子与效果(从这里能更快速学习);

3. 进入目录cocos2d-x-2.2.5\tools\project-creator,使用脚本create_project.py来创建项目;一般将在cmd命令窗口下创建。有机会,可以看看该python脚本的内容,大概200行左右;具体操作如下图所示:

  

  这里,我们创建了一个名为FirstGame的项目,使用语言是C++。结果:创建出了各个平台的项目。这里选在Win32上开发(无强制要求)。

  

  Resources目录中存资源;Classes目录中存代理和场景定义源文件且被其它项目共享;剩余目录均为项目目录。

  创建成功后的解决方案目录结构如下:

  

  至此,一个项目即创建成功。从下边几个文件的命名即可看出其中的逻辑:AppDelegate的作用就是一个桥梁,HelloWorldScene就是一个场景的简单示例,而main则是程序的入口。

  下边会项目中重要的几个文件的内容代码贴在下边(部分文件我也添加了一部分注释):

  AppDelegate.h

  1. #ifndef _APP_DELEGATE_H_
  2. #define _APP_DELEGATE_H_
  3.  
  4. #include "cocos2d.h"
  5.  
  6. /**
  7. @brief The cocos2d Application.
  8.  
  9. The reason for implement as private inheritance is to hide some interface call by CCDirector.
  10. */
  11. class AppDelegate : private cocos2d::CCApplication
  12. {
  13. public:
  14. AppDelegate();
  15. virtual ~AppDelegate();
  16.  
  17. /**
  18. @brief Implement CCDirector and CCScene init code here.
  19. @return true Initialize success, app continue.
  20. @return false Initialize failed, app terminate.
  21. */
  22. virtual bool applicationDidFinishLaunching();
  23.  
  24. /**
  25. @brief The function be called when the application enter background
  26. @param the pointer of the application
  27. */
  28. virtual void applicationDidEnterBackground();
  29.  
  30. /**
  31. @brief The function be called when the application enter foreground
  32. @param the pointer of the application
  33. */
  34. virtual void applicationWillEnterForeground();
  35. };
  36.  
  37. #endif // _APP_DELEGATE_H_

  AppDelegate.cpp

  1. #include "AppDelegate.h"
  2. #include "HelloWorldScene.h"
  3.  
  4. USING_NS_CC;
  5.  
  6. AppDelegate::AppDelegate() {
  7. // Constructor
  8. }
  9.  
  10. AppDelegate::~AppDelegate()
  11. {
  12. // Desctructor
  13. }
  14.  
  15. // After launching, enter the function below
  16. bool AppDelegate::applicationDidFinishLaunching() {
  17. // initialize director
  18. CCDirector* pDirector = CCDirector::sharedDirector();
  19. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
  20. // Set OpenGL view
  21. pDirector->setOpenGLView(pEGLView);
  22.  
  23. // turn on display FPS (Frame per sesond)
  24. pDirector->setDisplayStats(true);
  25.  
  26. // set FPS. the default value is 1.0/60 if you don't call this
  27. pDirector->setAnimationInterval(1.0 / );
  28.  
  29. // create a scene. it's an autorelease object
  30. CCScene *pScene = HelloWorld::scene();
  31.  
  32. // run the created scene
  33. // Really start from here
  34. pDirector->runWithScene(pScene);
  35.  
  36. return true;
  37. }
  38.  
  39. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  40. // Enter background
  41. void AppDelegate::applicationDidEnterBackground() {
  42. CCDirector::sharedDirector()->stopAnimation();
  43.  
  44. // if you use SimpleAudioEngine, it must be paused
  45. // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  46. }
  47.  
  48. // this function will be called when the app is active again
  49. // Enter foreground
  50. void AppDelegate::applicationWillEnterForeground() {
  51. CCDirector::sharedDirector()->startAnimation();
  52.  
  53. // if you use SimpleAudioEngine, it must resume here
  54. // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  55. }

HelloWorldScene.h

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. class HelloWorld : public cocos2d::CCLayer
  7. {
  8. public:
  9. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  10. virtual bool init();
  11.  
  12. // there's no 'id' in cpp, so we recommend returning the class instance pointer
  13. static cocos2d::CCScene* scene();
  14.  
  15. // a selector callback
  16. void menuCloseCallback(CCObject* pSender);
  17.  
  18. // implement the "static node()" method manually
  19. CREATE_FUNC(HelloWorld);
  20. };
  21.  
  22. #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

  1. #include "HelloWorldScene.h"
  2.  
  3. USING_NS_CC;
  4.  
  5. CCScene* HelloWorld::scene()
  6. {
  7. // 'scene' is an autorelease object
  8. CCScene *scene = CCScene::create();
  9.  
  10. // 'layer' is an autorelease object
  11. HelloWorld *layer = HelloWorld::create();
  12.  
  13. // add layer as a child to scene
  14. scene->addChild(layer);
  15.  
  16. // return the scene
  17. return scene;
  18. }
  19.  
  20. // on "init" you need to initialize your instance
  21. bool HelloWorld::init()
  22. {
  23. //////////////////////////////
  24. // 1. super init first
  25. if ( !CCLayer::init() )
  26. {
  27. return false;
  28. }
  29.  
  30. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  31. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  32.  
  33. /////////////////////////////
  34. // 2. add a menu item with "X" image, which is clicked to quit the program
  35. // you may modify it.
  36.  
  37. // add a "close" icon to exit the progress. it's an autorelease object
  38. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  39. "CloseNormal.png",
  40. "CloseSelected.png",
  41. this,
  42. menu_selector(HelloWorld::menuCloseCallback));
  43.  
  44. pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/ ,
  45. origin.y + pCloseItem->getContentSize().height/));
  46.  
  47. // create menu, it's an autorelease object
  48. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  49. pMenu->setPosition(CCPointZero);
  50. this->addChild(pMenu, );
  51.  
  52. /////////////////////////////
  53. // 3. add your codes below...
  54.  
  55. // add a label shows "Hello World"
  56. // create and initialize a label
  57.  
  58. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", );
  59.  
  60. // position the label on the center of the screen
  61. pLabel->setPosition(ccp(origin.x + visibleSize.width/,
  62. origin.y + visibleSize.height - pLabel->getContentSize().height));
  63.  
  64. // add the label as a child to this layer
  65. this->addChild(pLabel, );
  66.  
  67. // add "HelloWorld" splash screen"
  68. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  69.  
  70. // position the sprite on the center of the screen
  71. pSprite->setPosition(ccp(visibleSize.width/ + origin.x, visibleSize.height/ + origin.y));
  72.  
  73. // add the sprite as a child to this layer
  74. this->addChild(pSprite, );
  75.  
  76. return true;
  77. }
  78.  
  79. void HelloWorld::menuCloseCallback(CCObject* pSender)
  80. {
  81. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  82. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  83. #else
  84. CCDirector::sharedDirector()->end();
  85. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  86. exit();
  87. #endif
  88. #endif
  89. }

main.cpp

  1. #include "main.h"
  2. #include "AppDelegate.h"
  3. #include "CCEGLView.h"
  4.  
  5. USING_NS_CC;
  6.  
  7. int APIENTRY _tWinMain(HINSTANCE hInstance,
  8. HINSTANCE hPrevInstance,
  9. LPTSTR lpCmdLine,
  10. int nCmdShow)
  11. {
  12. UNREFERENCED_PARAMETER(hPrevInstance);
  13. UNREFERENCED_PARAMETER(lpCmdLine);
  14.  
  15. // create the application instance
  16. AppDelegate app;
  17. CCEGLView* eglView = CCEGLView::sharedOpenGLView();
  18. eglView->setViewName("FirstGame");
  19. eglView->setFrameSize(, );
  20. return CCApplication::sharedApplication()->run();
  21. }

Cocos2d-x学习笔记(一)环境搭建与项目创建的更多相关文章

  1. Android Studio 学习笔记(一)环境搭建、文件目录等相关说明

    Android Studio 学习笔记(一)环境搭建.文件目录等相关说明 引入 对APP开发而言,Android和iOS是两大主流开发平台,其中区别在于 Android用java语言,用Android ...

  2. Ionic2开发环境搭建、项目创建调试与Android应用的打包、优化

    Ionic2开发环境搭建.项目创建调试与Android应用的打包.优化. windows下ionic2开发环境配置步骤如下: 下载node.js环境,稳定版本:v6.9.5 下载android stu ...

  3. 我的Java学习笔记 -开发环境搭建

    开始学习Java~ 一.Java简介 Java编程语言是一种简单.面向对象.分布式.解释型.健壮安全.与系统无关.可移植.高性能.多线程和动态的语言. Java分为三个体系: JavaSE(J2SE) ...

  4. Django学习笔记 开发环境搭建

    为什么使用django?1.支持快速开发:用python开发:数据库ORM系统,并不需要我们手动地构造SQL语句,而是用python的对象访问数据库,能够提升开发效率.2.大量内置应用:后台管理系统a ...

  5. cocos2d-x lua 学习笔记(1) -- 环境搭建

    Cocos2d-x 3.0以上版本的环境搭建和之前的Cocos2d-x 2.0 版差异较大的,同时从Cocos2d-x 3.0项目打包成apk安卓应用文件,搭建安卓环境的步骤有点繁琐,但搭建一次之后, ...

  6. SpringData JPA的学习笔记之环境搭建

    一.环境搭建 1.加入jar包   spring jar+jpa jar +springData jar >>SpringData jar包     2.配置applicationCont ...

  7. Mybatis-Plus 实战完整学习笔记(二)------环境搭建

     第二章    使用实例   1.搭建测试数据库 -- 创建库 CREATE DATABASE mp; -- 使用库 USE mp; -- 创建表 CREATE TABLE tbl_employee( ...

  8. Mybatis学习笔记之---环境搭建与入门

    Mybatis环境搭建与入门 (一)环境搭建 (1)第一步:创建maven工程并导入jar包 <dependencies> <dependency> <groupId&g ...

  9. 前端框架vue学习笔记:环境搭建

    兼容性 不兼容IE8以下 Vue Devtools 能够更好的对界面进行审查和调试 环境搭建 1.nodejs(新版本的集成了npm)[npm是node包管理 node package manager ...

  10. Web安全测试学习笔记 - vulhub环境搭建

    Vulhub和DVWA一样,也是开源漏洞靶场,地址:https://github.com/vulhub/vulhub 环境搭建过程如下: 1. 下载和安装Ubuntu 16.04镜像,镜像地址:htt ...

随机推荐

  1. Session实例

    Session常用方法(一) session对象用来保存一些在与每个用户回话期间需要保存的数据信息,这样就方便了回话期间的一些处理程序.如可以用session变量记住用户的用户名,以后就不必在其他的网 ...

  2. Oracle数据库返回字符类型-1~1的结果处理

    如果实体类中定义的字段是String类型,Oracle数据库中返回的是数字类型,那么Oracle返回0.xxx的时候会丢失前面的0. 要想不丢失0,那么数据库返回的就要是字符串类型的,所以要将返回值转 ...

  3. C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法

    对于无标题栏窗体,也就是FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还 ...

  4. 2:5 视图控制器result的配置

    result:   1:其实底层还是使用原来servlet的转发和重定向的方法: 2:redirectAction:只能定位到 action (比如下面name属性为 *User 的Action ,但 ...

  5. wonderware historian 10安装配置

    安装文件为: 关闭用户控制 配置dcom. 安装.net framework 3.5 安装sql server,打sp1补丁 安装Historain 停止ww服务 安装sp1包 重启机器,启动ww服务 ...

  6. FastDFS+nginx+keepalived集群搭建

    安装环境 nginx-1.6.2 libfastcommon-master.zip FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastd ...

  7. zookeeper 详解

    是 分布式 协调 服务. ZK的工作:注册:所有节点向ZK争抢注册,注册成功会建立一套节点目录树,先注册的节点为Active节点,后注册节点成为standby;监听事件:节点在ZK集群里注册监听动作: ...

  8. pythonl类继承例子

    #coding=utf-8 class Person(object):    def __init__(self,name,age):        self.name=name        sel ...

  9. 如何合并两个Git仓库

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  10. 实用的4~20mA输入/0~5V输出的I/V转换电路(转)

    源: 实用的4~20mA输入/0~5V输出的I/V转换电路