我有一个GameUI.h
,Round1.cpp
和GameUI.cpp
文件。
在GameUI.h中,我声明了:
static cocos2d::sprite* battery;
Round1.cpp
和GameUI.cpp
都包含GameUI.h
,这会产生多重定义问题并提供
链接器命令失败,退出代码为1 - 错误
GameUI.h文件是
#ifndef __GAME_UI_H__
#define __GAME_UI_H__
#include "cocos2d.h"
#include "common.h"
class GameUI : public cocos2d::Layer
{
public:
static cocos2d::Sprite *_batteryPannel;
};
#endif
GameUI.cpp文件是
#include <GameUI.h>
USING_NS_CC;
Node* GameUI::createBatteryPanel()
{
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto batteryNode = Node::create();
_batteryPannel = CommonFunc::createSpriteFromCache("BatteryPanel.png", 0, 0);
batteryNode->addChild(_batteryPannel, 2);
return batteryNode;
}
我有Round1.cpp文件
#include "Round1.h"
#include "GameUI.h"
USING_NS_CC;
// on "init" you need to initialize your instance
bool RoundA::init()
{
auto batteryPosition = GameUI::_batteryPanel->getPosition();
}