Use resizableImageWithCapInsets on SKSpriteNode

1.1k views Asked by At

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?

1

There are 1 answers

0
jonahb On BEST ANSWER

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."