Cocos2d-x学习笔记(一)环境搭建与项目创建
可运行的代码可以说明一切问题。
环境需安装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
- #ifndef _APP_DELEGATE_H_
- #define _APP_DELEGATE_H_
- #include "cocos2d.h"
- /**
- @brief The cocos2d Application.
- The reason for implement as private inheritance is to hide some interface call by CCDirector.
- */
- class AppDelegate : private cocos2d::CCApplication
- {
- public:
- AppDelegate();
- virtual ~AppDelegate();
- /**
- @brief Implement CCDirector and CCScene init code here.
- @return true Initialize success, app continue.
- @return false Initialize failed, app terminate.
- */
- virtual bool applicationDidFinishLaunching();
- /**
- @brief The function be called when the application enter background
- @param the pointer of the application
- */
- virtual void applicationDidEnterBackground();
- /**
- @brief The function be called when the application enter foreground
- @param the pointer of the application
- */
- virtual void applicationWillEnterForeground();
- };
- #endif // _APP_DELEGATE_H_
AppDelegate.cpp
- #include "AppDelegate.h"
- #include "HelloWorldScene.h"
- USING_NS_CC;
- AppDelegate::AppDelegate() {
- // Constructor
- }
- AppDelegate::~AppDelegate()
- {
- // Desctructor
- }
- // After launching, enter the function below
- bool AppDelegate::applicationDidFinishLaunching() {
- // initialize director
- CCDirector* pDirector = CCDirector::sharedDirector();
- CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
- // Set OpenGL view
- pDirector->setOpenGLView(pEGLView);
- // turn on display FPS (Frame per sesond)
- pDirector->setDisplayStats(true);
- // set FPS. the default value is 1.0/60 if you don't call this
- pDirector->setAnimationInterval(1.0 / );
- // create a scene. it's an autorelease object
- CCScene *pScene = HelloWorld::scene();
- // run the created scene
- // Really start from here
- 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
- // Enter background
- void AppDelegate::applicationDidEnterBackground() {
- CCDirector::sharedDirector()->stopAnimation();
- // if you use SimpleAudioEngine, it must be paused
- // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- }
- // this function will be called when the app is active again
- // Enter foreground
- void AppDelegate::applicationWillEnterForeground() {
- CCDirector::sharedDirector()->startAnimation();
- // if you use SimpleAudioEngine, it must resume here
- // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- }
HelloWorldScene.h
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
- virtual bool init();
- // there's no 'id' in cpp, so we recommend returning the class instance pointer
- static cocos2d::CCScene* scene();
- // a selector callback
- void menuCloseCallback(CCObject* pSender);
- // implement the "static node()" method manually
- CREATE_FUNC(HelloWorld);
- };
- #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
- #include "HelloWorldScene.h"
- USING_NS_CC;
- 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;
- }
- // on "init" you need to initialize your instance
- 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));
- pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/ ,
- origin.y + pCloseItem->getContentSize().height/));
- // create menu, it's an autorelease object
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu, );
- /////////////////////////////
- // 3. add your codes below...
- // add a label shows "Hello World"
- // create and initialize a label
- CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", );
- // position the label on the center of the screen
- pLabel->setPosition(ccp(origin.x + visibleSize.width/,
- origin.y + visibleSize.height - pLabel->getContentSize().height));
- // add the label as a child to this layer
- this->addChild(pLabel, );
- // add "HelloWorld" splash screen"
- CCSprite* pSprite = CCSprite::create("HelloWorld.png");
- // position the sprite on the center of the screen
- pSprite->setPosition(ccp(visibleSize.width/ + origin.x, visibleSize.height/ + origin.y));
- // add the sprite as a child to this layer
- this->addChild(pSprite, );
- return true;
- }
- 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();
- #endif
- #endif
- }
main.cpp
- #include "main.h"
- #include "AppDelegate.h"
- #include "CCEGLView.h"
- USING_NS_CC;
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // create the application instance
- AppDelegate app;
- CCEGLView* eglView = CCEGLView::sharedOpenGLView();
- eglView->setViewName("FirstGame");
- eglView->setFrameSize(, );
- return CCApplication::sharedApplication()->run();
- }
Cocos2d-x学习笔记(一)环境搭建与项目创建的更多相关文章
- Android Studio 学习笔记(一)环境搭建、文件目录等相关说明
Android Studio 学习笔记(一)环境搭建.文件目录等相关说明 引入 对APP开发而言,Android和iOS是两大主流开发平台,其中区别在于 Android用java语言,用Android ...
- Ionic2开发环境搭建、项目创建调试与Android应用的打包、优化
Ionic2开发环境搭建.项目创建调试与Android应用的打包.优化. windows下ionic2开发环境配置步骤如下: 下载node.js环境,稳定版本:v6.9.5 下载android stu ...
- 我的Java学习笔记 -开发环境搭建
开始学习Java~ 一.Java简介 Java编程语言是一种简单.面向对象.分布式.解释型.健壮安全.与系统无关.可移植.高性能.多线程和动态的语言. Java分为三个体系: JavaSE(J2SE) ...
- Django学习笔记 开发环境搭建
为什么使用django?1.支持快速开发:用python开发:数据库ORM系统,并不需要我们手动地构造SQL语句,而是用python的对象访问数据库,能够提升开发效率.2.大量内置应用:后台管理系统a ...
- cocos2d-x lua 学习笔记(1) -- 环境搭建
Cocos2d-x 3.0以上版本的环境搭建和之前的Cocos2d-x 2.0 版差异较大的,同时从Cocos2d-x 3.0项目打包成apk安卓应用文件,搭建安卓环境的步骤有点繁琐,但搭建一次之后, ...
- SpringData JPA的学习笔记之环境搭建
一.环境搭建 1.加入jar包 spring jar+jpa jar +springData jar >>SpringData jar包 2.配置applicationCont ...
- Mybatis-Plus 实战完整学习笔记(二)------环境搭建
第二章 使用实例 1.搭建测试数据库 -- 创建库 CREATE DATABASE mp; -- 使用库 USE mp; -- 创建表 CREATE TABLE tbl_employee( ...
- Mybatis学习笔记之---环境搭建与入门
Mybatis环境搭建与入门 (一)环境搭建 (1)第一步:创建maven工程并导入jar包 <dependencies> <dependency> <groupId&g ...
- 前端框架vue学习笔记:环境搭建
兼容性 不兼容IE8以下 Vue Devtools 能够更好的对界面进行审查和调试 环境搭建 1.nodejs(新版本的集成了npm)[npm是node包管理 node package manager ...
- Web安全测试学习笔记 - vulhub环境搭建
Vulhub和DVWA一样,也是开源漏洞靶场,地址:https://github.com/vulhub/vulhub 环境搭建过程如下: 1. 下载和安装Ubuntu 16.04镜像,镜像地址:htt ...
随机推荐
- Session实例
Session常用方法(一) session对象用来保存一些在与每个用户回话期间需要保存的数据信息,这样就方便了回话期间的一些处理程序.如可以用session变量记住用户的用户名,以后就不必在其他的网 ...
- Oracle数据库返回字符类型-1~1的结果处理
如果实体类中定义的字段是String类型,Oracle数据库中返回的是数字类型,那么Oracle返回0.xxx的时候会丢失前面的0. 要想不丢失0,那么数据库返回的就要是字符串类型的,所以要将返回值转 ...
- C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法
对于无标题栏窗体,也就是FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还 ...
- 2:5 视图控制器result的配置
result: 1:其实底层还是使用原来servlet的转发和重定向的方法: 2:redirectAction:只能定位到 action (比如下面name属性为 *User 的Action ,但 ...
- wonderware historian 10安装配置
安装文件为: 关闭用户控制 配置dcom. 安装.net framework 3.5 安装sql server,打sp1补丁 安装Historain 停止ww服务 安装sp1包 重启机器,启动ww服务 ...
- FastDFS+nginx+keepalived集群搭建
安装环境 nginx-1.6.2 libfastcommon-master.zip FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastd ...
- zookeeper 详解
是 分布式 协调 服务. ZK的工作:注册:所有节点向ZK争抢注册,注册成功会建立一套节点目录树,先注册的节点为Active节点,后注册节点成为standby;监听事件:节点在ZK集群里注册监听动作: ...
- pythonl类继承例子
#coding=utf-8 class Person(object): def __init__(self,name,age): self.name=name sel ...
- 如何合并两个Git仓库
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- 实用的4~20mA输入/0~5V输出的I/V转换电路(转)
源: 实用的4~20mA输入/0~5V输出的I/V转换电路