我的iOS项目中有一段代码通过setTexture交换CCSprite的纹理,如下所示:
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"Circle.png"]];
Run Code Online (Sandbox Code Playgroud)
但是,交换前后CCSprite纹理的尺寸不同,因此,Circle.png纹理会被裁剪掉; 卡在原始纹理的大小(因为圆圈较大).
交换纹理后如何调整大小?
试试这个:
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:@"Circle.png"];
if (texture)
{
// Get the size of the new texture:
CGSize size = [texture contentSize];
[sprite setTexture:texture];
// use the size of the new texture:
[sprite setTextureRect:CGRectMake(0.0f, 0.0f, size.width,size.height)];
}
Run Code Online (Sandbox Code Playgroud)