Набор спрайтов, стреляющих снарядами в цель

#sprite-kit

#набор спрайтов

Вопрос:

Я хотел, чтобы мой турельный спрайт стрелял в монстра. я уже заставил турель повернуться к монстру и следовать за ним, пока он не выйдет за пределы досягаемости, теперь мне просто нужно произвести стрельбу.

каков наилучший способ выстрелить снарядом из башни в монстра?

я уже выполнил эту часть:

 -(void)shoot
{
     SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"CannonMissile-hd.png"]; ... i don't know what to do next

}
  

кроме того, мне нужно, чтобы он стрелял с интервалом в x секунд,

Спасибо

Ответ №1:

 -(void)shoot
{
    SKSpriteNode *turretNode;//I assume you have this node already in the scene . Dont use this line
    SKSpriteNode *enemy;//I assume you have this node already in the scene . Dont use this line
    SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"CannonMissile-hd.png"];
    bullet.zPosition = turretNode.zPosition -1;//if you want your bullet not to spawn on top of your turret
    [turretNode addChild:bullet];
    //you need to set the physics body of the bullet so you can detect contacts
    SKAction *move = [SKAction moveTo:enemy.position duration:0.5];//if u have multiple enemies then you have to deceide which one to hit
    [bullet runAction:move completion:^{
        [bullet removeFromParent];//removes the bullet when it reaches target
        //you can add more code here or in the didBeginContact method
    }];
    //repeat the process
    [self performSelector:@selector(shoot) withObject:nil afterDelay:5];//replace 5 with ur x seconds
    //that's it
}