文件管理 · 2022年7月26日

cocos2dx文件日志|cocos2dx怎么遍历资源文件夹里的文件

① 在cocos2dx中怎么使用sqlite来纪录数据

1)下载sqlite源代码,并解压到工程中[这步不需要了,cocos2dx中已经加入了sqlite,只需引用头文件sqlite3.h就可以了]:减压到工程中(shell.c不需要),在xcode中如下:2) 打开数据库:先用sqlite工具创建一个数据库,叫test.db;(工具可以使用sqlite提供的命令行工具;在sqlite下载页面中的Precompiled Binaries );把test.db拷贝到Resource文件夹下,并加入工程,如下图:然后调用如下代码,打开数据库:[cpp] view plainstd::string filename = CCFileUtils::sharedFileUtils()->fullPathForFilename("test.db");result = sqlite3_open(filename.c_str(), &pDB);CCLog(filename.c_str());if( result != SQLITE_OK )CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg );elseCCLog("成功地打开了数据库");3)创建表和插入数据:[html] view plain//创建表,设置ID为主键,且自动增加result=sqlite3_exec( pDB, "create table MyTable_1( ID integer primary key autoincrement, name nvarchar(32) ) " , NULL, NULL, &errMsg );if( result != SQLITE_OK )CCLog( "创建表失败,错误码:%d ,错误原因:%s\n" , result, errMsg );//插入数据sqlstr=" insert into MyTable_1( name ) values ( '克塞' ) ";result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );if(result != SQLITE_OK )CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );//插入数据sqlstr=" insert into MyTable_1( name ) values ( '葫芦娃' ) ";result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );if(result != SQLITE_OK )CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );//插入数据sqlstr=" insert into MyTable_1( name ) values ( '擎天柱' ) ";result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );if(result != SQLITE_OK )CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );4)查询对数据库进行查询,有两种方式:1)sqlite3_prepare_v2 + sqlite3_step + sqlite3_column_text + sqlite3_finalize; 2) sqlite3_exec;

② cocos2dx怎么读取配置文件

在cocos2d-x中可以用.plist格式的文件来保存数据,它是XML文件格式的一种,在cocos2d-x解析.plist方面相关的资料比较少,但本身也很简单,要解析.plist文件可以参考cocos2d-x类库中的CCSpriteFrameCache类和CCParticleSystem类,它主要是使用CCDictionary类来对.plist文件进行操作。下面有一个.plist文件:[html] view plain<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>level1</key> <dict> <key>bg_far_scene</key> <dict> <key>path</key> <string>images/far_scene.png</string> <key>pos</key> <string>{358, 309}</string> </dict> <key>bg_near_scene</key> <dict> <key>path</key> <string>images/near_scene.png</string> <key>pos</key> <string>{360, 100}</string> </dict> </dict> </dict> </plist> 读取.plist文件的代码如下:[cpp] view plainconst char* testPlistPath = "BSPlistDatas\\test.plist"; const char* fullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("test.plist", testPlistPath); CCDictionary* plistDic = CCDictionary::createWithContentsOfFile(testPlistPath); CCDictionary* levelDic = dynamic_cast<CCDictionary*>(plistDic->objectForKey("level1")); CCDictionary* farScene = dynamic_cast<CCDictionary*>(levelDic->objectForKey("bg_far_scene")); CCString* farScenePath = dynamic_cast<CCString*>(farScene->objectForKey("path")); CCPoint point = CCPointFromString(farScene->valueForKey("pos")->getCString()); CCLog("path = %s", farScenePath->getCString()); CCLog("pos = %f, %f", point.x, point.y); 第一行是.plist文件的相对路径,通过CCFileUtils类获得文件中绝对路径后,使用CCDictionary::createWithContensOfFile(filePath);将文件中内容加载到CCDictionary数据结构的内存中,然后通过xxxForKey获得相应的key下的value。这里需要注意的是,当在读取'pos'的时候,它的值一个{x, y}的字符串,这是.plist文件中的数组存储规则,我们可以通过cocos2d-x提供函数api将这样的字符串转化为CCpoint对象。[cpp] view plainCCPoint point = CCPointFromString(farScene->valueForKey("pos")->getCString()); 上面这句话就是做了这样的一个转化的过程,同样的cocos2d-x还支持CCSize、CCRect的字符串的转化。他们转化的方法以及在.plist中对应的字符串格式如下:CCPoint: CCPointFromString(); {x, y}CCSize: CCSizeFromString(); {w, h}CCRect: CCSizeFromString(); {x, y, w, h}这样我们2D游戏所初始化所需要的数据都基本上够用了,可以尝试将游戏的初始数据放在.plist中,然后修改调整数值就可以直接修改plist文件,而无需重新编译程序了,从而实现游戏数据和游戏逻辑的分离。

③ cocos2dx lua 怎么在Android工程中输出日志到logcat

华为系列手机默认是关闭了日志输出的,需要人工打开。方法如下:进入拨号界面输入:*#*#2846579#*#*依次选择ProjectMenu—后台设置—-LOG设置—LOG开关,勾选"CP日志“即可。

④ cocos2dx 怎么定义debug模式的宏

通常的原则:Debug相关的信息,仅对Debug方式有效,对Release无任何影响,包括性能#if _MSC_VER < 1400 // 版本较低时,不支持…类型的宏,屏蔽代码LogPrintf (vc60, vc70) #define LogPrintf //#else // 1400之后的版本支持…类型的宏,此时来区分_Debug和_Release模式 #ifdef _DEBUG #define LogPrintf(format, …)\ {\ printf(format, __VA_ARGS__);\ } #else #define LogPrintf(format, …) #endif#endif再进一步,我们希望打印 行号、文件名、函数名:此时,借用系统中的宏可以实现__FUNCTIONW__ 函数名(Unicode)__FUNCTION__: 函数名(ANSI)__FILEW__ 文件名(Unicode)__FILE__ : 文件名(ANSI)__LINE__ : 行号例如,在Debug日志中打印行号和文件名,方便定位,我们可以再定位一种Debug的Printf: LogPrintfWithFileLine#define LogPrintfWithFileLine(format, …)\ {\ printf(format, __VA_ARGS__);\ printf(_T(" at %s<line:%d>\n"), __FILEW__ , __LINE__);\ }

⑤ 使用cocos2dx移植 android平台,怎么收集崩溃日志

很高兴能帮助您如果你用的是windows系统作为开发环境,使用的是cocos2d-x3.2最新版的,你在一开始安装cocos2d-x的时候,会要求输入NDK_ROOTANDROID_SDK_ROOTANT_ROOT最新版的cocos2d-x使用了python来进行安装和编译程序的,所以你要先在你的windows系统上安装上python.安装好之后,下载最新版的cocos2d-x,然后,cd到这个目录下,你会看到一个有一个setup.py的文件,.py后缀的文件就是python的脚本文件了。是要用python来运行的。如果你安装好python之后设置好了python的环境变量,这个时候,你只要在命令行里运行:python setup.py就可以了。然后你就可以看到要求你输入的这三个路径了。这三个工具的所在路径,这几个路径输入对了之后,你的cocos2d-x就安装好了,这个时候,在cmd里创建一个新项目,创建的方法是在命令行里输入:cocos new youprojectname -p com.youcom,youprojectname -l cpp -d 你的新建项目所在路径然后回车,你就可以看你的的项目会在你的新建项目路径下有一个youprojectname的文件夹,打开,你就可以看到里边有各个系统的project文件夹了,还有一个Class和Resources的文件夹。这个时候,你在命令行里cd到你的proj.android文件夹下,使用命令:python bulid_native.py就可以对你的安卓新建的helloworld项目进行编译了。不出意外,要不然一分钟就编译好了。会生成一个libcocos2dcpp.so在你的pro.android/libs/armabi的路径下。然后打开你的ADT,也就是eclipse,进入pro.android目录,导入android项目到eclipse。这个时候可能会报一个cocos2d-x的lib库错误,只要把cocos2d-x的cocos2d/cocos/platform/android/java/src 目录下的文件夹复制到pro.android/src下就可以解决这个问题了。然后,接上你的android手机,并且在手机中打开开发者模式。点击程序远行设置,选择使用设备运行。在弹出的一个选择手机设备的框时选择run,不一会,你的helloworld项目就可以在手机上看到了。如果要调试,你可以在eclipse中打开logcat,看到cocos2d-x的log输出信息。总的来说,android最好的开发环境还是mac系统。因为在mac系统下很多linux命令都可以使用。不像windows系统比较蹩脚。你的采纳是我前进的动力,记得好评和采纳,答题不易,互相帮助

⑥ android cocos2dx 游戏统计支持崩溃日志收集吗

你在具体的那篇日志里面直接单独加个统计代码。我没用过51la我用的是statcounter这个统计,可以直接Pageload Activity里面看见每个页面有多少人访问,访问时间IP等等

⑦ cocos2dx 安卓crash怎么导出日志

首先目前来看unity3d 因为是3d引擎,目前对2d支持并不完善,unity3d 目前做2d普遍两种思路,一种是正交相机,3d画面2d视角,另一种是通过一些插件,动态创建mesh来绘制图形单元目前用的较多的是2d toolkit,ex2d,smooth moves,sm2,最近uni2d 刚刚更新,支持骨骼动画,像素级碰撞,是个很赞的2d 插件,如果时间上你不急的话,unity3d 将于秋季发布原生的2d工具,看过视频,效果不错,开发效率的话 因为全部都是可视化编辑,而且一键跨平台,所以相对cocos2d-x 效率稍微高一点,人员招聘的话有点难度,大部分有经验的在目前这个时间点准备换工作的应该不多,有意愿求职的大部分都没有经验,对于以后发展来说,因为跨平台的优势越来越明显,而且官方的更新也是比较快的,功能方面不多增加。最主要是2d出了以后,2d3d都支持,长远来看,比cocos2d-x 前景明朗再看cocos2d-x ,首先开发难度,对于熟练C++的来说难度不大,本身就是2d引擎,相对来说目前方案比较成熟,开发效率如果有经验的话,也是很快的,人员招聘的话,相对unity3d 的好找一点,毕竟C++程序很多,对于以后发展,目前来看,cocos2d-x 基本已到瓶颈,即使有更新,变化不会太大

⑧ cocos2dx怎么遍历资源文件夹里的文件

第一步在:在ZipFile添加方法,因为_dataThread是个私有的,尽量不改变源码的情况下,添加一个get方法是最好的这个getAllFile可以返回所有的文件目录std::vector<std::string>ZipFile::getAllFile(){ std::vector<std::string> vec; ZipFilePrivate::FileListContainer::iterator it1; for(it1=_dataThread->fileList.begin();it1!=_dataThread->fileList.end();++it1) { vec.push_back(it1->first.c_str()); } return vec;}第二步:使用getAllFile的返回值做遍历,这里直接帖出iOS和Android的同时遍历吧,同时搜索png和jpg的图片,可以用于加载资源,记得导入头文件#include "support/zip_support/ZipUtils.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)#include <dirent.h>#include <sys/stat.h>#else#include "platform/CCCommon.h"#include "support/zip_support/unzip.h"#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"#endifvoid ResourceLoadingLayer::getAllFile(std::string folderPath,int depth, std::vector<std::string> *list, std::string head){#if CC_PLATFORM_IOS == CC_TARGET_PLATFORM DIR *dp; structdirent *entry; structstat statbuf; if((dp =opendir(folderPath.c_str())) ==NULL) { fprintf(stderr,"cannot open directory: %s\n", folderPath.c_str()); return; } chdir(folderPath.c_str()); while((entry =readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) ==0) continue; getAllFile(entry->d_name,depth+4,list,head+entry->d_name); } else { if (head.length() ==0) { string name = entry->d_name; if (name.length()>3 && name.rfind(".") > 0 && (name.substr(name.rfind(".")+1,3) == "jpg" || name.substr(name.rfind(".")+1,3) == "png")) { list->push_back(entry->d_name); } } else { string filename = head+"/" +entry->d_name; if (filename.length()>3 && filename.rfind(".") > 0 && (filename.substr(filename.rfind(".")+1,3) == "jpg" || filename.substr(filename.rfind(".")+1,3) == "png")) { list->push_back(filename); } } } } chdir(".."); closedir(dp);#else ZipFile* pFile = new ZipFile(getApkPath(),"assets/"); vector<string> vec = pFile->getAllFile(); for (int i=0; i<vec.size(); i++) { string file = vec.at(i); if (file.compare("assets/")) { file = file.substr(7,file.length()); }if(file.substr(0,folderPath.length()) == folderPath ){ string filename = file.substr(file.rfind("/")+1,file.length()); if (filename.length()>3 && filename.rfind(".") >0 && (filename.substr(filename.rfind(".")+1,3) =="jpg" || filename.substr(filename.rfind(".")+1,3) =="png") { list->push_back(filename); } } }#endif}第三步,使用:首先得拿到资源文件夹的完整路径,方法有很多,只举其一:在HelloWorld的工程下的资源根目录有一张HelloWorld.png图片,我们可以直接获取它在程序中的完整路径,即string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename("HelloWorld.png");所以 string resPath = fullpath.substr(0,fullpath.rfind("/")+1);得到了完整的资源路径后,vector<string> vec;getAllFile(resPath, 0, &vec, "");这样就大功告成了,vec会保存所有的png或jpg的资源,会带head名的哦,比如说bg/welcome.jpg上面是遍历所有的文件夹,如果单独遍历资源文件下的某个目录,即是getAllFile(resPath+"xxx",0,&vec,""); xxx 为资源根目录下的子目录,这样就可以分别加载某个目录了

⑨ android和cocos2dx可以互调吗

在cocos2dx里访问/互调android里的activity方法/变量 不得不感叹下COCOS2DX的强大。。同一份游戏代码不加改动的可以放到android和iphone上面。 而最近在往ANDROID上放的时候,需要集成其它非官方运营渠道的API,而对方提供的API是jar包。所以需要在cocos2dx C++源文件中调用JAVA的API,因此有了以下DEMO代码: activity里有如下定义: Java代码 public void cppCall_nonStatic_logsth(){ //非静态方法 Log.i("cppCall_nonStatic", "test2~~~~!!!"); } public static Object cppCall_logsth(){ //静态方法 Log.i("cppCall", "test~~~~!!!"); return activity; } AppDelegate.cpp里: 加上预定义(for android) C++代码 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include <jni.h> #include <JniHelper.h> #include <android/log.h> #if 1 #define LOG_TAG "JniHelper" #define LOGD(…) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #else #define LOGD(…) #endif #endif 关键代码片段: C++代码 JniMethodInfo minfo; jobject jobj; bool b = JniHelper::getStaticMethodInfo(minfo, "com/loy/puzzles/Puzzles", //类路径 "cppCall_logsth", //静态方法名 "()Ljava/lang/Object;"); //括号里的是参数,后面的是返回值。 if (!b) { LOGD("JniHelper::getStaticMethodInfo error…"); }else{ jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); } JniHelper::getMethodInfo(minfo, "com/loy/puzzles/Puzzles", "cppCall_nonStatic_logsth", "()V"); if(!b){ LOGD("JniHelper::getMethodInfo error…"); }else{ LOGD("ready to invoke method…"); minfo.env->CallVoidMethod(jobj, minfo.methodID); } 最终LOGCAT打印日志如下: 12-29 12:46:33.891: D/JniHelper(2008): testing~~~ 12-29 12:46:33.891: I/cppCall(2008): test~~~~!!! 12-29 12:46:33.891: D/JniHelper(2008): ready to invoke method… 12-29 12:46:33.891: I/cppCall_nonStatic(2008): test2~~~~!!! 12-29 12:46:33.891: D/JniHelper(2008): testing over~~~ 即表示调用成功。

⑩ 【急】cocos2dx为啥无法打开包括文件:“curl/curl.h”!!!

因为你的新工程里没有把对应的include和lib添加进去。你可以参考一下tests的proporties的设置,主要是C/C++->general的Addtionnal Include Directories和Linker->Input的Addtional Libirary xxxx和你新建的工程的对应项,把curl所在的文件夹添加进去还有curl的.lib文件添加即可。