问题
I'm creating a 2d platform game with cocos2d-x v3.2 (c++) and i'm using label.
cocos2d-x v3.0 (c++)
declared like cocos2d::LabelTTF* currentScore;
cocos2d-x v2.2.2 (c++)
declared like cocos2d::CCLabelTTF* currentScore;
cocos2d-x v3.2(c++) how to declare label in global class(helloworld.h) i have try like
HelloWorld.h
class HelloWorld : public cocos2d::LayerColor
{
public:
virtual bool init();
cocos2d::LabelTTF* currentScore; //semantic issue(LabelTTF deprecared)
};
#endif
HelloWorld.cpp
bool HelloWorld::init()
{
currentScore = LabelTTF::create("", "Arial", 40); //semantic issue(LabelTTF deprecared)
// position the label on the center of the screen
currentScore->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - currentScore->getContentSize().height));
// add the label as a child to this layer
this->addChild(currentScore, 1);
char buffer[10];
sprintf(buffer, "%04i",0);
currentScore->setString(std::string(buffer));
}
one more try again
HelloWorld.cpp
bool HelloWorld::init()
{
Auto currentScore = LabelTTF::create("", "Arial", 40);
//position the label on the center of the screen
currentScore->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - currentScore->getContentSize().height));
// add the label as a child to this layer
this->addChild(currentScore, 1);
}
#endif
it working but can't "Auto currentScore;" declared in global class(HelloWorld.h)
回答1:
In .h file
Label *autolabel4;
in .cpp
// autolabel4 = Label::create(); can't improve label size and fount
autolabel4 = Label::createWithSystemFont("hello","Arial.ttf",40);
autolabel4->setString("name isss :");
autolabel4->setColor(Color3B(23,33,44));
autolabel4->setPosition(Point(origin.x+ visibleSize.width/2,
origin.y + visibleSize.height - 400));
this->addChild(autolabel4, 1);
来源:https://stackoverflow.com/questions/27577619/how-to-declare-label-in-global-class-helloworld-h-with-cocos2d-x-v3-2