在上一篇中,我们已经说过了初始化戏(Scene 类)的方式,以及让导演上演这场戏。
// create a scene. it's an autorelease object
Scene *scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
那么接下来,我们看看这场戏到底内部是执行流程的啊。
OK,首先看看HelloWorldScene.h 到底有什么东西。
静态创建函数
static cocos2d::Scene* createScene();
初始化
virtual bool init();
菜单的一个回调函数
void menuCloseCallback(cocos2d::Ref* pSender);
这个。。。。看宏定义上面的注释说是创建一个特定的类
CREATE_FUNC(HelloWorld);
/**
* define a create function for a specific type, such as Layer
* @param \__TYPE__ class type to add create(), such as Layer
*/
#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(HelloWorld);
就是相当于
在 HelloWorldScene.h 的定义
static HelloWorld* create();
在 HelloWorldScene.m 的实现
HelloWorld* HelloWorld::create()
{
//创建一个 HelloWorld 对象
HelloWorld* helloWorld = new HellWorld();
//判断 HelloWorld 对象是否创建以及初始化成功
if (helloWorld && helloWorld->init())
{
//创建成功,初始化成功后,让其自动释放内存
helloWorld->autorelease();
//返回 HelloWorld 实例
return helloWorld;
} else {
//如果创建失败,将安全删除 HelloWorld 对象
delete helloWorld;
helloWorld = NULL;
return NULL;
}
}
好了,有宏的话,让我们剩下了不少代码的工作量啊。
接下来我们看看其他的吧
HelloWorldScene.cpp 的里面的函数的执行顺序是
先
Scene* HelloWorld::createScene();
再
bool HelloWorld::init();
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auro layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
可以写成
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
Scene* scene = Scene::create();
// 'layer' is an autorelease object
Layer* 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 ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point 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));
closeItem->setPosition(Point(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(Point::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(Point(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(Point(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;
}
虽然这段很长,不过包含了很多要学习的知识。
从表面上看,这段代码都在讲初始化的那些事。
细心观察,这个和Objective-C 的 init 方法多类似啊,只是不是返回对象。
我们精简一下这段代码的框架
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
//初始化的内容
return true;
}
接下来看看里面进行的初始化的内容吧
向导演问了相关舞台的数据
Size visibleSize = Director::getInstance()->getVisibleSize();
Point 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));
closeItem->setPosition(Point(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(Point::ZERO);
this->addChild(menu, 1);
// 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
MenuItemImage* closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
Menu* menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
MenuItemImage 类 创建一个对象,放入两张图片,和一个回调函数。
第一张图片是正常状态的,第二张是选择状态时的,回调函数,this 应该是目标和iOS 创建按钮很相似,而区别是没有触发事件的手势设置。
接下来就是设置 MenuItemImage 类 实例的位置
通过 MenuItemImage 类 实例 创建一个 Menu 类的实例。
设置坐标
最后,将这个Menu类的实例加入当前 Layer中
接下来就是创建一个Label 类了。
根据官方发布文档所描述。3.0将采用一个Label 类 来创建不同类型的Label,而且优化了很多性能,这些也是后话了。
// 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(Point(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);
创建一个LabelTTF类的实例,参数1是内容,参数2是字体,参数3是字体大小
然后就是设置 这个实例的位置
然后加入层
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
我们接下来就是把加载一张图片,全屏显示
首先用精灵创建一个实例,参数是一张图片。
然后设置精灵的位置。
最后把精灵加入层里,
最后我们看看回调函数吧,当点击按钮时,就会触发这个回调函数,因为已经关联上了。
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
}
这里实现的功能很简单,就是退出应用程序而已。
好了,就这样就结束了。接下来就是详情了。
表面上看,Cocos2d-X 真的不难~~~
呵呵