我花了这么多时间来调试它,但我找不到任何解决方案, 以下代码总是在我的galaxy S2上崩溃(在android 4.1.2上运行), 我使用cocos2d-x 2.1.5进行开发,我避免在onHttpRequestCompleted()函数中使用autorelease,release,retain ..但它仍无法在我的设备上运行。
请帮助我! 这是我的代码:#include "WuXiaUpdateScene.h"
#include "base64.h";
#include "FileOperation.h";
#include <algorithm>
#include "WuXiaLoginScene.h";
#include "WuXiaProgressBar.h"
#include "WuXiaServer.h"
#include "WuXiaMsgBox.h"
#include "StringHelper.h"
USING_NS_CC;
CCScene* WuXiaUpdateScene::scene()
{
// 'layer' is an autorelease object
WuXiaUpdateScene *layer = WuXiaUpdateScene::create();
// return the scene
return (CCScene*)layer;
}
// on "init" you need to initialize your instance
bool WuXiaUpdateScene::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init())
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
this->setFlag(UPDATE);
/////////////////////////////
// 3. add your codes below...
fileCount = 0;
fileUpdated = 0;
CCLabelTTF * checkupdatesTTF = CCLabelTTF::create("Checking updates","Arial",22);
checkupdatesTTF->setTag(1);
checkupdatesTTF->setPosition(CCPoint(visibleSize.width/2,visibleSize.height/2));
this->addChild(checkupdatesTTF);
// 建立 progress bar
WuXiaProgressBar * progressBar = WuXiaProgressBar::create();
progressBar->setTag(2);
progressBar->setPercentage(0);
status = Updating;
connect();
return true;
}
void WuXiaUpdateScene::onEnter()
{
CCLayer::onEnter();
this->schedule(schedule_selector(WuXiaUpdateScene::onUpdateCompleted),0.5);
}
void WuXiaUpdateScene::onHttpRequestCompleted(cocos2d::extension::CCHttpClient* client, cocos2d::extension::CCHttpResponse* response)
{
std::vector<char> *buffer = response->getResponseData();
char* cString = new char[buffer->size()];
for (unsigned int i=0;i<buffer->size();i++)
{
sprintf(cString+i,"%c",(*buffer)[i]);
}
std::string sString = cString;
if (response->isSucceed() && sString.find("SUCCESS") != string::npos && sString.find("#SUCCESS") != string::npos)
{
if (sString.find("NO_UPDATES") != string::npos)
{
CCLabelTTF * checkupdatesTTF = (CCLabelTTF*)this->getChildByTag(1);
if (checkupdatesTTF)
{
checkupdatesTTF->setString("No updates");
status = Updated;
}
return;
}
vector<string> fileParts = StringHelper::Split(cString,"#");
std::string writablePath = cocos2d::CCFileUtils::sharedFileUtils()->getWritablePath();
std::vector<std::string> vPaths = std::vector<std::string>();
vPaths.push_back(writablePath);
cocos2d::CCFileUtils::sharedFileUtils()->setSearchPaths(vPaths);
vector<string> fileList = StringHelper::Split(fileParts.at(3).c_str(),",");
CCLabelTTF * checkupdatesTTF = (CCLabelTTF*)this->getChildByTag(1);
char* cFileCount = new char[5];
sprintf(cFileCount,"0/%d",fileList.size());
string count(cFileCount, cFileCount+strlen(cFileCount));
string wstr;
wstr.append("There're ");
wstr.append(count);
wstr.append(" files need to be updated.");
checkupdatesTTF->setString(wstr.c_str());
version = fileParts.at(1);
fileCount = fileList.size();
for (int i=0;i<fileList.size();i++)
{
cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest();
std::string version="0";
char* url= new char[1024];
sprintf(url,"%s",fileList.at(i).c_str());
request->setUrl(url);
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(WuXiaUpdateScene::onFileDownloaded));
cocos2d::extension::CCHttpClient::getInstance()->send(request);
request->release();
delete url;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
WuXiaProgressBar * progressBar = (WuXiaProgressBar*)this->getChildByTag(2);
progressBar->setPosition(CCPoint(visibleSize.width/2,visibleSize.height/2-50));
this->addChild(progressBar);
}
}
void WuXiaUpdateScene::onFileDownloaded(cocos2d::extension::CCHttpClient* client, cocos2d::extension::CCHttpResponse* response)
{
WuXiaProgressBar * progressBar = (WuXiaProgressBar *)this->getChildByTag(2);
std::vector<char> *buffer = response->getResponseData();
cocos2d::extension::CCHttpRequest *hr = response->getHttpRequest();
const char * url = hr->getUrl();
int len = strlen("7a30a48c-bd38-46b2-bb38-4afb79ac89abi.png");
char * fileName = new char[len+1];
memcpy( fileName, &url[strlen(url)-len], len );
fileName[41] = '\0';
FileOperation::saveFile(fileName,buffer);
progressBar->setPercentage((++fileUpdated)/fileCount*100);
if (fileUpdated == fileCount)
{
// 取得版本
char* versionFile = "version.cfg";
// 儲存版本資訊
FileOperation::saveFile(versionFile,(char*)version.c_str());
CCLabelTTF * checkupdatesTTF = (CCLabelTTF*)this->getChildByTag(1);
checkupdatesTTF->setString("Update finished.");
status = Updated;
return;
}
else
{
CCLabelTTF * checkupdatesTTF = (CCLabelTTF*)this->getChildByTag(1);
char* cFileCount = new char[5];
sprintf(cFileCount,"%.0f/%.0f",fileUpdated,fileCount);
string count(cFileCount, cFileCount+strlen(cFileCount));
string wstr;
wstr.append("there're ");
wstr.append(count);
wstr.append(" files need to be updated.");
checkupdatesTTF->setString(wstr.c_str());
}
}
// connect to server and get file list to update
void WuXiaUpdateScene::connect()
{
cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest();
std::string version="0";
char* url= new char[1024];
std::string versionFile = "version.cfg";
if (FileOperation::isFileAvaiable(versionFile))
sprintf(url,"%sgetUpdateFiles.aspx?text=%s",WuXiaServer::PrimaryServer,base64_encode(FileOperation::readFile(versionFile)).c_str());
else
sprintf(url,"%sgetUpdateFiles.aspx?text=%s",WuXiaServer::PrimaryServer,base64_encode("0").c_str());
request->setUrl(url);
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(WuXiaUpdateScene::onHttpRequestCompleted));
cocos2d::extension::CCHttpClient::getInstance()->send(request);
request->release();
delete url;
}
// switch screen whem update completed
void WuXiaUpdateScene::onUpdateCompleted(float dtTime)
{
if (status != Updated)
return;
this->unschedule(schedule_selector(WuXiaUpdateScene::onUpdateCompleted));
CCScene * scene = WuXiaLoginScene::scene();
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1,scene));
}@
答案 0 :(得分:0)
您是否检查过从Android加载时加载的所有资源? Windows平台和Android平台可能具有不同的分辨率,如果您已根据分辨率加载资源,则检查是否所有资源都可用。
答案 1 :(得分:0)
感谢您回复我。
我已经解决了这个问题,这个问题可能是由Visual Studio IDE造成的,我删除了一些评论,现在它可以正常工作。我不知道为什么当我只是将新的.cpp和.h文件添加到我的项目中时,可能会再次出现同样的问题,但要删除或添加一些代码到某些页面,它会起作用,非常奇怪。
答案 2 :(得分:0)
我纠正了我的回答,这是一个记忆问题。 char * cString = new char [buffer-&gt; size()];无法为“缓冲区”存储足够的字符, char * cString = new char [buffer-&gt; size()+ 1];是正确的, 我没有考虑缓冲区末尾的空字符。