如何使用 Cocos2D-X v3.2 在全局类 (Helloworld.h) 中声明标签

how to declare label in global class (helloworld.h) with cocos2d-x v3.2?

本文关键字:Helloworld 标签 声明 Cocos2D-X 何使用 v3 全局      更新时间:2023-10-16

我正在使用cocos2d-x v3.2(c ++(创建一个2d平台游戏,并且正在使用标签。

cocos2d-x v3.0 (c++(声明像cocos2d::LabelTTF* currentScore;

Cocos2D-X v2.2.2 (C++(像cocos2d::CCLabelTTF* currentScore;一样宣布

cocos2d-x v3.2(c++(如何在全局类中声明标签(helloworld.h(我试过像

你好世界

class HelloWorld : public cocos2d::LayerColor
{
public:
  virtual bool init();
cocos2d::LabelTTF* currentScore;   //semantic issue(LabelTTF deprecared)
};
    #endif

你好世界.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));
}

再试一次

你好世界.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

它可以工作,但不能"自动当前分数;"在全局类(HelloWorld.h(中声明

在 .h 文件中

Label *autolabel4;

在.cpp

autolabel4 = Label::create((; 无法改善标签大小和源码

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);