SKAction完成后重新定位精灵节点

时间:2014-06-27 14:11:24

标签: ios7 sprite-kit skaction

我试图在SKAction moveTo动作完成后重新定位我的精灵。我已将敌人编程为进入屏幕(self.frame.size.width / 2,0)。它们仅在y轴上移动。当他们的y位置大于(self.frame.size.height)并且以相同的方式一次又一次地移动它们直到玩家杀死所有敌人时,我想将它们重新定位在初始位置。我在这一点上苦苦挣扎。我应该添加哪些代码?有任何想法吗?此代码可能有助于您了解我的实现:

-(void) addEnemies {

for (int j = 0; j < 6; j++) {

   SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:@"player"];
    enemy.position = CGPointMake(((self.frame.size.width) -20) - j * (enemy.frame.size.width) , 0);


    CGPoint realDest = CGPointMake((enemy.position.x), (self.frame.size.height));
    float velocity = 50/1.0;
    float realMoveDuration = self.size.height / velocity;
    SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
    [enemy runAction:actionMove];

    [self addChild:enemy];
}

}

2 个答案:

答案 0 :(得分:1)

-(void)addEnemies{
    for (int j = 0; j < 6; j++) {

        SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:@"player"];
        enemy.position = CGPointMake(((self.frame.size.width) -20) - j * (enemy.frame.size.width) , 0);

        [self addChild:enemy];
        [self moveEnemyNode:enemy];
    }
}
-(void)moveEnemyNode:(SKSpriteNode *)enemy{
    enemy.position = CGPointMake(enemy.position.x, 0);
    float velocity = 50/1.0;
    float realMoveDuration = self.size.height / velocity;
    SKAction *actionMove = [SKAction moveToY:self.frame.size.height + enemy.frame.size.height duration:realMoveDuration];
    [self runAction:actionMove completion:^{
        [self moveEnemyNode:enemy];
    }];
}

答案 1 :(得分:0)

  1. 创建一个复制屋顶的SKSpriteNode - &gt;矩形(self.frame.size.width,1)
  2. 让它坐在屏幕的顶部边缘,使用非动态物理主体
  3. 设置&#34;屋顶&#34;和敌人互相联系
  4. 调用联系方式,重置位置并调用SKaction
  5. 这使您可以在每个敌人到达屏幕顶部时直接访问它们。

    以下是如何创建屋顶并让敌人与之互动的示例。 https://stackoverflow.com/a/24195006/2494064