Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale CCSprite to exact size

What is the best technique to scale a sprite to an exact size. The scale property is a multiplier, but if you want a sprite to be exactly X pixels wide, is there a simple technique? Or, would it require simply using the desired size and the sprites actual contentsize to calculate the necessary scale operation?

like image 235
johnbakers Avatar asked Mar 14 '12 03:03

johnbakers


1 Answers

I believe this works:

-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
    sprite.scaleX = width / sprite.contentSize.width;
    sprite.scaleY = height / sprite.contentSize.height;
}

Put it in your game, and use like this:

[self resizeSprite:mySprite toWidth:350 toHeight:400];
like image 146
Voldemort Avatar answered Jan 20 '23 14:01

Voldemort