HelloWorld入门

HelloWorld入门
    代码解释:
        AppDelegate.h类是Cocos2d-x引擎要求实现的游戏应用委托对象,在Cocos2d-x游戏
    运行的不同生命周期阶段会触发他的不同函数
        AppDelegate继承了cocos2d::Application,是Cocos2d-x引擎提供的基类

    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 Director.
10 */
11 class  AppDelegate : private cocos2d::Application
12 {
13 public:
14     AppDelegate();
15     virtual ~AppDelegate();
16
17     /**
18     @brief    Implement Director and Scene init code here.
19     @return true    Initialize success, app continue.
20     @return false   Initialize failed, app terminate.
21     */
22     /*
23         *游戏启动时调用的函数,在这里可以初始化导演对象和场景对象
24     */
25     virtual bool applicationDidFinishLaunching();
26
27     /**
28     @brief  The function be called when the application enter background
29     @param  the pointer of the application
30     */
31     /*
32         *游戏进入后台时调用的函数
33     */
34     virtual void applicationDidEnterBackground();
35
36     /**
37     @brief  The function be called when the application enter foreground
38     @param  the pointer of the application
39     */
40     /*
41         *游戏进入前台时调用的函数
42     */
43     virtual void applicationWillEnterForeground();
44 };
45
46 #endif // _APP_DELEGATE_H_

AppDelegate.cpp

 1 #include "AppDelegate.h"
 2 #include "HelloWorldScene.h"
 3
 4 USING_NS_CC;//是用来替换using namespace cocos2d
 5
 6 AppDelegate::AppDelegate() {
 7
 8 }
 9
10 AppDelegate::~AppDelegate()
11 {
12 }
13
14 bool AppDelegate::applicationDidFinishLaunching() {
15     // initialize director
16     //初始化director
17     auto director = Director::getInstance();
18     auto glview = director->getOpenGLView();
19     if (!glview) {
20         glview = GLView::create("My Game");
21         director->setOpenGLView(glview);//设置导演类的OpenGL视图
22     }
23
24     // turn on display FPS
25     //设置是否在屏幕上显示帧率等信息,一般是为了测试,实际发布时会影响游戏的外观,所以就不显示了
26     director->setDisplayStats(true);
27
28     // set FPS. the default value is 1.0/60 if you don‘t call this
29     //设定定时器1.0/60秒间隔一次,即设定帧率为60
30     director->setAnimationInterval(1.0 / 60);
31
32     // create a scene. it‘s an autorelease object
33     //创建场景的对象Sence
34     auto scene = HelloWorld::createScene();
35
36     // run
37     //运行该场景,使游戏进入该场景
38     director->runWithScene(scene);
39
40     return true;
41 }
42
43 // This function will be called when the app is inactive. When comes a phone call,it‘s be invoked too
44 void AppDelegate::applicationDidEnterBackground() {
45     //停止场景中的动画
46     Director::getInstance()->stopAnimation();
47
48     // if you use SimpleAudioEngine, it must be pause
49     //停止背景音乐
50     // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
51 }
52
53 // this function will be called when the app is active again
54 void AppDelegate::applicationWillEnterForeground() {
55     //开始游戏场景中的动画
56     Director::getInstance()->startAnimation();
57
58     // if you use SimpleAudioEngine, it must resume here
59     //继续背景音乐
60     // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
61 }

HelloWorld类继承cocos2d::Layer类,他被成为层(layer),这些层被放到场景(scene)场景类是cocos2d::Scene
        
        HelloWorldScene.h

 1 #ifndef __HELLOWORLD_SCENE_H__
 2 #define __HELLOWORLD_SCENE_H__
 3
 4 #include "cocos2d.h"
 5
 6 class HelloWorld : public cocos2d::Layer//继承了Layer是一个层不是场景
 7 {
 8 public:
 9     // there‘s no ‘id‘ in cpp, so we recommend returning the class instance pointer
10     // 声明创建当前层HelloWorld所在场景的静态函数createScene()
11     static cocos2d::Scene* createScene();
12
13     // Here‘s a difference. Method ‘init‘ in cocos2d-x returns bool, instead of returning ‘id‘ in cocos2d-iphone
14     virtual bool init();//初始化层的实例函数
15
16     // a selector callback
17     void menuCloseCallback(cocos2d::Ref* pSender);//声明菜单回掉函数,用于触摸菜单时间的回掉
18
19     // implement the "static create()" method manually
20     CREATE_FUNC(HelloWorld);//创建一个静态函数create,可以创建层
21 };
22
23 #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

  1 #include "HelloWorldScene.h"
  2
  3 USING_NS_CC;
  4
  5 /*
  6     *做了三件事,首先创建HelloWorld层所在的场景对象
  7     *其次创建类HelloWorld层
  8     *最后将HelloWorld层添加到场景scene中
  9 */
 10 Scene* HelloWorld::createScene()
 11 {
 12     // ‘scene‘ is an autorelease object
 13     auto scene = Scene::create();//创建HelloWorld层所在的场景对象
 14
 15     // ‘layer‘ is an autorelease object
 16     auto layer = HelloWorld::create();//创建类HelloWorld层
 17
 18     // add layer as a child to scene
 19     scene->addChild(layer);//最后将HelloWorld层添加到场景scene中
 20
 21     // return the scene
 22     return scene;
 23 }
 24
 25 // on "init" you need to initialize your instance
 26 bool HelloWorld::init()
 27 {
 28     //////////////////////////////
 29     // 1. super init first
 30     //初始化父类Layer
 31     if ( !Layer::init() )
 32     {
 33         return false;
 34     }
 35
 36     //定义视图的可视化尺寸
 37     Size visibleSize = Director::getInstance()->getVisibleSize();
 38     //定义视图的可视化原点
 39     Vec2 origin = Director::getInstance()->getVisibleOrigin();
 40
 41     /////////////////////////////
 42     // 2. add a menu item with "X" image, which is clicked to quit the program
 43     //    you may modify it.
 44
 45     // add a "close" icon to exit the progress. it‘s an autorelease object
 46     //增加一个菜单项,单击它的时候退出程序
 47     //创建一个图片菜单项对象,单击该菜单项的时候回掉menuCloseCallback函数
 48     auto closeItem = MenuItemImage::create(
 49                                            "CloseNormal.png",
 50                                            "CloseSelected.png",
 51                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
 52     //菜单项的位置
 53     closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
 54                                 origin.y + closeItem->getContentSize().height/2));
 55
 56     // create menu, it‘s an autorelease object
 57     //创建Menu菜单对象
 58     auto menu = Menu::create(closeItem, NULL);
 59     //定义菜单对象的位置
 60     menu->setPosition(Vec2::ZERO);
 61     //把菜单项对象添加到当前层上
 62     this->addChild(menu, 1);
 63
 64     /////////////////////////////
 65     // 3. add your codes below...
 66
 67     // add a label shows "Hello World"
 68     // create and initialize a label
 69     //在下面添加自己的代码
 70     //创建一个LabelTTF标签对象
 71     auto label = LabelTTF::create("Hello World", "Arial", 24);
 72
 73     // position the label on the center of the screen
 74     //设置标签对象位置为水平居中,在垂直方向上与屏幕顶对齐
 75     label->setPosition(Vec2(origin.x + visibleSize.width/2,
 76                             origin.y + visibleSize.height - label->getContentSize().height));
 77
 78     // add the label as a child to this layer
 79     //将文本对象添加到层中
 80     this->addChild(label, 1);
 81
 82     // add "HelloWorld" splash screen"
 83     //创建精灵Sprite对象
 84     auto sprite = Sprite::create("HelloWorld.png");
 85
 86     // position the sprite on the center of the screen
 87     //设置精灵对象的位置,是屏幕的中央
 88     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
 89
 90     // add the sprite as a child to this layer
 91     //将精灵添加到层中
 92     this->addChild(sprite, 0);
 93
 94     return true;
 95
 96 }
 97
 98
 99 void HelloWorld::menuCloseCallback(Ref* pSender)
100 {
101     //CC_TARGET_PLATFORM读取当前运行的平台的宏
102 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
103     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
104     return;
105 #endif
106
107     Director::getInstance()->end();
108
109 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
110     exit(0);
111 #endif
112 }
时间: 2024-08-07 09:59:32

HelloWorld入门的相关文章

Hiberate HelloWorld入门

通过两种方式来创建项目,一种是用xml方式来描述映射关系,一种是用Annation方式来进行描述 1 建立新java 项目,名为hibernate_0100_HelloWorld 2 学习建User-library-hibernate,并加入相应的jar包 a) 项目右键-buildpath-configure build path-add library— b) 选择User-library,在其中新建 libraray,命名为 hibernate c) 在该library中加入hiberna

.Net 转战 Android 4.4 日常笔记(2)--HelloWorld入门程序

原文:.Net 转战 Android 4.4 日常笔记(2)--HelloWorld入门程序 我不知道人们为什么那么喜欢用HelloWorld来做为自己的第一个程序入门,为什么不是hello **其他的东西或者hi. 一.打开ADT 的Eclipse开发工具新建一个Android项目 New----> Android Application Project Minimum Required SDK这个是运行hello world的最低android版本 Target SDK是现在的目标版本 Co

SpringMVC系列(二)HelloWorld入门

有关SpringMVC环境的搭建和Demo网上比比皆是,这里不再赘述,本文的侧重点将放在:配置文件的理解! Hello World入门主要分为以下步骤: 1.准备开发环境和运行环境-->2.前端控制器的配置-->3.页面控制器-->4.开发视图页面-->5.启动服务器运行调试 1.准备开发环境和运行环境 开发工具:Eclipse 运行环境:Tomcat 工程:springmvc 依赖jar包(放在WEB-INF/lib下): 1.Spring框架的jar包 2.框架依赖jar包 日

.Net 转战 Android 4.4 日常笔记--HelloWorld入门程序(2)

我不知道人们为什么那么喜欢用HelloWorld来做为自己的第一个程序入门,为什么不是hello **其他的东西或者hi. 一.打开ADT 的Eclipse开发工具新建一个Android项目 New----> Android Application Project Minimum Required SDK这个是运行hello world的最低android版本 Target SDK是现在的目标版本 Compile With是编译版本 Theme是主题 一般我是初学者默认下一步就可以了,点了都不知

AliOS-Things Visual studio code helloworld 入门

AliOS Things 完成第一个应用:Hello World 全局掌控 从Git上下载源码 用VSCode打开源码,查看源码的目录结构 打开HelloWorld 确认手中的硬件.零妖的硬件型号是 Developer Kit . 编译源码 确认硬件,烧录代码.打开串口查看程序输出的信息. 从Git上下载源码 新建一个文件夹,用来存放源码.比如我在电脑的F盘根目录下,新建一个 AliOS_Source 的文件夹. 打开这个文件夹,然后打开命令行.如果你是WIN7等系统,可以打开CMD命令行,进入

HelloWorld入门程序

程序开发步骤说明 开发环境已经搭建完毕,可以开发我们第一个Java程序了.Java程序开发三步骤:编写.编译.运行. 代码实现 public class HelloWorld { //程序的起点 public static void main(String[] args) { System.out.println("hello world"); } } 入门程序说明: 编译和运行是两回事编译:是指将我们编写的Java源文件翻译成JVM认识的class文件,在这个过程中, javac 编

HelloWorld入门代码

A:定义类 B:写main方法 C:写输出语句 D:Java程序开发运行与工作原理 E:编译和运行程序 class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld"); } } 常见问题: A:找不到文件 a:文件扩展名隐藏导致编译失败 b:文件名写错了 B:单词拼写问题( a:class写成Class b:String写成string c:System写成syst

3、HelloWorld入门程序

3.1 程序开发步骤说明 开发环境搭建完毕后,可以开发我们第一个Java程序了. Java程序开发三步骤:编写.编译.运行. 3.2 编写Java源程序 在编写java源程序时,使用记事本就可以了,新建文件夹HelloWorld,后缀名改为.java. 打开该文件,输入: 1 public class HelloWorld{ 2 public static void main(String[] args){ 3 System.out.println("Hello,World!"); 4

Eclipse安装springsource-tool-suite插件及spring helloworld入门实例

一.查看eclipse版本 Help-->About Eclipse,我的版本是4.4.2. 二.根据eclipse 版本,选择插件版本 访问网址:http://spring.io/tools/sts/all 查看eclipse对应的插件版本 eclipsep安装spring插件有两种方式:在线安装和本地安装: 1.在线安装 Help-->Install New Software-->work with 中输入http://dist.springsource.com/release/TO