Dvole
Dvole

Reputation: 5795

SKTexture returns wrong size?

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.

Upvotes: 0

Views: 984

Answers (2)

iago849
iago849

Reputation: 1854

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!

Upvotes: 1

Dvole
Dvole

Reputation: 5795

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.

Upvotes: 6

Related Questions