How to create CCSprite for setting borders?

How to create a CCSprite that scales the image according to input boundaries, i.e. if I want CCSprite to be width = 70 and height = 50 and scale the image in the file to 70.50. Is there an easy way to do this differently than to calculate the scale factor from the image size compared to the desired final size?

+4
source share
2 answers

Here is an implementation of a category that works, based on a response 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 
+9
source

Not sure if there is an easier way, but I would 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; 

Put this in a category on CCSprite and you won’t have to worry about it anymore.

+3
source

All Articles