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

时间: 2025-01-04 21:20:44

cocos2d-x 3.0的入门程序:helloworld的相关文章

asp.net mvc3.0第一个程序helloworld开发图解

步骤一:新建asp.net mvc3.0项目 (选择Razor模板) 步骤二:创建控制器 步骤三:控制器源码内右键创建对应视图 步骤四:控制器内添加代码 步骤五:视图页面输出内容 步骤六:F5调试

病毒入门程序

#include "windows.h" #define UINT8 unsigned char /* 该函数构造一个简单的溢出函数 */ void GetName(UINT8 *pucSrcName, UINT8 *pucDstName) { while ((*pucSrcName) != 0xfe) { *pucDstName++ = *pucSrcName++; } *pucDstName = 0; } /* 在此函数中,调用后GetName弹出时,注入程序接管 */ void

scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld

IDE选择并下载: scala for eclipse 下载: http://scala-ide.org/download/sdk.html 根据自己的机器配置选择合适的IDE: 我这里选择For scala2.11 版本的Windows 32 bit的IDE,单击即下载. scala安装: 安装包下载地址,进入官网:http://www.scala-lang.org/ 进入DOWNLOAD下,选择scala 2.11 版本,单击下载: Windows上安装scala 2.11: 单击运行sca

.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

ArcGIS for Android入门程序之DrawTool2.0

来自:http://blog.csdn.net/arcgis_mobile/article/details/8084763 GISpace博客<ArcGIS for Android入门程序之DrawTool>http://blog.csdn.net/gispace/article/details/6723459 在ArcGIS Android SDK 0.9版本实现绘制各种几个图形.ArcGIS Android SDK目前版本为2.0,较之前版本变化较大,故将之前版本移植到2.0版本下.源代码

HelloWorld入门程序

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

EJB3.0快速入门

1.首先介绍运行环境及相关的配置: EJB的运行环境: JAVAEE应用服务器包含Web容器和EJB容器,EJB3.0应用需要运行在EJB容器里. Tomcat目前只是Web容器,它不能运行EJB应用. Jboss作为最常用EJB容器,其自身所带Web服务器部分就是直接使用Tomcat(Jboss的默认端口也为:8080). 相关配置: 1.配置classpath:%JDK安装目录%/lib/dt.jar和tools.jar 2.JDK版本需要1.5以上. 3.为Jboss设置Jboss_HOM

struts2入门程序

1.搭建struts2环境开发的步骤 搭建struts2环境时,我们一般需要做一下几个步骤的工作: 1.  创建javaweb工程 2.  找到开发struts应用所需要使用的jar 3.  创建jsp文件 4.  创建action文件 5.  编写struts2的配置文件 6.  在web.xml中加入struts2 MVC框架启动配置 开发struts2中需要使用的基本jar包: 关于struts2的提示: 我的myeclipes默认的struts2提示只到2.1,如果需要有2.3的提示,有

java入门程序、常量、变量

1. Java语言的发展历史 1.什么是Java语言 Java语言是美国Sun公司(Stanford University Network),在1995年推出的高级的编程语言.所谓编程语言,是 计算机的语言,人们可以使用编程语言对计算机下达命令,让计算机完成人们需要的功能. 2.Java语言发展历史 1995年Sun公司发布Java1.0版本 1997年发布Java 1.1版本 1998年发布Java 1.2版本 2000年发布Java 1.3版本 2002年发布Java 1.4版本 2004年