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

来源:互联网 发布:java float 精度 编辑:程序博客网 时间:2024/06/06 07:16




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/1508 may 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纹理赋值,并显示













0 1