Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKSpriteNode stretching when manually animating textures

I have a fairly simple animation with 8 identically sized images. I'm not using the built in animation methods as I want to manually control the speed of the animation on the fly. I'm using preloaded SKTexture's and doing [object setTexture:texture]; inside of the update:currentTime method.

The problem is that sometimes the texture gets really distorted/stretched. After a lot of debugging, I have narrowed it down to only happening when the node is stationary. In fact, if I move the node a pixel and move it back like this, the problem never occurs:

[self setTexture:texture];
CGPoint currentPosition = self.position;
self.position = CGPointMake(currentPosition.x + 1, currentPosition.y + 1);
self.position = currentPosition;

This feels extremely hacky to me. I think under the hood, it's triggering a redraw on the parent node. Has anyone else experienced this? I have two major questions. 1) What is the cause? and 2) How can I resolve this without resorting to a hack?

Here is a normal frame and a stretched version (I apologize for the quality, placeholder art...)

not-stretchedstreched

Edit: After a few comments, I realized that I forgot to mention that I scaled the size of the node smaller than the size of the texture. Even though the textures are the same size, applying a new texture to a node with a smaller size causes the bug.

like image 290
nothappybob Avatar asked Mar 21 '23 14:03

nothappybob


1 Answers

It seems that upon setting the texture using setTexture: sprite node doesn't change it size, until being moved, resized, etc...

You can resolve this by manually setting the size after setting the texture.

[spriteNode setTexture:texture];
[spriteNode setSize:texture.size];
like image 166
Dobroćudni Tapir Avatar answered Mar 23 '23 12:03

Dobroćudni Tapir