我想通过将每个图层设置为cpp文件来在HelloWorldScene.cpp中安排许多图层。但我不知道如何开始编写头文件以及如何将它们包含在HelloWorld.cpp中,尽管我在Google和Google上搜索了几天。 StakOverflow ..为了解释我想做什么,我在下面做了一个简单的例子。我在这里只添加了2个图层,但在这1个场景中将有超过20个图层,因此我想将它们作为文件制作。
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
cocos2d::Layer* layer01;
cocos2d::Layer* layer02;
int GrossiniPops;
cocos2d::Sprite* pMan;
virtual void getTouched01();
virtual void closingLayer();
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
layer01 = Layer::create();
this->addChild(layer01);
layer02 = LayerColor::create(Color4B::WHITE);
this->addChild(layer02);
layer02->setVisible(false);
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto btn01 = cocos2d::ui::Button::create();
btn01->loadTextures("images/moveBar.png","","");
btn01->addTouchEventListener(CC_CALLBACK_0(HelloWorld::getTouched01, this));
btn01->setPosition(Point(275,250));
layer01->addChild(btn01);
auto btn02 = cocos2d::ui::Button::create();
btn02->loadTextures("images/moveBar.png", "", "");
btn02->addClickEventListener(CC_CALLBACK_0(HelloWorld::closingLayer, this));
btn02->setPosition(Point(375, 250));
this->addChild(btn02, 3);
GrossiniPops = 0;
pMan = Sprite::create("images/grossini.png");
pMan->setPosition(Point(325, 150));
layer01->addChild(pMan);
pMan->setVisible(false);
return true;
}
void HelloWorld::getTouched01()
{
GrossiniPops++;
if (GrossiniPops%4==2) {
pMan->setVisible(true);
}
else { pMan->setVisible(false); }
}
void HelloWorld::closingLayer()
{
log("Layer Change");
if (layer01->isVisible()==true)
{
layer01->setVisible(false);
layer02->setVisible(true);
}
else {
layer02->setVisible(false);
layer01->setVisible(true);
}
}
答案 0 :(得分:0)
您需要包含" HelloWorld.h"文件在你的" HelloWorld.cpp"文件,如果你想使用它。把这一行放在" HelloWorld.cpp"的顶部。文件:
#include "HelloWorld.h"
那应该解决它。
如果您想在C ++中阅读有关包含和源文件的更多信息,那么这里有一个很好的参考: