开发者

How do I create a CCSprite to set bounds?

开发者 https://www.devze.com 2023-01-22 20:22 出处:网络
How can I create a CCSprite that scales the image to fit within input bounds, i.e. if开发者_JS百科 I want a CCSprite that is width = 70 and height = 50 and scales the image in the file to 70,50.Is the

How can I create a CCSprite that scales the image to fit within input bounds, i.e. if开发者_JS百科 I want a CCSprite that is width = 70 and height = 50 and scales the image in the file to 70,50. Is there a simple way to do this other than compute the scale factor from the image's size in comparison to the desired final size?


Here is a category implementation that works, based on the answer from @Martin

@implementation CCSprite(Resize)

-(void)resizeTo:(CGSize) theSize
{
    CGFloat newWidth = theSize.width;
    CGFloat newHeight = theSize.height;


    float startWidth = self.contentSize.width;
    float startHeight = self.contentSize.height;

    float newScaleX = newWidth/startWidth;
    float newScaleY = newHeight/startHeight;

    self.scaleX = newScaleX;
    self.scaleY = newScaleY;

}

@end


Not sure if there's an easier way but I'd just do something like

            CGFloat myDesiredWidth=50;
        CGFloat myDesiredHeight=70;

        CGFloat startWidth=mySprite.size.width;
        CGFloat startHeight=mySprite.size.height;

        CGFloat scaleX=myDesiredWidth/startWidth;
        CGFloat scaleY=myDesiredHeight/startHeight;

        CGFloat finalScale=MIN(scaleX,scaleY);
        mySprite.scale=finalScale;

Drop that into a category on CCSprite and you'll never have to worry about it again

0

精彩评论

暂无评论...
验证码 换一张
取 消