解释cocos2dx的HelloWorldScene的类
在之前的AppDelegate中的
//这里可以换场景de // create a scene. it's an autorelease object CCScene *pScene = HelloWorld::scene(); // run pDirector->runWithScene(pScene);
默认导演将Hello World 这个场景展示在模拟器中
注释HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
//初始化函数
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
//静态创建函数
static cocos2d::CCScene* scene();
// a selector callback
//menu 菜单的一个回调函数 用于响应CCMenuItemImage
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for "static create()" constructor ( node() deprecated )
//备注1
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp注释
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* HelloWorld::scene() { //创建一个场景 // 'scene' is an autorelease object CCScene *scene = CCScene::create(); //创建一个HelloWorld的层 // 'layer' is an autorelease object HelloWorld *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 ( !CCLayer::init() ) { return false; } ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object //将一个相片创建菜单项 并调用方法menuCloseCallback CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); //开关相片的位置 pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); //将一个已经创建好的菜单项加入菜单中 并声称一个菜单对象的实例 // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); // 设置菜单的位置 pMenu->setPosition( CCPointZero ); // 将菜单加入当前的layer里 并设置z轴为1 this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label //创建一个文本对象 并设置字体和大小 CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // 获取当前设备的尺寸 // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); //设置文本的位置 // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer // 将文本放大layer层中 和menu的设置轴是一样的 就是同一层 this->addChild(pLabel, 1); // add "HelloWorld" splash screen" // 设置一个精灵 并且放入图片 CCSprite* pSprite = CCSprite::create("HelloWorld.png"); //精灵的位置 // position the sprite on the center of the screen pSprite->setPosition( ccp(size.width/2, size.height/2) ); //把精灵放到layer中去 但是在底层 // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { // 程序退出 CCDirector::sharedDirector()->end(); //判定是否为ios设备 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
相信能看明白