CCLabelTTF 應用在數字顯示

CCLabelTTF 在 HelloWorld 中應用在標題字體的顯示,當然想用在變化數字上也是相當方便,只需要做一些數值切換而已,我們在 GameLogoScene 中,首先在
GameLogoScene.h

   1:      int score;
   2:      
   3:      CCLabelTTF* ttf1;
   4:      void ttf1Load();
   5:      void ttf1Act();

第 1 行為分數,用來紀錄得分
第 3 行為 CCLabelTTF 指標 ,ttf1用來顯示分數
第 4 行宣告以及定義 ttf1 用
第 5 行用來位元及數值的變換

接著來看看.cpp的內容
GameLogoScene.cpp

   1:  void GameLogoScene::ttf1Load()
   2:  {
   3:      ttf1 = CCLabelTTF::labelWithString("", "Thonburi", 20);
   4:   
   5:      ttf1->setColor(ccc3(0,0,255));
   6:   
   7:      ttf1->setPosition(ccp(100,300));
   8:   
   9:      this->addChild(ttf1);
  10:  }
  11:   
  12:  void GameLogoScene::ttf1Act()
  13:  {
  14:      char tempc[20];
  15:   
  16:      std::string str1 = "";
  17:   
  18:      sprintf(tempc,"%d",score);
  19:   
  20:      str1 = tempc;
  21:   
  22:      ttf1->setString(str1.c_str());
  23:  }

第 3 行定義 ttf1 的內容,字型,以及字體大小
第 5 行設定顏色
第 7 行設定座標
第 9 行加入layer

第 14 行建立1個字元陣列轉換數值用
第 16 行建立 string 也是轉換用
第 18 行利用 sprintf方法把 score 轉換成字元陣列
第 20 行再把字元陣列內容給予 string
第 22 行設定 ttf1 的內容

最後我們一樣把 ttf1Load() 放在建構子中(16行)

   1:  GameLogoScene::GameLogoScene(void)
   2:  {
   3:      v1Load();
   4:   
   5:      //初始化各項數值
   6:      this->initWithColor(ccc4(255,255,255,255));
   7:      sleep = false;
   8:      gameFlow = 1;
   9:      GF1Time = 0;
  10:      logoSprActTime =0;
  11:      score =0;
  12:   
  13:   
  14:      logoSprLoad();
  15:   
  16:      ttf1Load();
  17:   
  18:      //加入schedule
  19:      this->schedule( schedule_selector(GameLogoScene::Run)); //加入schedule
  20:      
  21:  }

把 ttf1Act() 放到 Run() 迴圈中(10行)

   1:  void GameLogoScene::Run(float dt)
   2:  {
   3:   
   4:      if(gameFlow==GAMEFLOW1)
   5:      {
   6:          GF1Time++;
   7:          logoSprAct();
   8:          v1Add();
   9:   
  10:          ttf1Act();
  11:      }
  12:      
  13:  }

修改 ccTouchesEnded(),如果觸碰到了開關,分數 score 就會加 10 (25行)

   1:  void GameLogoScene::ccTouchesEnded(CCSet* touches, CCEvent* event)
   2:  {
   3:   
   4:      CCTouch* touch = (CCTouch*)( touches->anyObject() );
   5:   
   6:      CCPoint location = touch->locationInView();
   7:   
   8:      location = CCDirector::sharedDirector()->convertToGL(location);
   9:   
  10:      CCLog("convertToGLXY  x:%f, y:%f", location.x, location.y);
  11:      
  12:      std::vector<CCSprite*>::iterator iter;
  13:   
  14:      for(iter = v1->begin() ; iter != v1->end() ;)
  15:      {
  16:   
  17:          CCSprite *temp = *iter;
  18:              
  19:          if(location.x>temp->getPositionX()-temp->getContentSize().width/2 && location.x<temp->getPositionX()-temp->getContentSize().width/2+temp->getContentSize().width
  20:              && location.y>temp->getPositionY()-temp->getContentSize().height/2 && location.y<temp->getPositionY()-temp->getContentSize().height/2+temp->getContentSize().height)
  21:          {
  22:                  
  23:              iter = v1->erase(iter);
  24:                  
  25:              score+=10;
  26:          }
  27:          else
  28:          {
  29:              iter++;
  30:          }
  31:      }
  32:  }

最後在 visit() 方法中繪出 ttf1 (第20行)

   1:  void GameLogoScene::visit()
   2:  {
   3:      if(gameFlow==GAMEFLOW1)
   4:      {    
   5:          this->draw();
   6:   
   7:          logoSpr->visit();
   8:          
   9:          
  10:          std::vector<CCSprite*>::iterator iter;
  11:          for(iter = v1->begin() ; iter != v1->end() ; ++iter)
  12:          {
  13:   
  14:              CCSprite* temp = *iter;
  15:   
  16:              temp->visit();
  17:   
  18:          }
  19:          
  20:          ttf1->visit();
  21:   
  22:      }
  23:      
  24:  }
變化數字的功能已經完成,我們來加點小質感,在GameLogoScene.h新增一個tempscore變數(第2行)




   1:      int score;
   2:      int tempscore;
   3:   
   4:      CCLabelTTF* ttf1;
   5:      void ttf1Load();
   6:      void ttf1Act();

在ttf1Act()中加入計算(4~7行)





   1:  void GameLogoScene::ttf1Act()
   2:  {
   3:   
   4:      if(tempscore<score)
   5:      {
   6:          tempscore++;
   7:      }
   8:   
   9:   
  10:      char tempc[20];
  11:   
  12:      std::string str1 = "";
  13:   
  14:      sprintf(tempc,"%d",tempscore);
  15:   
  16:      str1 = tempc;
  17:   
  18:      ttf1->setString(str1.c_str());
  19:  }

這樣數字就會呈現不斷增加的情況,而不是直接變化