您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx and c++ 的小坑

2015-09-27 07:43 681 查看
char[] 大小

char[x]

当时没注意这个 因为需求中 这里需要格式化的只有1、2、3、4这几种情况都是大小为1的 结果出现如下错误

原因应该是 只能容下 x-1 个字符 必须保留最后一个字符作为结尾判断 否则不知道如何释放



Run-Time Check Failure #2 - Stack around the variable ‘str’ was corrupted.

int[] 赋值



最容易想到的赋值方式却出问题了要用以下的赋值方式

memcpy(mMapView, b.barriers, sizeof(b.barriers));

cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法

网上搜出来的


但是去掉方法这样肯定就不好了 如果你舍不得去掉onEnter方法 我们可以在子类中调用父类的对应方法 比如我的情况在自己实现的onEnter里面调用下Layer::onEnter();

这个其实很容易想到的 至少ios开发中太多这种了

int string 转换 cocos2dx适用

Value(int).asString();// 函数里面调用的是  std::stringstream ret;
char str[255];
sprintf(str, "***%d**", index); 这种 太麻烦了  好累


函数赋值空

std::function<void(void)> //赋值空 应该用 nullptr


动画暂停到某一帧

//action->setCurrentFrame(10 * n);//这个看起来很像但是不行
action->gotoFrameAndPause(10 * n);


boolean bool

不要使用boolean在编译android的时候报错

string

#include <string>  //#include <string.h> 不要使用后者 (我当时的情况就不能用后者 android编译出错)


android解析

来源于互联网
bool AppDelegate::isFileExist(const char* pFileName)

{

if (!pFileName) return false;

std::string filePath = FileUtils::getInstance()->getWritablePath();

filePath += pFileName;

FILE *fp = fopen(filePath.c_str(), "r");

if (fp) {
fclose(fp);
return true;
}
return false;

}

void AppDelegate::copyData(const char* pFileName)
{
if (isFileExist(pFileName)) {
return;
}
std::string strPath = FileUtils::getInstance()->fullPathForFilename(pFileName);
ssize_t len = 0;
unsigned char *data = NULL;

data = FileUtils::getInstance()->getFileData(strPath.c_str(), "r", &len);

std::string destPath = FileUtils::getInstance()->getWritablePath();

destPath += pFileName;

FILE *fp = fopen(destPath.c_str(), "w+");

fwrite(data, sizeof(char), len, fp);

fclose(fp);

delete[]data;

data = NULL;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: