Posts

Post marked as solved
2 Replies
1.1k Views
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?
Posted
by johnqA.
Last updated
.