1

I make my first application and the question arose how to make it so that one object would follow another with a certain speed, tried to find the answer to the question but did not find it. This is what I did, but without delay

EDIT: gif is below.

create Hero and Guns.

 func createHero() {
 
    hero = SKSpriteNode(texture: heroTexture)
    
    //Anim hero
    heroTextureArray = [SKTexture(imageNamed: "hero.png"), SKTexture(imageNamed: "Fly1.png"), SKTexture(imageNamed: "Fly2.png"), SKTexture(imageNamed: "Fly3.png"), SKTexture(imageNamed: "Fly4.png")]
    let heroFlyAnimation = SKAction.animate(with: heroTextureArray, timePerFrame: 0.1)
    let flyHero = SKAction.repeatForever(heroFlyAnimation)
    hero.run(flyHero)
    
    hero.position = position
    hero.size.height = 60
    hero.size.width = 65
    
    hero.physicsBody?.isDynamic = true
    hero.physicsBody?.allowsRotation = false
    hero.zPosition = 1
    
    heroObject.addChild(hero)
}


 func createGuns(){
    
    gunsTexture = SKTexture(imageNamed: "guns")
    
    guns = SKSpriteNode(texture: gunsTexture)
    
    guns.zPosition = 1
    guns.position = CGPoint(x: self.frame.width/2, y: 0)
    guns.physicsBody?.isDynamic = true
    guns.size.height = 35
    guns.size.width = 40
    guns.zRotation = CGFloat(.pi/2.0)

    
    gunsObject.addChild(guns)
    
}

movement of the hero and the пгты

var flag = false

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
        let location = touch?.location(in: self)
    
        if (physicsWorld.body(at: location!) != nil) {
            
           flag = true
        
    }
}

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
     let touchLocation = touch!.location(in: self)
    let previousLocation = touch!.previousLocation(in: self)
    
    
    let hero = self.hero
    let guns = self.guns
    var heroX = hero.position.x + (touchLocation.x - previousLocation.x)
    
    heroX = max(heroX, hero.size.width / 2)
    heroX = min(heroX, size.width - hero.size.width / 2)
    
    var heroY = hero.position.y + (touchLocation.y - previousLocation.y)
    
    heroY = max(heroY, hero.size.height / 2 + 20)
    heroY = min(heroY, size.height - hero.size.height / 2)
    hero.position = CGPoint(x: heroX, y: heroY)
    guns.position = CGPoint(x: heroX, y: guns.position.y)
    guns.position = CGPoint(x: heroX, y: 20)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    flag = false
}

enter image description here

Community
  • 1
  • 1
Vasya2014
  • 203
  • 1
  • 5
  • 25
  • You need to clarify your question better, what do you mean by delay? Does it wait a second then persue, or is it always X pixels behind? – Knight0fDragon Sep 21 '17 at 15:14
  • @KnightOfDragon sprite2 need to follow sprite1 with less speed than sprite1 – Vasya2014 Sep 21 '17 at 15:18
  • use your variable terms, not new terms – Knight0fDragon Sep 21 '17 at 15:19
  • @Knight0fDragon I added gif where it is shown that the 2 sprites are moving at the same speed, I need that the lower sprite would move along the same triage as the second sprite but with a lower speed – Vasya2014 Sep 21 '17 at 15:28
  • I dont know what `sprite1` and `sprite2` are, your code doesn't have anything called `sprite` – Knight0fDragon Sep 21 '17 at 15:29
  • @Knight0fDragon I added the sprite name to the code. guns need to follow hero with less speed – Vasya2014 Sep 21 '17 at 15:52
  • I recommend moving the guns to the new x-coordinate using an SKAction, rather than setting the x-coordinate directly. You can calculate the horizontal speed of the hero by using his delta-x, and construct your SKAction such that its speed is slightly lower. Or perhaps you would rather always have your guns move at a fixed speed. That would be even easier, and might seem more natural. There might be other ways to do it, but that's the first idea that occurs to me. – SaganRitual Sep 21 '17 at 15:55
  • @GreatBigBore thank you – Vasya2014 Sep 21 '17 at 16:07

1 Answers1

1

it was necessary to add SKAction

let moveGuns = SKAction.moveTo (x: heroX, duration: 3) 
let moveBgForever = SKAction.repeatForever (SKAction.sequence ([moveGuns]))
guns.run (moveBgForever)
Vasya2014
  • 203
  • 1
  • 5
  • 25