我听触摸并将 SKAction 添加到精灵。如果现有操作尚未完成,我希望将操作添加到队列中,以便一个接一个地执行。有没有经验过的类似设计?
我确实使用了数组和块。如果有更简单的方法?
@interface GAPMyScene()
@property(strong,nonatomic)SKSpriteNode*ufo;
@property(strong,nonatomic)NSMutableArray*animationQueue;
@property(copy,nonatomic)void(^completeMove)();
@end
@implementation GAPMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.ufo = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
self.animationQueue = [[NSMutableArray alloc] init];
__unsafe_unretained typeof(self) weakSelf = self;
self.completeMove = ^(void){
[weakSelf.ufo runAction:[SKAction sequence:[weakSelf.animationQueue copy]] completion:weakSelf.completeMove];
NSLog(@"removeing %@", weakSelf.animationQueue);
[weakSelf.animationQueue removeAllObjects];
};
[self addChild:self.ufo];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKAction*moveAnimation = [SKAction moveTo:location duration:2];
if (![self.ufo hasActions]) {
[self.ufo runAction:moveAnimation completion:self.completeMove];
} else {
[self.animationQueue addObject:moveAnimation];
NSLog(@"in queue %@", self.animationQueue);
}
}
}
@end