cocos2dx 3.x lua http请求网络图片,自己封装的,很好用,需要的可以看看

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.x lua http请求网络图片,自己封装的,很好用,需要的可以看看前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
绑定了一个c++函数到lua,lua这边是这样调用的:

---测试 http请求网络图片代码
local function HttpRequestCompleted(statusCode,tagNum,image)
print("图片数据请求结果 statusCode:"..statusCode.." tag:"..tagNum)
---200表示获取网络图片成功,否则失败
if statusCode==200 then
local texture=cc.Texture2D:new()
texture:initWithImage(image)
local sp_goodsItem=cc.Sprite:createWithTexture(texture) --直接创建请求的网络图片精灵,不用再保存到本地,很方便的
end
end
---最后一个参数是tag值,缺省是-1,这个参数与回调函数HttpRequestCompleted的第2个参数对应
CUserData:ShareUserData():requestGoodsImageFromWeb("http://h.hiphotos.baidu.com/zhidao/pic/item/5bafa40f4bfbfbed0470471b78f0f736afc31fac.jpg",HttpRequestCompleted,123)
c++实现如下:
void CUserData::requestGoodsImageFromWeb(std::string strUrl,int callback,int tag)
{
if (strUrl.length()==0 || callback==0)
{
return;
}
HttpRequest* request = new HttpRequest();
request->setRequestType(HttpRequest::Type::GET);
request->setUrl(strUrl.c_str());
request->setResponseCallback(std::bind(&CUserData::onHttpRequestCompleted_head,this,std::placeholders::_1,std::placeholders::_2,callback,tag));
HttpClient::getInstance()->send(request);
request->release();
}
void CUserData::onHttpRequestCompleted_head(HttpClient *sender,HttpResponse *response,serif; font-size:14px; line-height:24px"> if (response && response->isSucceed())
log("response code: %ld",response->getResponseCode());
// 打印接收到的字符串
std::vector<char> *buffer = response->getResponseData();
std::string bufffff(buffer->begin(),buffer->end());
log("%s",bufffff.c_str());
if (callback!=0) {
LuaStack *stack = LuaEngine::getInstance()->getLuaStack();
stack->pushInt(200);
stack->pushInt(tag);
auto img=new Image;
img->autorelease();
img->initWithImageData((const unsigned char *)buffer->data(),buffer->size());
stack->pushObject(img,"Image");
stack->executeFunctionByHandler(callback,3); //执行lua函数
stack->clean();
}
if (response){
log("error buffer: %s",response->getErrorBuffer());
if (callback!=0) {
LuaStack *stack = LuaEngine::getInstance()->getLuaStack();
stack->pushInt(0);
stack->pushInt(tag);
stack->executeFunctionByHandler(callback,2); //执行lua函数
stack->clean();
绑定到lua的代码如下:
int lua_cocos2dx_custom__CUserData_requestGoodsImageFromWeb(lua_State* tolua_S)
int argc = 0;
cocos2d::CUserData* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
if (!tolua_isusertype(tolua_S,1,"cc.CUserData",&tolua_err)) goto tolua_lerror;
cobj = (cocos2d::CUserData*)tolua_tousertype(tolua_S,0);
if (!cobj)
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_custom__CUserData_requestGoodsImageFromWeb'",nullptr);
return 0;
argc = lua_gettop(tolua_S)-1;
if (argc == 2)
std::string arg0;
int arg1;
ok &= luaval_to_std_string(tolua_S,2,&arg0);
arg1=toluafix_ref_function(tolua_S,3,serif; font-size:14px; line-height:24px"> if(!ok)
return 0;
cobj->requestGoodsImageFromWeb(arg0,arg1);
}else if (argc == 3)
int arg2;
ok &= luaval_to_int32(tolua_S,4,&arg2);
CCLOG("%s has wrong number of arguments: %d,was expecting %d \n","requestGoodsImageFromWeb",argc,2);
return 0;
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_custom__CUserData_requestGoodsImageFromWeb'.",&tolua_err);
}

猜你在找的Cocos2d-x相关文章