cocos2d-x 3.0的入门程序:helloworld
看过了这么多不同方向的应用,发现很多程序入门都是helloworld
helloworld是所有程序员的绝对初恋
先看一下程序的运行结果吧
然后就是他的工程代码
工程的目录有两个
Classes:程序中的类
AppDelegate.h/cpp:Cocos2d-x程序框架
AppMacros.h:所用到的宏,主要是设置分辩率及对应的资源目录
HelloWorldScene.h/cpp:场景显示层
win32:WIN32程序所涉及的主函数
main.cpp:winMain主函数
WinMain函数:
#include "main.h"
#include "AppDelegate.h"
#include "cocos2d.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;
//运行创建程序
return Application::getInstance()->run();
}
一切都被封装到程序类AppDelegate中,这是一个基于Cocos2d-x的cocos2d::Application类的派生类。
它将程序框架封装为一个类,提供了统一的多平台上基本程序框架的实现。
AppDelegate.cpp:
#include "AppDelegate.h"
#include "HelloWorldScene.h" USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate()
{
} bool AppDelegate::applicationDidFinishLaunching() {
// initialize director创建导演
auto director = Director::getInstance();
//创建opengl窗口
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
} // turn on display FPS 打开FPS
director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call thi 1秒60帧
director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object创建场景和层
auto scene = HelloWorld::createScene(); // run启动场景
director->runWithScene(scene); 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() {
Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
//当电话完成,选择恢复游戏时,响应这句
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
下面我们来看一下HelloWorld场景,它是一个基于cocos2d::Layer的派生类。cocos2d::Layer是什么?在这里,我想打个比方来建立一些基本的认知,比方说我们生活在地球上,地球属于宇宙内的一部分。从Cocos2d-x的框架体系来看,我们是Sprite精灵,地球是Layer,而宇宙是Scene。
一个程序要想表现出精彩的世界,要先建立一个宇宙Scene,然后增加地球,月球,太阳等Layer,然后在这些Layer上增加相应的物体。而我们站在地球上,地球运动,我们也会跟着一起运动。
OK,现在我们来看一下如何创建Scene和Layer:
HelloWorldScene.h:
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
auto 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 ( !Layer::init() )
{
return false;
}
//得到屏幕的大小,得到原点
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// // 创建一个菜单项,它由两张图片来表现普通状态和按下状态,设置按下时调用menuCloseCallback函数响应关闭
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
//指定菜单位置,菜单项
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object菜单项放到菜单里
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);//放到当前的层 /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label
//创建标签
auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen标签位置
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer将标签放到层中
this->addChild(label, 1); // add "HelloWorld" splash screen"创建图片精灵
auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen精灵位置
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer将精灵放到层中
this->addChild(sprite, 0); return true;
} //点close菜单项的时候来回调的
void HelloWorld::menuCloseCallback(Ref* pSender)
{
////如果是WP8平台,弹出消息框提示一下。
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
//终止程序。
Director::getInstance()->end();
//如果是ios平台
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
Layer中增加了精灵,按钮,文字等表现物,有了这些表现物,一个Layer才有价值。
参考:http://blog.csdn.net/honghaier/article/details/24518863(谢谢)
cocos2d-x 3.0的入门程序:helloworld的更多相关文章
- Java入门程序HelloWorld
程序开发步骤说明 开发环境已经搭建完毕,可以开发我们第一个Java程序了.Java程序开发三步骤:编写.编译.运行.如下图所示 详解: 编写源程序:通俗来说就是我们通过Java的语法自己写的代码 编译 ...
- 【Irrlicht鬼火引擎】掌握引擎使用流程,入门程序HelloWorld
分析 一.简述使用步骤 一般而言,对于一个简单的程序,Irrlicht引擎的一般使用步骤如下: 预处理:(1)包含 <irrlicht.h> 头文件#include <irrlich ...
- asp.net mvc3.0第一个程序helloworld开发图解
步骤一:新建asp.net mvc3.0项目 (选择Razor模板) 步骤二:创建控制器 步骤三:控制器源码内右键创建对应视图 步骤四:控制器内添加代码 步骤五:视图页面输出内容 步骤六:F5调试
- Webstorm10.0.3破解程序及汉化包下载、Webstorm配置入门指南
核心提示: WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaSscri ...
- ArcGIS for Android入门程序之DrawTool2.0
来自:http://blog.csdn.net/arcgis_mobile/article/details/8084763 GISpace博客<ArcGIS for Android入门程序之Dr ...
- scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld
scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld 学习了: http://blog.csdn.net/wangmuming/article/details/3407911 ...
- 01-java前言、入门程序、变量、常量
今日目标 能够计算二进制和十进制数之间的互转 能够使用常见的DOS命令 理解Java语言的跨平台实现原理 jvm是运行java程序的假想计算机,所有的java程序都运行在它上面.java编写的软件可以 ...
- SpringMVC系列(一)SpringMVC概述和搭建SpringMVC的第一个helloWord入门程序
一.SpringMVC 概述 • Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一 • Spring3.0 后全面超越 Struts2,成为最优秀的 MVC ...
- ant入门程序
一. ant简单介绍 Ant是apache的一个核心项目, 它的作用是项目自己主动化构建, 由于它内置了Javac.Java.创建文件夹.拷贝文件等功能, 直接执行build.xml文件就能够编译我们 ...
随机推荐
- Appium 滑动界面swipe用法
Appium 滑动API:Swipe(int start x,int start y,int end x,int y,duration) 解释:int start x-开始滑动的x坐标, int st ...
- dell Nx000系列交换机
dell n2048(P) dell n3048(P) dell n4064(F) P: PoE+ F: SFP+ Model GbE 10GbE(SFP+) 40GbE(QSFP+) Layer d ...
- mybase修改内部文件免费使用
关于mybase的介绍就不多说了,下载后一般只有30天的使用期限.以下方法可以无限次使用该软件(当然,每隔一个周期就需要修改myBase.ini) 原文博客详见:https://www.cnblogs ...
- Linux读取NTFS类型数据盘
Windows的文件系统通常使用NTFS或者FAT32格式,而Linux的文件系统格式通常是EXT系列,请参考下面方法: 1) 在Linux系统上使用以下命令安装ntfsprogs软件使得Linux能 ...
- PostgresQL中的NUlls first/last功能
Nulls first/last功能简介Nulls first/last功能主要用于order by排序子句中,影响空值Null在排序结果中的位置.简单来说,Nulls first表示Null值在排序 ...
- wxWidgets窗口类型
如果在创建窗口的时候你没有指定窗口的边框类型,那么在不同的平台上将会有不同的边框类型的缺省值.在windows平台上,控件边框的缺省值为 wxSUNKEN_BORDER,意为使用当前系统风格的边框.你 ...
- javascript中parseInt(),08,09,返回0
javascript中在使用parseInt(08).parseInt(09),进行整数转换的时候,返回值是0 工具/原料 浏览器 文本编辑器 方法/步骤 javascript中在使用pa ...
- this指针和类的继承
神秘的家伙 在对象的世界里,有一个特殊的指针,它叫做this.我们从来没有见过他,但是他却从来都存在.我们通过一个典型的例子来认识它: class Human { char fishc; Human( ...
- matlab linux下无界面运行
今日做吸引域的仿真,由于需要遍历100*100*100的空间,需要的时间比较长,发现程序没运行一段时间,就会出现Out of memory的错误,而且出错的部分在于截取figure内部图片的部分. 开 ...
- 洛谷P1762 偶数(找规律)
题目描述 给定一个正整数n,请输出杨辉三角形前n行的偶数个数对1000003取模后的结果. 输入输出格式 输入格式: 一个数 输出格式: 结果 输入输出样例 输入样例#1: 复制 6 输出样例#1: ...