iOS SpriteKitanimation不会出现

我使用TextureAtlas中的纹理加载animation。

但是animation不会出现在屏幕上。

我只有当我NSLog的纹理! 看起来像一个错误。

有人有类似的经验吗?

SKTextureAtlas *atlas = [SKTextureAtlas atlasWithRetinaCorrection:spriteSheetName_]; if ([atlas.textureNames count] == 0) return; NSArray *r = [self getSprites:WEAPON_FIREANIMATION ofDict:atlas.textureNames]; if ([r count] == 0)return; SKSpriteNode *firstSprite = (SKSpriteNode*)[self childNodeWithName:tFIRSTSPRITE]; [firstSprite removeActionForKey:aFIRE_ANIM]; NSMutableArray *walkAnimFrames = [NSMutableArray array]; int k =0; if (self.weaponAnimDuration == 0) { k = (self.weaponShootInterval / 0.1f)/[r count]; } else { k = (self.weaponAnimDuration / 0.1f)/[r count]; } for (int i = 0; i<k; i++) { for (NSString *sname in r) { [walkAnimFrames addObject:[SKTexture textureWithImageNamed:sname]]; } } NSLog(@"%@",walkAnimFrames); SKAction *weaponAnim = [SKAction animateWithTextures:walkAnimFrames timePerFrame:0.1f]; [firstSprite runAction:weaponAnim withKey:aFIRE_ANIM]; 

 #import "SKTextureAtlas+MySKTextureAtlas.h" @implementation SKTextureAtlas (MySKTextureAtlas) +(SKTextureAtlas*)atlasWithRetinaCorrection:(NSString*)name { if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) { name = [NSString stringWithFormat:@"%@@2x",name]; } NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"atlasc"]; if (path == Nil) return Nil; return [SKTextureAtlas atlasNamed:name]; } 

 @implementation NSArray (Tools) -(NSArray*)animationTextureFrames { NSMutableArray *rr = [NSMutableArray new]; for (NSString *name in self) { SKTexture *tex = [SKTexture textureWithImageNamed:name]; [rr addObject:tex]; } return rr; } 

我发现了错误!

我不得不预先加载纹理。

  [SKTexture preloadTextures:explosionTextures withCompletionHandler:^(void){}]; 

我就是这样做的,而且还没有任何问题。

  NSMutableArray *textures = [NSMutableArray arrayWithCapacity:5]; for (int i = 1; i < 5; i++) { NSString *textureName = [NSString stringWithFormat:@"rocket%d", i]; SKTexture *texture = [SKTexture textureWithImageNamed:textureName]; [textures addObject:texture]; } self.rocketAnimation = [SKAction animateWithTextures:textures timePerFrame:0.1]; // add animation to my sprite [self.projectile runAction:[SKAction repeatActionForever:self.rocketAnimation]]; // add spite to screen etc... [self addChild:self.projectile]; 

看到这篇文章,有一个错误,当图像有很多使用纹理图集的透明空间。

SKTexture返回错误的大小?

与大多数其他操作相比,NSLog速度非常慢。 只要插入该调用,就会在应用程序的主线程中增加一个重要的暂停。 我猜你的纹理加载正在向GPU发送任务。 没有NSLog调用,GPU可能没有足够的时间来完成任务。 也许你可以预先加载纹理。

转到你的项目,并按照下列步骤: – 1>转到生成设置,2>search启用纹理地图集生成,并select