我不完全理解雪碧工具包动画中的最佳选择;1)苹果在“冒险”的例子中使用这种方法,它们以nsarray中的图片形式存储在内存动画中:
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]发布于 2014-08-08 05:50:29
将SKTextures存储在NSArray中是用于动画的推荐方法。我也不能说我发现SpriteKit在这个主题上缺乏文档,因为SKAction有animateWithTextures方法。
您确实可以拥有一个具有给定NSArray定义的给定动画的NSArray,并且可以将这些动画存储在带有动画名称的NSMutableDictionary中作为密钥。但是,我在上面看到的这个方法只有animateTextures代码行在他们的fireAnimationState方法中一次,您可以向它传递参数,比如animationState和key。
我认为你需要做的不仅仅是表面观察他们的方法来确定他们为什么选择走这条路。您可以看到,animationState是在动画完成时使用的,很可能触发了其他一些东西。
[self runAction:action]确实很简单,但是也很容易看出它不是以任何方式管理animationState或密钥,我不得不假设他们决定了他们的游戏需要做什么。
还请记住,他们的方法可能有一个更高的层次,在给定的玩家类中,它调用一种灵活的方法来为给定的游戏实体创建动画,并做其他的事情,而不仅仅是改变动画。因此,在他们的高级编码中,他们可能会做更像这样的事情:
[self runAnimation:@"jump"];
甚至是
[self jump];
因为他们设计了一个低级别的系统来管理动画状态并为给定的管理设计添加键,所以他们不需要对您所指出的长行进行编码,除非在上面看到的方法中有一次。
我发现存储帧的NSArray而不是包装在SKAction中的原因有几个,因为有时我想要操作动画的开始帧或以不同的速度运行动画。但是,您可能需要也可能没有必要操纵动画的这些方面。
https://stackoverflow.com/questions/24439162
复制相似问题