2013-10-03 77 views
0

我使用TextureAtlas中的紋理加載動畫。iOS SpriteKit動畫不出現

但是動畫並未出現在屏幕上。

我只有當我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; 
} 
+0

除了SpriteKit?當我將代碼粘貼到新的SpriteKit Game模板中時,atlasWithRetinaCorrection:animationTextureFrames都不是已知的選擇器。 –

+0

atlasWithRetinaCorrection是它增加了2倍@到參數時,我們有一個視網膜顯示 – tallis

+0

animationTextureFrames是另一個範疇,其過濾所需要的animationTexture名稱 – tallis

回答

3

我發現了錯誤!我不得不預先加載紋理。

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

這解決了我的問題。謝謝。 –

+0

我發現這完全不能解決問題。如果在某個時刻預先加載了很多紋理,紋理仍然會出錯。 –

1

這是我做我的,還沒有過任何問題呢。

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]; 
+0

嗨,我也有一個repeatActionForever動畫,它有效。問題在於不重複的動畫。 – tallis

+0

它適合我循環使用 – DogCoffee

0

NSLog與大多數其他操作相比非常緩慢。只需插入該呼叫會爲應用的主線添加重大暫停。我猜你的紋理加載正在向GPU發送任務。沒有NSLog調用,GPU可能沒有足夠的時間來完成任務。也許你可以預先加載紋理。

+0

我有一個類似的想法,我已經這樣做了。我很早就加載了數組中的紋理..但結果是一樣的 – tallis

0

轉到找你項目,並按照下列步驟操作: - 1>轉到構建設置,2>搜索啓用紋理圖集生成和你在使用選擇