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

北战南征 提交于 2020-01-02 15:03:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!