Moving SKSpritenode in the direction of another SKSpriteNode (Magnet)

My goal: To make SKSpriteNode move to another SKSpriteNodes position. (To create a magnet for my sprite game)

Hi, I am trying to get several SKSpriteNodes with the same name in order to move and collide with a node sprite, which they will call it a magnet. I just don’t understand where and how I will do it. I tried using this code, but I left scratches on what to do:
func update(_ currentTime: TimeInterval) {

   let node = childNode(withName: "specialObstacle")

        let magnetLocation = magnet.position
        let specialObjectLocation = node?.position

        let x = node?.position.x - magnetLocation.x
        let y = node?.position.y - magnetLocation.y

}

Note. My arrangement of magnets will change due to the fact that the user will move the magnet around the screen and would like him to continue moving towards the magnet, but I just can’t figure out how to do this.

EDIT 1

, , , , x, , , y - , , , .

vortex

- , :

 let playerLocation:CGPoint = self.convert(thePlayer.position, from: worldNode)

        let location = playerLocation
        let nodeSpeed:CGFloat = 3.5

        worldNode.enumerateChildNodes(withName: "levelUnit"){
            node, stop in

            let levelUnit:LevelUnit = node as! LevelUnit  //cast as an actual LevelUnit class


            levelUnit.enumerateChildNodes(withName: "specialObstacle"){
                node, stop in

                //Aim
                let dx = location.x - (node.position.x)
                let dy = location.y - (node.position.y)
                let angle = atan2(dy, dx)

                node.zRotation = angle

                //Seek
                let vx = cos(angle) * nodeSpeed
                let vy = sin(angle) * nodeSpeed

                node.position.x += vx
                node.position.y += vy

            }
        }
+4
1

- ?

enter image description here

import SpriteKit

class GameScene: SKScene {

    let magnet = SKFieldNode.linearGravityField(withVector: vector_float3(0, 9.8 * 2, 0))
    let circle = SKShapeNode(circleOfRadius: 30)
    override func didMove(to view: SKView) {
        circle.fillColor = .white
        circle.position.x = 0
        circle.position.y = frame.maxY
        addChild(circle)

        magnet.region = SKRegion(size: self.frame.size)
        magnet.isEnabled = false
        magnet.categoryBitMask = 0x1            
        circle.addChild(magnet)

        self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

        for i in 0..<20 {
            let ball = SKShapeNode(circleOfRadius: 30)
            ball.fillColor = .yellow
            ball.physicsBody = SKPhysicsBody(circleOfRadius: 30)
            ball.position.x = CGFloat(i) * 10
            ball.physicsBody!.fieldBitMask = 0x1
            addChild(ball)
        }

        let ball = SKShapeNode(circleOfRadius: 30)
        ball.fillColor = .green
        ball.physicsBody = SKPhysicsBody(circleOfRadius: 30)
        ball.position.y = 40
        ball.physicsBody!.fieldBitMask = 0x2
        addChild(ball)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        circle.fillColor = .red
        magnet.isEnabled = true
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        circle.fillColor = .white
        magnet.isEnabled = false
    }
}

?

  • SKFieldNode, categoryBitMask - 0x1
  • fieldBitMask = 0x1 ( FieldNode)
  • fieldBitMask = 0x2 ( SKFieldNode)

, / SKFieldNode, / .

, SKFieldNode, .

+3

Source: https://habr.com/ru/post/1661125/


All Articles