-1

I am trying to rotate a sprite using swift, IOS 10 and the touchesmoved function, the code I have works in the fact it moves the sprite but in not the right way, the wrong directions etc.

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
   super.touchesMoved(touches, with: event)
   guard let touch=touches.first{
         return
   }
   let touchLocation = touch.location(in: self)
   self.zRotation=CGFloat(atan2(Double(touchLocation.y), Double(touchLocation.x)))
}

does anyone know the correct way to rotate a sprite using zRotation and touchesMoved? Thank you

craig
  • 103
  • 1
  • 7
  • You need to provide more detail on what you are trying to do. Right now you are getting the angle relative to your scene coordinate system, not your sprite, so if your sprite is not at (0,0) you will not see the sprite point to where your finger is – Knight0fDragon Dec 16 '16 at 16:24
  • What is the self here ? Is it a subclass of an `SKSpriteNode`? – Whirlwind Dec 16 '16 at 19:33
  • Self is a subclass of skspritenode yes – craig Dec 16 '16 at 22:04
  • As Knight0fDragon pointed, you have to say what exactly you want to achieve here, eg. when you move a finger over the sprite (or somehow else) what exactly are you trying to see. If you want to just rotate a sprite towards the tap location in the scene's coordinate system, then see this (Aiming part) : http://stackoverflow.com/a/36235426/3402095 – Whirlwind Dec 16 '16 at 23:09

2 Answers2

1

I think you've probably overlooked the wonderful power of constraints in SpriteKit. Easy enough to do, Apple does a terrible job of promoting, presenting and educating those considering using SpriteKit.

Constraints allow you to say "go here", or "look at this".

In your case, the "look at this" is ideal, from the docs:

Orientation Constraints

A common use for orientation constraints is to make a look-at constraint. For example, you may create a look-at constraint to make a pair of eyes follow a moving object or to have a rocket point in the direction of its target.

More specifically, you can use these in your touchesMoved:

https://developer.apple.com/reference/spritekit/skconstraint/1519627-orient

Confused
  • 6,048
  • 6
  • 34
  • 75
0

thanks for all the suggestions. Yes my explanation of the problem was not too great. so will add the full code here and it seems to be working apart from occasionally moving by 90 degrees on its own when i stop dragging and start dragging again

What I was trying to do was rotate a sprite around another sprite for example a cannon barrel around its wheel... When the wheel is loaded from the scene it calls this class and adds a child node (the barrel) which should be able to be moved around its wheel

import SpriteKit
class WheelNode: SKSpriteNode, CustomNodeEvent{
  private var tubeNode=SKSpriteNode(imageNamed: "cannon-barrel")
  var previousPoint:CGPoint!
  var touchLocation:CGPoint!
  func didMoveToScene() {
    guard let scene=scene else{
        return
    }
    isUserInteractionEnabled=true
    tubeNode.anchorPoint=CGPoint(x:0, y:0)
    tubeNode.position=position
    self.addChild(tubeNode)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch=touches.first{
            previousPoint=touch.location(in: scene!)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        if let touch=touches.first{
            touchLocation=touch.location(in: scene!)
            var angle=atan2(touchLocation.y-previousPoint.y, touchLocation.x-previousPoint.x)
            self.zRotation=angle
        }
    }
}
craig
  • 103
  • 1
  • 7