facebook twitter hatena line email

Cocos2dx/Log

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サンプル

log("hoge");
int _int = 1;
log("%i", _int);
const char *str = "hogestr";
log("%s", str);
double _double = 1.0001;
log("%f", _double); // 1.000100;

android studioで確認した場合

02-20 16:59:52.260 18958-18981/com.example.helloworld D/cocos2d-x debug info: hoge

error: cannot convert 'const char*' to 'double' for argument '1' to 'double log(double)'エラーとなる場合

以下を記述するか

USING_NS_CC;

coocs2d::を追加する

cocos2d::log("hoge");

ログカスタム関数

addLog("init");
void HelloWorld::addLog(const std::string str) {
   log("%s", str.c_str());
}

ログカスタム関数に数字などを入れる

int cnt = 10;
const char* chr = "a";
addLog("id=" + label->getString());
addLog("count=" + StringUtils::toString(cnt)); // int
addLog("log=" + std::string(chr); // char

ログをformに入れる(成功)

labelを複数用意する方法

Classes/HelloWorldScene.h

   cocos2d::Label *logLabel[10];
   std::string loglist[10];

Classes/HelloWorldScene.cpp

   for (int i = 0; i < 10; i++) {
       logLabel[i] = Label::createWithTTF("", "fonts/Marker Felt.ttf", 6);
       logLabel[i]->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + (int)(visibleSize.height * (0.5f - i * 0.025f)) - logLabel[i]->getContentSize().height));
       logLabel[i]->setWidth((int)(visibleSize.width * 0.9f));
       this->addChild(logLabel[i]);
   }
void HelloWorld::addLog(const std::string str) {
   log("%s", str.c_str());
   int cnt = 9;
   for (int i = cnt - 2; i >= 0; i--) {
       loglist[i + 1] = loglist[i];
   }
   loglist[0] = str;
   for (int i = cnt; i >= 0; i--) {
       logLabel[i]->setString(loglist[i]);
   }
}

ログをformに入れる(失敗版)

一つのlabelに入れると駄目っぽい。

Classes/HelloWorldScene.h

std::string loglist[10];

Classes/HelloWorldScene.cpp

logLabel = Label::createWithTTF("", "fonts/arial.ttf", 6);
logLabel->setPosition(Vec2(origin.x + visibleSize.width/2,
                           origin.y + visibleSize.height * 0.5f - logLabel->getContentSize().height));
logLabel->setWidth(visibleSize.width * 0.9f);
this->addChild(logLabel, 1);
void HelloWorld::addLog(const std::string str) {
   log("%s", str.c_str());
   int cnt = 9;
   for (int i = cnt - 2; i >= 0; i--) {
       loglist[i + 1] = loglist[i];
   }
   loglist[0] = str;
   std::string logStr = "";
   for (int i = cnt - 1; i >= 0; i--) {
       logStr = loglist[i] + "\n" + logStr;
   }
   logLabel->setString(logStr);
}

時々こういうエラーが出る使えない・・・

09-25 17:57:01.500 974-974/? A/DEBUG: backtrace:
       #00 pc 00017794  /system/lib/libc.so (__memcpy_base+104)
       #01 pc 00692cb4  /data/app/jp.co.example.exampleapp/lib/arm/libMyGame.so (_ZN7cocos2d12TextureAtlas10insertQuadEPNS_16V3F_C4B_T2F_QuadEi+520)

日本語が文字化け

log = Label::createWithTTF("", "fonts/arial.ttf", 10); // これは文字化け
log = Label::createWithSystemFont("", "arial", 10); // これは文字化けしなかった