2014-06-26 21 views
2

我不完全明白在精靈套件動畫的最佳選擇; 1)蘋果在「冒險」例如使用這種方法,他們在內存中的動畫如圖片存儲的NSArray:存儲在內存NSArray與圖片或SKAction

static NSArray *sSharedTouchAnimationFrames = nil; 
- (NSArray *)touchAnimationFrames { 
    return sSharedTouchAnimationFrames; 
} 

- (void)runAnimation 
{ 
    if (self.isAnimated) { 
     [self resolveRequestedAnimation]; 
    } 
} 

- (void)resolveRequestedAnimation 
{ 
    /* Determine the animation we want to play. */ 
    NSString *animationKey = nil; 
    NSArray *animationFrames = nil; 
    VZAnimationState animationState = self.requestedAnimation; 

    switch (animationState) { 

     default: 
     case VZAnimationStateTouch: 
      animationKey = @"anim_touch"; 
      animationFrames = [self touchAnimationFrames]; 
      break; 

     case VZAnimationStateUntouch: 
      animationKey = @"anim_untouch"; 
      animationFrames = [self untouchAnimationFrames]; 
      break; 
    } 

    if (animationKey) { 
     [self fireAnimationForState:animationState usingTextures:animationFrames withKey:animationKey]; 
    } 

    self.requestedAnimation = VZAnimationStateIdle; 
} 

- (void)fireAnimationForState:(VZAnimationState)animationState usingTextures:(NSArray *)frames withKey:(NSString *)key 
{ 
    SKAction *animAction = [self actionForKey:key]; 
    if (animAction || [frames count] < 1) { 
     return; /* we already have a running animation or there aren't any frames to animate */ 
    } 

    [self runAction:[SKAction sequence:@[ 
             [SKAction animateWithTextures:frames timePerFrame:self.animationSpeed resize:YES restore:NO], 
             /* [SKAction runBlock:^{ 
     [self animationHasCompleted:animationState]; 
    }]*/]] withKey:key]; 
} 

我很欣賞這種方法,但我無法理解。將SKAction存儲在內存中不是更好的選擇,並且總是像這樣使用動畫?

[self runAction:action]; 

沒有使總是新的SKAction;

[SKAction animateWithTextures:frames timePerFrame:self.animationSpeed resize:YES restore:NO] 
+0

蘋果關於SpriteKit的文檔是......缺乏禮貌。我發現很多情況下,我的代碼比這裏的例子顯着更短,更甜。我一般信賴雷。 http://www.raywenderlich.com/45152/sprite-kit-tutorial-animations-and-texture-atlases – meisenman

+0

在ray的教程中還介紹了這種方法,當動畫圖像存儲在NSArray – user3780799

+0

不妨測試它的性能和答案你自己的問題。社區肯定會受益。 – meisenman

回答

1

存儲在NSArraySKTextures是用於動畫的推薦方法。我不能說我發現SpriteKit的文檔缺乏這方面的內容,因爲SKAction的方法是animateWithTextures

你確實可以有一個SKAction具有由給定NSArray定義一個給定的動畫,也許那些存儲在一個NSMutableDictionary與關鍵的動畫名。但是,我上面看到的這種方法在其fireAnimationState方法中只有animateTextures一行代碼,您可以將參數傳遞給它,例如animationState和。

我認爲你需要採取更多的表面看他們的方法來確定他們爲什麼選擇走這條路線。您可以看到animationState在動畫完成時被利用,並可能觸發了其他事情。

[self runAction:action]確實很簡單,但也很容易發現它不以任何方式管理animationState或key,我必須假定他們決定他們的遊戲需要做。

另外請記住,他們的方法可能有一個更高的水平,在給定的玩家類中,它會調用一個靈活的方法來爲給定的遊戲實體制定動畫併除了更改動畫之外還要做其他事情。因此,在他們的高層次的編碼,他們可能會做更多的東西是這樣的:

[self runAnimation:@"jump"];

甚至

[self jump];

因爲他們已經設計管理的動畫狀態,並增加了一個低級別的系統對於一個給定的管理設計來說,他們不需要對你指出的長線進行編碼,除非你在上面看到的那種方法。

我發現存儲NSArray幀並且不存儲包含在SKAction中的一些原因是我有時想要操作動畫的開始幀或以不同的速度運行動畫。但是,您可能有也可能沒有必要操縱動畫的這些方面。

+0

感謝您的描述! – user3780799