0
点赞
收藏
分享

微信扫一扫

Cocos2d-x创建Scene Demo


前提

请参考​​《Ubuntu18.04搭建Cocos2d开发环境》​​建立好工程

开始开发第一个场景

我们Cocos2d-x项目建在目录:/home/kyun/Desktop/Games/MyGame

第一步:添加以下资源到/home/kyun/Desktop/Games/MyGame/Resources目录下

Cocos2d-x创建Scene Demo_2d

#### 第三步:修改/home/kyun/Desktop/Games/MyGame/CMakeLists.txt文件:

修改前:

# add cross-platforms source files and header files 
list(APPEND GAME_SOURCE
Classes/AppDelegate.cpp
Classes/HelloWorldScene.cpp
)
list(APPEND GAME_HEADER
Classes/AppDelegate.h
Classes/HelloWorldScene.h
)

修改后:

# add cross-platforms source files and header files 
list(APPEND GAME_SOURCE
Classes/AppDelegate.cpp
Classes/HelloWorldScene.cpp
Classes/GraphicsScene.cpp
)
list(APPEND GAME_HEADER
Classes/AppDelegate.h
Classes/HelloWorldScene.h
Classes/GraphicsScene.h
)

第三步:编写.h文件和.cpp文件

GraphicsScene.h:

#include "cocos2d.h"

class GraphicsScene : public cocos2d::Layer{

public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(GraphicsScene);

};

GraphicsScene.cpp:

#include "GraphicsScene.h"

USING_NS_CC;

Scene* GraphicsScene::createScene(){
auto scene = Scene::create();
auto layer = GraphicsScene::create();
scene->addChild(layer);
return scene;
}

bool GraphicsScene::init() {
if(!Layer::init()){
return false;
}
auto sprite = Sprite::create("logo.png");
sprite->setPosition(0,0);
this->addChild(sprite);
return true;
}

第四步:修改AppDelegate.cpp

bool AppDelegate::applicationDidFinishLaunching() {
...

// create a scene. it's an autorelease object
// auto scene = HelloWorld::createScene();
auto scene = GraphicsScene::createScene();
// run
director->runWithScene(scene);

return true;
}

最后,运行就可以了。

分析GraphicsScene中的方法

GraphicsScene.h

  • 写头文件的目的是为了GraphicsScene.cpp的功能可以在其他.cpp文件里使用,否则不需要头文件。
  • 包含#include "cocos2d.h"进来
  • 定义GraphicsScene类,并public继承Layer
  • 在公开的方法中,定义一个static的createScene()方法,返回Scene引用,我们将在这个方法里创建我们的场景,并返回场景。
  • 定义一个虚函数init(),我们将在这里初始化我们的GraphicsScene类
  • 使用了一个宏CREATE_FUNC,这个宏是帮我们创建static的create()方法的,CREATE_FUNC的定义如下:

#define

那么CREATE_FUNC(GraphicsScene)展开后就是:

static GraphicsScene* create() 
{
GraphicsScene *pRet = new(std::nothrow) GraphicsScene();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = nullptr;
return nullptr;
}
}

因此我们可以知道上面这个方法,其实是在对象初始化后,帮助我们标记对象为自动释放。使用这个宏CREATE_FUNC,可以做到保证每个对象都调用了autorelease(),当对象引用计数为0时就会被自动释放。这几个方法的执法过程如下:


1.调用

2.调用

3.返回GraphicsScene

createScene

create

init


GraphicsScene.cpp

在init方法中:

sprite->setPosition(0,0);

有几点要注意的:

  • setPosition(0,0)的坐标系原点在屏幕的左下角。
  • 这个方法默认是相对对象的几何中心来定位的,如果要改变这个相对定位的“点”,可以通过对象的setAnchorPoint()来改变。
  • Sprite的setPosition()是相对它的“父母”的,在本列中,sprite的父母是Layer,如:

bool GraphicsScene::init() {
if(!Layer::init()){
return false;
}
auto sprite = Sprite::create("logo.png");
auto sprite2 = Sprite::create("logo2.png");
Vec2 origin = Director::getInstance()->getVisibleOrigin();
sprite->setAnchorPoint(Vec2(0.0,0.0));
sprite2->setAnchorPoint(Vec2(0.0,0.0));
sprite->addChild(sprite2);
sprite->setPosition(50+origin.x,50+origin.y);
sprite2->setPosition(0+origin.x,0+origin.y);
this->addChild(sprite);
return true;
}

此时sprite2的父母是sprite,那么sprite2在setPosition时,就会相对sprite来定位。由此可知孩子定位是相对于父母的。

谢谢阅读


举报

相关推荐

0 条评论