Ell*_*Yap 5 objective-c sprite-kit skaction
我听触摸并将 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
Run Code Online (Sandbox Code Playgroud)
通常,您可以SKActions使用该方法使它们同时运行group,并使用该sequence方法让它们顺序运行。
但如果您需要一个排队系统,而不是构建自己的系统,请使用本机操作队列来为您完成此操作。因此,您可以创建一个串行操作队列并向其中添加操作。问题是您不希望操作在完成之前完成SKAction。
因此,您可以将您的代码包装SKAction在一个并发NSOperation子类中,该子类仅在SKAction执行时完成。然后你可以将你的操作添加到一个serial中NSOperationQueue,然后它在完成前一个操作之前不会启动下一个操作。
因此,首先,创建一个如下所示的ActionOperation(从 的子类NSOperation):
// ActionOperation.h
#import <Foundation/Foundation.h>
@class SKNode;
@class SKAction;
@interface ActionOperation : NSOperation
- (instancetype)initWithNode:(SKNode *)node action:(SKAction *)action;
@end
Run Code Online (Sandbox Code Playgroud)
和
// ActionOperation.m
#import "ActionOperation.h"
@import SpriteKit;
@interface ActionOperation ()
@property (nonatomic, readwrite, getter = isFinished) BOOL finished;
@property (nonatomic, readwrite, getter = isExecuting) BOOL executing;
@property (nonatomic, strong) SKNode *node;
@property (nonatomic, strong) SKAction *action;
@end
@implementation ActionOperation
@synthesize finished = _finished;
@synthesize executing = _executing;
- (instancetype)initWithNode:(SKNode *)node action:(SKAction *)action
{
self = [super init];
if (self) {
_node = node;
_action = action;
}
return self;
}
- (void)start
{
if ([self isCancelled]) {
self.finished = YES;
return;
}
self.executing = YES;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.node runAction:self.action completion:^{
self.executing = NO;
self.finished = YES;
}];
}];
}
#pragma mark - NSOperation methods
- (BOOL)isConcurrent
{
return YES;
}
- (void)setExecuting:(BOOL)executing
{
[self willChangeValueForKey:@"isExecuting"];
_executing = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)setFinished:(BOOL)finished
{
[self willChangeValueForKey:@"isFinished"];
_finished = finished;
[self didChangeValueForKey:@"isFinished"];
}
@end
Run Code Online (Sandbox Code Playgroud)
例如,您可以在初始化过程中创建一个串行队列:
self.queue = [[NSOperationQueue alloc] init];
self.queue.maxConcurrentOperationCount = 1;
Run Code Online (Sandbox Code Playgroud)
然后您可以向其中添加操作:
SKAction *move1 = [SKAction moveTo:point1 duration:2.0];
[self.queue addOperation:[[ActionOperation alloc] initWithNode:nodeToMove action:move1]];
Run Code Online (Sandbox Code Playgroud)
您可以稍后添加更多操作:
SKAction *move2 = [SKAction moveTo:point2 duration:2.0];
[self.queue addOperation:[[ActionOperation alloc] initWithNode:nodeToMove action:move2]];
Run Code Online (Sandbox Code Playgroud)
因为队列是串行的,所以您知道在完成move2之前不会启动。move1
| 归档时间: |
|
| 查看次数: |
1555 次 |
| 最近记录: |