2 Replies
      Latest reply on Jun 4, 2019 5:59 AM by johnqA
      johnqA Level 1 Level 1 (0 points)

        I am building a spriteKit game in Xcode 10.2.1 with swift 5, where the player has to reach the end of a large background (avoiding enemies, obstacles etc.) in order to complete the level. The camera is tied to the player spriteNode's position.x and the player can move the spriteNode in any direction with swipes.

         

         

         

         

        I am using a cobbled together UIPanGestureRecognizer to enable this movement, and this seems to work reasonably well.

         

         

         

         

            @objc func handlePan(recognizer: UIPanGestureRecognizer) {  

                let transformerX = 1024/self.view!.frame.size.width

                let transformerY = 768/self.view!.frame.size.height

                if recognizer.state == .began {

                    lastSwipeBeginningPoint = recognizer.location(in: self.view)

                } else if (recognizer.state == .ended) {

                    if scrolling { // this is controlls whether player can be moved - scrolling is set to true once introductory elements have run

                        if playerDamage < 4 { //player cannot be controlled and falls off screen if it has 4 damage

                            let velocity = recognizer.velocity(in: self.view)

                            player.physicsBody?.applyImpulse(CGVector(dx: velocity.x * transformerX / 5.4, dy: velocity.y * transformerY * -1 / 5.4)) //-1 inverts y direction so it moves as expected

                         }

                     }

                 }

            }

         

         

        This does allow movement around the level, but not in as precise and controlled a way as I would like. The player moves off at the set speed but any further swipes (to change direction/avoid obstacles) seem to multiply the speed and generally make it difficult to control accurately.

        Is there a way of choking off the speed, so that it starts off at the set speed but then starts to lose momentum, or is there a better way altogether of controlling multi-direction movement?

        • Re: How to concisely control a spriteNode's movement with swipes
          gchiste Apple Staff Apple Staff (190 points)

          Hello,

           

          "The player moves off at the set speed but any further swipes (to change direction/avoid obstacles) seem to multiply the speed and generally make it difficult to control accurately."

           

          Impulses, forces, and torques are additive, meaning that every time you apply an impulse to a physics body, it is summed with the currently active impulses on the physics body.  To get around this additive behavior, you can set the physicsBody.velocity to zero right before you apply a new impulse.

           

          "Is there a way of choking off the speed, so that it starts off at the set speed but then starts to lose momentum"

           

          Yes, in fact, the linearDamping of your physicsBody should be set to 0.1 by default, so this should already be happening to some extent (i.e. your body will not conitnue in linear motion forever unless another force/impulse is applied).  To exaggerate the damping you can increase the value of the linearDamping property until you find a damping coefficient that feels right for your game.