Hi there,
I have an app/game where I am loading some images from a remote site and want to scale them to a fixed size.
The challenge I have is the calls to create a sprite or add an image to the texture cache return immediately (as they should) with undefined heights and widths. A subsequent call to setScale cannot use the content size to help make decisions on scaling the original image to the fixed size.
i.e.
@
img = cc.TextureCache.getInstance().addImage(“some web address of an image”);
image = cc.Sprite.createWithTexture(img);
image.setScaleX(size/image.getContentSize().width);
image.setScaleY(size/image.getContentSize().height);@
My current solution for now (so I can work on other things) is simply to crop when I don’t know the image size but it is of course not ideal. Once it is in the texture cache all subsequent usage can be scaled properly.
img = cc.TextureCache.getInstance().addImage("some web address of an image"); if (img.height == 0) { image = cc.Sprite.createWithTexture(img, cc.rect(0,0,size,size)); } else { image = cc.Sprite.createWithTexture(img); image.setScaleX(size/image.getContentSize().width); image.setScaleY(size/image.getContentSize().height); }
Does cocos2d have a scale to size type function? This would alleviate the need to calculate the scale and will resolve itself later when the image draws.
The alternative is to detect when the image has loaded and run an action at that time. This would mean overriding the “load” listener on the canvas Image() I believe. Is there some built in functionality for this in cocos2d-html5 or will I need to subclass and possibly build a class that basically does the same thing as the standard image loaders but calls an action (if defined) after loading. This sounds like something that others might eventually need as well.
Alan