문제

I'm currently trying to use a stretchable image on a SKSpriteNode.

Typically, strechable images are returned by the method UIImage::resizableImageWithCapInsets:

So i wrote that code:

SKTexture * texture = [SKTexture textureWithImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)]];
SKSpriteNode * node = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(150, 8)];

But it don't work. The node streaches itself without taking Edge Insets into account. Is it only possible?

도움이 되었습니까?

해결책

Use the centerRect property of SKSpriteNode. Assuming your texture is 50 pixels wide and you want not to stretch the leftmost and rightmost 10 pixels:

SKTexture *texture = [SKTexture textureWithImage:image];
SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(150, 8)];
node.centerRect = CGRectMake(10 / 50.0, 0, 30 / 50.0, 1);

centerRect is specified in unit coordinates of the texture. More info here under "Resizing a Sprite."

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top