USING_NS_CC;//??等价于using namespace cocos2d;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease(??自动释放,如果创建失败则break) object
auto scene = Scene::create();//??auto自由索引变量
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);//??sence调用公有函数
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
<span style="white-space:pre"> </span>//调用父类的初始化函数,失败则返回false
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.
// 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));//??this为禁用时的函数,绑定回调函数
<span style="white-space:pre"> </span>//??设置菜单项位置(Vec2是用来创建坐标点的)
//??设置图片居中
<span style="white-space:pre"> </span>closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
<span style="white-space:pre"> </span>//用这个菜单项来创建一个菜单menu
auto menu = Menu::create(closeItem, NULL);
<span style="white-space:pre"> </span>//设置菜单位于零点x=0,y=0;
menu->setPosition(Vec2::ZERO);
<span style="white-space:pre"> </span>//把menu加到当前layer
this->addChild(menu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
// 创建一个文本文档label,内容是“Hello World”字体是“Arial”,字号是24;
auto label = LabelTTF::create("Hello World", "Cursive", 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(把label加到当前layer)
this->addChild(label, 1);
// add "HelloWorld" splash screen"
<span style="white-space:pre"> </span>//使用HelloWorld.png创建一个精灵
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;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
<span style="white-space:pre"> </span>MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}"
USING_NS_CC;//??等价于using namespace cocos2d;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease(??自动释放,如果创建失败则break) object
auto scene = Scene::create();//??auto自由索引变量
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);//??sence调用公有函数
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
//调用父类的初始化函数,失败则返回false
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.
// 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));//??this为禁用时的函数,绑定回调函数
//??设置菜单项位置(Vec2是用来创建坐标点的)
//??设置图片居中
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
//用这个菜单项来创建一个菜单menu
auto menu = Menu::create(closeItem, NULL);
//设置菜单位于零点x=0,y=0;
menu->setPosition(Vec2::ZERO);
//把menu加到当前layer
this->addChild(menu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
// 创建一个文本文档label,内容是“Hello World”字体是“Arial”,字号是24;
auto label = LabelTTF::create("Hello World", "Cursive", 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(把label加到当前layer)
this->addChild(label, 1);
// add "HelloWorld" splash screen"
//使用HelloWorld.png创建一个精灵
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;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#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();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}