我聽觸摸並將SKAction添加到一個精靈。如果現有的動作尚未完成,我希望將動作添加到隊列中,以便它將一個接一個地執行。 任何經驗類似的設計?將SKAction添加到Sprite隊列運行一個接一個
我沒有使用數組和塊。如果有更簡單的方法?
@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
只是我們的好奇心:Xcode向我展示了'__unsafe_unretained typeof(self)weakSelf = self;'的ARC Retain Cycle警告。代碼是否正確? –
我這麼認爲。爲了防止保留週期,我必須使其成爲弱參考。 –
好的,在使用[本答案](http://stackoverflow.com/a/17011096/867635)代碼後警告消失了。關於你的問題:我還沒有找到一個更簡單的動畫隊列方法。 –