Spritekit暂停并通过touchesBegan恢复SKSpriteNode的动作

时间:2014-11-02 20:48:01

标签: sprite-kit skspritenode

我有一个重复移动的SKSpriteNode,让我称之为RunAction A,例如上下移动。现在我想做一个动作,我把它叫做RunAction B,左边 - 中间 - 右边 - 中间,通过touchesBegan。

完成RunAction B后,应该恢复RunAction A.它应该开始一个位置,RunAction B确实开始并停止了。

如果我使用(注意伪郎。)

 [sprite RunAction A] 
 [sprite setPaused: True]  
 [sprite RunAction B]
 [sprite setPaused: False]

我可以看到,精灵从未停顿过来!

是否有可能让精灵让它恢复之前停止的动作?

由于

2 个答案:

答案 0 :(得分:0)

每个动作都有一个精灵套件中的完整处理程序,因此您可以通过使用以下句子轻松获取动作完成或完成

//动作完成动作后,您可以执行任何操作

[sprite runAction: A  completion:^{
                    [sprite setPaused: True]  

                }];

答案 1 :(得分:0)

#import "GameScene.h"

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];

    myLabel.text = @"Hello, World!";
    myLabel.fontSize = 65;
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));
    NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"fireflies" ofType:@"sks"];
    SKEmitterNode *myParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
    [self addChild:myParticle];
    self.physicsWorld.gravity=CGVectorMake(0.0, -9);
    self.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    self.physicsBody.linearDamping=0.0;

    SKSpriteNode *rectNode=[SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(50, 50)];
    rectNode.name=@"rect";
    rectNode.position=CGPointMake(300, 300);
    [self addChild:rectNode];
    [self moveAction];




}
-(SKAction*)groupAction:(CGPoint)points
{
    SKAction *A=[SKAction moveTo:CGPointMake(points.x+100, points.y+100) duration:1];
    SKAction *B=[SKAction  moveTo:CGPointMake(500, 100) duration:1];
     SKAction *C=[SKAction  moveTo:CGPointMake(100, 100) duration:1];
    SKAction *s=[SKAction sequence:@[A,B,C]];
    return s;
}
-(void)moveRect:(SKNode*)node
{
    [node runAction:[SKAction  repeatActionForever:[self groupAction:node.position]] withKey:@"moveAction"];
}
-(void)moveAction
{
    [self enumerateChildNodesWithName:@"rect" usingBlock:^(SKNode *node, BOOL *stop) {
        [self moveRect:node];
    }];
}
-(void)clearAction
{
    [self enumerateChildNodesWithName:@"rect" usingBlock:^(SKNode *node, BOOL *stop) {
        [node removeActionForKey:@"moveAction"];
    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        if(!_stop)
        {
          [self clearAction];
            _stop=TRUE;
        }
        else
        {
            [self moveAction];
            _stop=FALSE;
        }
        /*
        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
        sprite.physicsBody.allowsRotation=FALSE;
        sprite.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(sprite.size.width, sprite.size.height)];
        sprite.physicsBody.friction=0.0;
        sprite.physicsBody.linearDamping=0.0;
        sprite.physicsBody.restitution=1.0;
        sprite.xScale = 0.5;
        sprite.yScale = 0.5;
        sprite.position = location;



        [self addChild:sprite];
         */
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

看看这个例子我只使用了一个矩形,但你可以使用你想要的任意数量的对象,确保不同的动作组包含不同的名称,这样当用户触摸屏并应用时,您可以轻松删除和暂停动作。删除或暂停操作时对另一个对象的新操作组。