0

I have an atlas of images, they are 80 by 80. But SKTexture says some of frames are 33x80 despite them clearly being 80x80. What is wrong? My animation looks very wrong when it runs.

Here is the code:

SKTextureAtlas *myAtlas = [SKTextureAtlas atlasNamed:@"my"];
NSInteger numberOfImages = [run.textureNames count];

NSLog(@"atlas: %@", myAtlas);

for (int i = 0; i < numberOfImages; i++) {
    NSString *textureName = [NSString stringWithFormat:@"walk%.2d", i];
    SKTexture *temp = [myAtlas textureNamed:textureName];
    [self.runningFrames addObject:temp];
}

Logging returns this:

<SKTextureAtlas> 'my' 16 textures:
 (
    "<SKTexture> 'walk00.png' (33 x 80)",
    "<SKTexture> 'walk01.png' (80 x 80)",
    "<SKTexture> 'walk02.png' (33 x 80)",
... omitted
)

Why is that? My animation goes completely out of hand, it shifts from side to side, etc.

Dvole
  • 5,725
  • 10
  • 54
  • 87
  • does walk one have a lot of transparent space (alpha) areas ? See this video for explanation of why i asked http://www.youtube.com/watch?v=TDwSR3e6nN0 – DogCoffee Oct 09 '13 at 10:40
  • @Smick yes, it has lots of alpha. So I should add 1% alpha to all my images as background to avoid that? This seems like a hack – Dvole Oct 09 '13 at 10:58
  • Yeah, is a hack, so I would do what the guy in the video does. – DogCoffee Oct 09 '13 at 10:59
  • I don't like the links to the video, Youtube isn't loading right now for instance. Can you just say what he does. – Cocorico Dec 13 '13 at 05:52
  • I watched the video looking for help on the same issue, but what the guy in the video does is exactly the hack Dvole was proposing. – cargath Nov 29 '14 at 17:43
  • Sometimes an incorrect size can be due to the image itself, see: http://stackoverflow.com/questions/19744111/sprite-kit-os-x-sktexture-size-property-is-incorrect – CodeSmile Apr 21 '15 at 12:22

2 Answers2

6

I found a solution to the problem. I was animating using method

[self.player runAction:[SKAction repeatActionForever:[SKAction animateWithTextures:self.runningFrames timePerFrame:0.05 resize:NO restore:YES]] withKey:@"playerRunning"];

Changing resize to YES fixed all my problems.

Dvole
  • 5,725
  • 10
  • 54
  • 87
  • Well done! I was looking for stuff in SKTextureAtlas, didnt think to look in SKAction. Will have to do some testing with this method. – DogCoffee Oct 09 '13 at 14:21
  • great!!! this solve my problem too thanks! but still do not understand why.. i simply do animateWithTextures:timePerFrame: , and at the beginning everything worked fine, but after some time start the problems.. – Ilario Mar 11 '14 at 11:06
1

Despite the question is rather old, in my runner there still was a problem with collision detection. Earlier I used to use such a method:

- (CGRect)collisionBoundingBox
{
    return self.frame;
}

However, it seems that SKAction or something changes the frame of my sprite despite "resize" flag (iOS 7.1). Once I've changed implementation to:

- (CGRect)collisionBoundingBox
{
    CGFloat x = self.position.x;
    CGFloat y = self.position.y;
    CGFloat w = self.texture.size.width;
    CGFloat h = self.texture.size.height;
    return CGRectMake(x - w / 2, y - h / 2, w, h);
}

it works really fine, because texture.size is constant! Anyway, I think it's a glitch - I don't want some engine to change frame of my sprite!

iago849
  • 1,818
  • 1
  • 13
  • 9