gpt4 book ai didi

c++ - cocos2d-x 中所有场景都可以访问的对象

转载 作者:行者123 更新时间:2023-11-28 06:09:29 25 4
gpt4 key购买 nike

我有自己的 ClientGame 类,我想添加一个此类的对象,cocos2d-x 中的所有 Scenes 都可以访问它。我尝试了以下方法,但它们不起作用。

1) 我创建了一个 BaseScene 类,其中包含我的类对象并继承了 cocos2d::Layer。然后,每个场景都继承这个BaseScene。它编译,但窗口屏幕只是显示并立即关闭。

2) 我试图同时继承cocos2d::LayerBaseScene。它编译,但产生与上述相同的结果。

创建一个所有场景都可以访问的对象的最佳方法是什么?这是我的代码。

BaseScene.h

#include "cocos2d.h"
#include "ClientGame.h"


class BaseScene
{
public:

private:
ClientGame mClientGame;

};

ClientGame.h

class ClientGame
{
public:
ClientGame(void);
~ClientGame(void);
ClientNetwork* network;
void sendActionPackets(int action);
void sendLineDestroyed();
char network_data[MAX_PACKET_SIZE];
void update();
void clientLoop();

};

IntroScene.h

#include "cocos2d.h"
#include "BaseScene.h"


class IntroScene : public cocos2d::Layer, public BaseScene
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);

// implement the "static create()" method manually
CREATE_FUNC(IntroScene);

cocos2d::Sprite* pTetrisLogo;
cocos2d::Sprite* pCloseNormal;

void SinglePlay(Ref* pSender);
void MultiPlay(Ref* pSender);
void Exit(Ref* pSender);

// void ImageButton(Ref* pSender);

};

最佳答案

因为我还没有足够的声誉来发表评论,所以在这里发帖。

您是否有任何特定原因希望它被继承?你不能把它变成Singleton吗?相反,如果您的代码中所有类的对象都需要访问它?

关于c++ - cocos2d-x 中所有场景都可以访问的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31577398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com