我试图在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];
}
}
答案 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)
这使您可以在每个敌人到达屏幕顶部时直接访问它们。
以下是如何创建屋顶并让敌人与之互动的示例。 https://stackoverflow.com/a/24195006/2494064