头文件:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(Ref* pSender);
void movePicturetoRight(Ref* pSender);
void movePicturetoDown(Ref* pSender);
void movePicturetoUp(Ref* pSender);
void movePicturetoLeft(Ref* pSender);
// implement the "static create()" method manually
static HelloWorld* create()
{
HelloWorld* pHelloWorld = new HelloWorld();
if (pHelloWorld&&pHelloWorld->init())
{
pHelloWorld->autorelease();
return pHelloWorld;
}
else
{
CC_SAFE_DELETE(pHelloWorld);
return nullptr;
}
}
//CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
helloWorld继承layer基类
helloWorld创建过程:
首先调用静态方法creatScene()创建一个空场景,然后再调用静态方法create()创建HelloWorld层,并将其挂载到这个空场景上。
在调用helloWorld的create()方法中,调用helloWorld的init()方法,将helloWorld层中的所有元素进行初始化(加载图片,定位等)如下:
在加载前,应当先对父类进行加载。(类似于构造函数)
bool HelloWorld::init()
{
//
// 1. super init first
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
MenuItemImage* ItemMoveSpritToUp = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::movePicturetoUp, this));
ItemMoveSpritToUp->setPosition(Vec2(origin.x + visibleSize.width - ItemMoveSpritToUp->getContentSize().width / 2,
origin.y + ItemMoveSpritToUp->getContentSize().height*6));
// create menu, it's an autorelease object
Menu* menu = Menu::create(ItemMoveSpritToUp ,NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
Label* label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 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
this->addChild(label, 1);
// add "HelloWorld" splash screen"
Sprite* sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setAnchorPoint(Vec2(0.5, 0.5));
sprite->setPosition(Vec2(origin.x+sprite->getContentSize().width/2, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
/*this->addChild(sprite, 0);*/
this->addChild(sprite, 0, "sprite");
return true;
}
除了按钮,其他所有类都是直接挂载到helloWorld层上,按钮挂载在菜单上,菜单再挂载到helloWorld层 (调用addChild()方法)
setAnchorPoint()方法是设置锚点,setPosition是将锚点定在相应位置。sprite、label等都有相对应的create方法创建其对象。
HelloWorld中的create()方法中的pHelloWorld->autorelease();语句,是将HelloWorld对象丢进自动回收指针池。自动回收指针池的基本工作原理是清理一帧中没被使用的指针所对应的内存空间。
HelloWorld场景的调用:
在AppDelegate类中的applicationDidFinishLaunching()方法中的最后调用,代码如下:
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// run
director->runWithScene(scene);