Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top