We need a 'AutoFit' funtion

We need a 'AutoFit' funtion
0.0 0


Is there any function we can use to do the ‘auto-fit’?Some sprites may stretch to a relative size.‘Scale’ is not a good way.


You could do it like this:-

void ScaleMySprite( float wantedWidth, float wantedHeight )
  CCRect rect = mySprite->boundingBox();

  float xScale = rect.width / wantedWidth;
  sprite->setScaleX( xScale );

  float yScale = rect.height / wantedHeight;
  sprite->setScaleY( yScale );

Haven’t tested it but it should work.


Here’s my utility function I use to do this, basically like Gavs but takes a sprite as an argument

void ScaleSpriteToSize( CCSprite* pSprite, float width, float height )
    CCSize textureSize = pSprite->getTexture()->getContentSize();
    float scaleX = width / textureSize.width;
    float scaleY = height / textureSize.height;

    pSprite->setScaleX( scaleX );
    pSprite->setScaleY( scaleY );