Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit animateWithTextures not work with texture atlas

When I don't use texture atlas,everything works fine.But when I use texture atlas,animateWithTextures doesn't work and nothing appears. Here is my code

SKTexture *spaceshipTexture = [SKTexture textureWithImageNamed:@"monkey.png"];
SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithTexture:spaceshipTexture];
spaceship.position = CGPointMake(0,0);
spaceship.anchorPoint = CGPointMake(0,0);
[self addChild: spaceship];

NSMutableArray *images=[NSMutableArray arrayWithCapacity:14];
for (int i=1; i<=14; i++) {
    NSString *fileName=[NSString stringWithFormat:@"%dShuGuangx.png",i];
    SKTexture *tempTexture=[SKTexture textureWithImageNamed:fileName];
    [images addObject:tempTexture];
}
NSLog(@"count %d",images.count);
SKAction *walkAnimation = [SKAction animateWithTextures:images timePerFrame:0.1];
[spaceship runAction:walkAnimation];
like image 775
Han Pengbo Avatar asked Oct 08 '13 10:10

Han Pengbo


1 Answers

[SKTexture preloadTextures:images withCompletionHandler:^(void){
        [spaceship runAction:walkAnimation];
    }];

This solved my problem.

like image 72
Han Pengbo Avatar answered Sep 30 '22 13:09

Han Pengbo