1

This has been addressed in How do I drag and drop a sprite in Swift 3.0?.

But... this this gives me a bad lag between finger and sprite. With Xcode, try creating a from-scratch iOS Game app, using SpriteKit. Comment out all the touch-handling code and replace it with the following

   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let t = touches.randomElement()!
        self.spinnyNode?.position = t.location(in:self)
    }

(You will also have to do a few obvious things to make the spinnyNode stay visible.)

The result is shown here at http://andrewduncan.net/drag/drag2.m4v

The following posts all recommend the same approach — setting the position in touchesMoved().

I have implemented a bletcherous hack (see Using SpriteKit inside UIKit) where I have small SKViews (one per sprite) that are quickly draggable using UIGestureRecognizers. But each view needs its own SKScene, which seems backward (or orthogonal?) to the original intent.

Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55
  • you need to share the code for what you are doing, also how you are making your m4v videos. If you are doing it on the simulator, then it is going to lag be cause the simulator is not an emulator, it is pretending to be an iOS device, it is not designed to actually mimic the iOS hardware – Knight0fDragon Oct 05 '18 at 23:55
  • The first line in the question cited your own earlier answer. Which I implemented on the Apple sample code. I have rephrased the question to show how to reproduce this. The symptom is only a bit better on devices. The lag only gets longer as the complexity of the scene grows. The movie is a faithful (e.g. no sub-sample aliasing) representation of the simulator behavior. I'm on a 2.8 GHz laptop. – Andrew Duncan Oct 06 '18 at 05:45
  • Oh you are doing the SpriteKit approach, you should use an skaction so that it isnt jumping, that is not lag, that is you moving the spinny node fast over large distances – Knight0fDragon Oct 06 '18 at 05:49
  • Please note that in the first-referenced post, you recommended setting the `position` directly, saying "This is the correct way to do it in Sprite Kit.". I have edited the OP to show how many different people concur. Nonetheless, I replaced the code in `touchesMoved()` with this: `spinnyNode?.run(SKAction.move(to: t.location(in:self), duration: 0))` Same lag. Longer durations, of course, lag more. – Andrew Duncan Oct 06 '18 at 17:18
  • You need to chain your actions, you cant have the newest action running with the previous action – Knight0fDragon Oct 06 '18 at 17:21
  • Also when I said correct way, I was talking about the correct way to retain the sprite to drop it, not the correct way to create a smooth panning effect, I was keeping it simple for the OP – Knight0fDragon Oct 06 '18 at 17:23
  • Hmm, not clear to me how to chain actions. I don't know any future moves, so how can I string them into a chain? I did try this inside the touchesMoved() method. (Pardon the formatting.) No difference. `spinnyNode?.removeAllActions() ; var moves = [SKAction]() ; for t in touches { moves.append(SKAction.move(to: t.location(in:self), duration: 0)) } ; let sequence = SKAction.sequence(moves) ; spinnyNode?.run(sequence)` – Andrew Duncan Oct 06 '18 at 18:41
  • Well you are doing a duration of 0, that will have the same effect as setting the position directly, you need to have some kind of duration so that the node can move slower than the touch, this way the animation can be finer. If you want the node to follow the touch at all times, then you will need to create a blur effect of some kind. Set up a sequence, and on the last part of the sequence, use a run block to check an array and start the next action you need – Knight0fDragon Oct 06 '18 at 18:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181407/discussion-between-andrew-duncan-and-knight0fdragon). – Andrew Duncan Oct 06 '18 at 19:22

0 Answers0