Created
February 23, 2016 07:56
-
-
Save derofim/b118a47af0022e3c9c57 to your computer and use it in GitHub Desktop.
Cocos load image from url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see http://discuss.cocos2d-x.org/t/cocos2d-js-3-6-1-load-image-from-url/21744 | |
downloadCompleted : function(texture) | |
{ | |
var sprite = new cc.Sprite(texture); | |
sprite.x = 200; | |
sprite.y = 200; | |
this.addChild(sprite); | |
}, | |
cc.textureCache.addImageAsync("http://i.imgur.com/tT3sO9M.jpg", this.downloadCompleted, this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see https://github.com/iyou/paohuzi/blob/aa9f09fe01cd29875e612a5d6287ba8d0a4e861f/client/src/engin/util/HttpRequest.js | |
// and http://prog3.com/sbdm/blog/alex8046/article/details/48548355 | |
LoadImgFromUrl: function (target, imgUrl, pos, tag){ | |
if(!imgUrl) return; | |
var Self = target; | |
var loadCb = function(err, IMG){ | |
cc.textureCache.addImage (imgUrl); | |
var texture2d = new cc.Texture2D (); | |
texture2d.initWithElement (IMG); | |
texture2d.handleLoadedTexture (); | |
var sp = new cc.Sprite (); | |
sp.initWithTexture(texture2d); | |
Self.addChild (sp); | |
sp.x = pos.x; | |
sp.y = pos.y; | |
sp.tag = tag; | |
}; | |
cc.loader.loadImg (imgUrl, {isCrossOrigin: true }, loadCb); | |
}, | |
this.LoadImgFromUrl(this, "http://i.imgur.com/tT3sO9M.jpg", cc.p(100, 100), 9999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Server must have Access-Control-Allow-Origin header!
Example server: load image from i.imgur.com