如何使用cocos2d-x v3.2在全局类(helloworld.h)中声明标签?

时间:2014-12-20 06:18:49

标签: c++ ios cocos2d-x cocos2d-x-3.0

我正在使用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) 我试过像

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

再试一次

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

它有效但不能自动当前分数;"在全局类(HelloWorld.h)中声明

1 个答案:

答案 0 :(得分:3)

在.h文件中

Label *autolabel4;

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