cocos2d-x 3.0的入门程序:helloworld
来源:互联网 发布:mysql查询最近一周 编辑:程序博客网 时间:2024/06/06 02:56
转自:http://www.cnblogs.com/zsboy/p/3925098.html
看过了这么多不同方向的应用,发现很多程序入门都是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 toovoid 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 againvoid 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 instancebool 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
- cocos2d-x 3.x HelloWorld入门
- cocos2d-x的Helloworld程序运行流程(win32)
- 编译Cocos2d-x的HelloWorld
- Cocos2d-x 的“HelloWorld” 深入分析
- Cocos2d-x 的“HelloWorld” 深入分析
- Cocos2d-x 的“HelloWorld” 深入分析
- Cocos2d-x 的“HelloWorld” 深入分析
- Cocos2d-x HelloWorld的全面解析
- Cocos2d-x 的“HelloWorld” 深入分析
- Cocos2d-x 的“HelloWorld” 深入分析
- 建立Cocos2d-x版的HelloWorld
- Cocos2d-x版的HelloWorld工程分析
- 建立Cocos2d-x版的HelloWorld
- Cocos2d-x版的HelloWorld工程分析
- [Cocos2d-x]创建一个新的HelloWorld
- 建立Cocos2d-3.x版的HelloWorld
- Cocos2d-x 3.0正式版 HelloWorld分析
- 《C++语言基础》程序阅读——深复制、友元、模板
- iOS开发网络请求中ASIHTTPRequest的用法整理
- 《C++语言基础》实践项目——深复制、友元、模板
- The best way to deal with large files in Python style
- 函数指针类的虚函数表是一块连续的内存,每个内存单元中记录一个JMP指令的地址。 注意的是,编译器会为每个有虚函数的类创建一个虚函数表,该虚函数表将被该类的所有对象共享。类的每个虚成员占据虚函数表中的一
- cocos2d-x 3.0的入门程序:helloworld
- php开发之文件指针,文件锁定
- Robotium 5.0.1 源码解析之控件搜索
- Cocos2d-x 资源加密解密实践总结
- PHP基础之使用DOM解析xml
- Android 引导界面的实现过程
- Android应用的自动升级、更新模块的实现
- android中用Spannable在TextView中设置超链接、颜色、字体
- oracle创建表空间,创建用户笔记