我不完全了解Sprite Kit动画中的最佳选择;
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]
最佳答案
建议将SKTextures
存储在NSArray
中是用于动画的推荐方法。我不能说我也没有找到SpriteKit的文档,因为SKAction具有animateWithTextures
方法。
您确实可以拥有一个SKAction
,该NSArray
具有由给定的NSMutableDictionary
定义的给定动画,并且可以将这些动画存储在带有密钥动画名称的fireAnimationState
中。但是,我在上面看到的该方法仅在其animationState
方法中包含了animateTextures行代码,您可以将诸如key
和animationState
的参数传递给它。
我认为您需要从根本上了解他们的方法,以确定他们为什么选择走这条路。您可以看到在完成动画时正在使用[self runAction:action]
并可能触发了其他操作。[self runAnimation:@"jump"];
确实很简单,但是也很容易看出它并不是以任何方式管理animationState或key,我必须假定他们确定了他们需要做的游戏。
还请记住,他们的方法可能具有更高的水平,在给定的玩家类别中,它调用的是一种灵活的方法,用于为给定的游戏实体制定动画并执行其他操作,而不仅仅是更改动画。因此,在他们的高级编码中,他们可能正在做这样的事情:[self jump];
甚至NSArray
而且,由于他们设计了一个低级系统来管理动画状态并为给定的管理设计添加了关键点,因此他们无需编码您指出的长行,只需要在上面看到的那种方法中进行一次编码即可。
我发现存储帧的SKAction
而不存储包装在ojit_code中有用的一些原因是,我有时想操纵动画的起始帧或以不同的速度运行动画。但是您可能需要也可能不需要操纵动画的那些方面。