将 CCSprite 缩放到精确大小缩放、精确、大小、CCSprite

2023-09-06 09:26:08 作者:墨閼

将精灵缩放到精确大小的最佳技术是什么.scale 属性是一个乘数,但是如果你想让一个精灵正好是 X 像素宽,有没有简单的技术?或者,它是否需要简单地使用所需的大小和精灵的实际 contentsize 来计算必要的 scale 操作?

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?

推荐答案

我相信这是可行的:

-(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];