加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d js显示远程下载的图片资源

发布时间:2020-12-14 16:46:37 所属栏目:百科 来源:网络整理
导读:1.远程下载图片函数: cc.textureCache.addImage(address,function(texture){ if(texture){ //todo //3 var spTouXiang = new cc.Sprite(texture); spTouXiang.setPosition(x,y); m_choiceLayer.addChild(spTouXiang); } },this); } 2.会发现,报这样类似的




1.远程下载图片函数:

cc.textureCache.addImage(address,function(texture){
if(texture){

//todo

//3

var spTouXiang = new cc.Sprite(texture);
spTouXiang.setPosition(x,y);
m_choiceLayer.addChild(spTouXiang);

}

},this);

}



2.会发现,报这样类似的错误:

textureCache加载远程图片,在WebGL下失败

// 环境
浏览器; chrome
自动使用的WebGL模式,
执行测试用例:TextureCacheTest.js

Uncaught SecurityError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at http://www.cocos2d-x.org/attachments/download/1508may not be loaded.
TexturesWebGL.js:467

强制使用canvas renderer显示正常,不过在console中会报跨域的警告
Image from origin 'http://www.cocos2d-x.org' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8081' is therefore not allowed access.
解决方法: 在 project.json中将 "renderMode" : 0, 改成 "renderMode" : 1,


3.经过上述2部,texture正常情况下已经有值了。


然后在上述 //todo //3的位置写上

var spTouXiang = new cc.Sprite(texture);
spTouXiang.setPosition(x,y);
this.addChild(spTouXiang);

表示增加一个sprite,然后用从远程下载的texture纹理赋值,并显示

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读