3.1 第一个场景 HelloWorldScene
2013-11-11 23:38
267 查看
HelloWorldScene.h
HelloWorldScene.cpp
这是一个 cocos2d-x 引擎使用者 最初级的入门篇章, 上面的注释已经很清楚了,没什么难点,你会看懂的 believe yourself!!! Come on , baby.
下面看一下 CREATE_FUNC 这个宏的定义:
看似高端 大气 上档次 , 其实也很容易理解
定义 CREATE_FUNC
创建实例(new)
判断成功与否(if) 自动释放内存(autorelease) 返回实例(return)
创建失败(else) 删除实例(delete)
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "Box2D/Box2D.h" #include "SimpleAudioEngine.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 recommand to return the exactly class 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 namespace cocos2d; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { //p1 = this; ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 50)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel, 1); // 3. Add add a splash screen, show the cocos2d splash image. CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen pSprite->setPosition(ccp(size.width/2, size.height/2)); // Add the sprite to HelloWorld layer as a child layer. this->addChild(pSprite, 0); bRet = true; } while (0); return bRet; } void HelloWorld::menuCloseCallback(CCObject* pSender) { // "close" menu item clicked CCDirector::sharedDirector()->end(); }
这是一个 cocos2d-x 引擎使用者 最初级的入门篇章, 上面的注释已经很清楚了,没什么难点,你会看懂的 believe yourself!!! Come on , baby.
下面看一下 CREATE_FUNC 这个宏的定义:
/** * define a create function for a specific type, such as CCLayer * @__TYPE__ class type to add create(), such as CCLayer */ #define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \ __TYPE__ *pRet = new __TYPE__(); \ if (pRet && pRet->init()) \ { \ pRet->autorelease(); \ return pRet; \ } \ else \ { \ delete pRet; \ pRet = NULL; \ return NULL; \ } \ }
看似高端 大气 上档次 , 其实也很容易理解
定义 CREATE_FUNC
创建实例(new)
判断成功与否(if) 自动释放内存(autorelease) 返回实例(return)
创建失败(else) 删除实例(delete)
相关文章推荐
- 模仿HelloWorldScene,写第一个Scene
- 第一个java程序"HelloWorld"
- Java基础教程1:环境配置及第一个HelloWorld.java
- 第一个驱动helloworld module加载i…
- Android开发之旅----第一个程序Hello,world
- [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序
- 第一个atlas "Hello world"
- Linux下G++编译第一个C++程序(Hello, world)
- 我的第一个程序:hello_world(基于at91sam9g20)
- 第2步:第一个“Hello,world!”之Android App(从零开始学Android)
- Spring配置及第一个Spring HelloWorld
- Java环境变量设置后,编写第一个程序报错:找不到或无法加载主类 D:\java\HelloWorld
- 开始第一个小程序,打印“hello,world”
- MiniUI快速入门教程(二)编写第一个MiniUI程序:Hello, world!
- Eclipse + vs2013 编写第一个JNI HelloWorld
- 黑马程序员-----java基础01第一个应用程序 "HelloWorld"
- 初学atlas之(一):第一个atlas "Hello world"
- mina推送场景小hello world demo
- Linux下G++编译第一个C++程序(Hello, world)
- 解析HelloWorldScene