facebook twitter hatena line email

「Cocos2dx/Log」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ログをformに入れる)
(ログをformに入れる)
行31: 行31:
  
 
==ログをformに入れる==
 
==ログをformに入れる==
 +
Classes/HelloWorldScene.h
 +
std::string loglist[10];
 +
 +
Classes/HelloWorldScene.cpp
 
  logLabel = Label::createWithTTF("", "fonts/arial.ttf", 6);
 
  logLabel = Label::createWithTTF("", "fonts/arial.ttf", 6);
 
  logLabel->setPosition(Vec2(origin.x + visibleSize.width/2,
 
  logLabel->setPosition(Vec2(origin.x + visibleSize.width/2,
行37: 行41:
 
  this->addChild(logLabel, 1);
 
  this->addChild(logLabel, 1);
 
  void HelloWorld::addLog(const std::string str) {
 
  void HelloWorld::addLog(const std::string str) {
    log("%s", str.c_str());
+
    log("%s", str.c_str());
    logLabel->setString(str + "\n" + logLabel->getString());
+
    int cnt = 9;
    auto visibleSize = Director::getInstance()->getVisibleSize();
+
    for (int i = cnt - 2; i >= 0; i--) {
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
+
        loglist[i + 1] = loglist[i];
    logLabel->setPosition(Vec2(origin.x + visibleSize.width / 2,
+
    }
                            origin.y + visibleSize.height / 2 - logLabel->getContentSize().height / 2));
+
    loglist[0] = str;
 +
    std::string logStr = "";
 +
    for (int i = cnt - 1; i >= 0; i--) {
 +
        logStr = loglist[i] + "\n" + logStr;
 +
    }
 +
    logLabel->setString(logStr);
 
  }
 
  }
  

2018年9月25日 (火) 17:59時点における版

サンプル

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に入れる

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)