0

So here is my code:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var ship1 = [2,1]
    var ship2 = [1,2]

    let jonahSpriteNode = SKSpriteNode(imageNamed: "jonah_spaceship")
    let georgeSpriteNode = SKSpriteNode(imageNamed: "george_spaceship")

    override func didMove(to view: SKView) {
        //var jonahFrames = [SKTexture]()
        jonahSpriteNode.position = CGPoint(x: 30, y: frame.midY)
        jonahSpriteNode.size = CGSize(width: 100.0, height: 100.0)
        addChild(jonahSpriteNode)

        georgeSpriteNode.position = CGPoint(x: 628, y: frame.midY)
        georgeSpriteNode.size = CGSize(width: 100.0, height: 100.0)
        addChild(georgeSpriteNode)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            var touchLocation = touch.location(in: self)
            var angle1 = atan2(jonahSpriteNode.position.y - touchLocation.y , jonahSpriteNode.position.x - touchLocation.x)
            var angle = angle1 - CGFloat(Double.pi / 1)

            makeCircularRange(to: jonahSpriteNode)

            let rotate = SKAction.rotate(toAngle: angle, duration: 1.0)
            let move = SKAction.move(to: CGPoint(x: touchLocation.x, y: touchLocation.y), duration: 2.5)
            let sequence = SKAction.sequence([rotate, move])
            jonahSpriteNode.run(sequence)
        }
    }

    func makeCircularRange(to node: SKNode) {
        let range = SKRange(lowerLimit: 0, upperLimit: 400)
        let constraint = SKConstraint.distance(range, to: .zero)
        node.constraints = [constraint]
    }

}

I wanted to display the SKRange by showing upperLimit in a certain color. I tried making nodes around the limit and then coloring the nodes but it just showed a bunch of errors. If you have any ideas please answer.

Something like this: With the sprite node in the middle to show where you can move

The sprite node will be in the center and the circle will show where it can move.

Jonah Legg
  • 121
  • 5
  • SKRange is a abstraction for complete one constraint, what do you want to draw on screen? Can you add example? – Maetschl Apr 19 '20 at 15:16

1 Answers1

0

You can make a circular shape using range.upperLimit as a radius an add to scene.

func drawCircularRangeBy(range: SKRange) {
    let radius = range.upperLimit
    let node = SKShapeNode(circleOfRadius: radius)
    node.strokeColor = .white
    addChild(node)
}

Take a look of this example: https://github.com/Maetschl/SpriteKitExamples/blob/master/CircularRange/CircularRange/GameScene.swift

If you really need dots instead of a line please see this answer: Drawing dashed line in Sprite Kit using SKShapeNode

enter image description here

Maetschl
  • 1,330
  • 14
  • 23