cocos-2dx引擎helloWorld文件初步认识

头文件:

#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);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值