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 returnscene;23 }24
25 //on "init" you need to initialize your instance
26 boolHelloWorld::init()27 {28 //29 //1. super init first30 //初始化父类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 program43 //you may modify it.44
45 //add a "close" icon to exit the progress. it's an autorelease object46 //增加一个菜单项,单击它的时候退出程序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 object57 //创建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 label69 //在下面添加自己的代码70 //创建一个LabelTTF标签对象
71 auto label = LabelTTF::create("Hello World", "Arial", 24);72
73 //position the label on the center of the screen74 //设置标签对象位置为水平居中,在垂直方向上与屏幕顶对齐
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 layer79 //将文本对象添加到层中
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 screen87 //设置精灵对象的位置,是屏幕的中央
88 sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 +origin.y));89
90 //add the sprite as a child to this layer91 //将精灵添加到层中
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 }