1

I'm trying to make the player sprite node reset when it touched the danger node. How can I do that? That code doesn't work.

if player.position == danger.position {
    player.position = CGPoint(x: 0, y: 550)
} else if player.position == goal.position {
    print("Good Job!") 
}
secon19665
  • 19
  • 1
  • 8

1 Answers1

0

In the easy case if the nodes are in the same coordinate space (ie they have the same parent) CGRect has a method that will tell you if a rect intersects another rect:

if player.frame.intersects(danger.frame) {
    //Do collision stuff here
}

If not use SKNode.convert(from:) to transform one of the position then reconstruct a transformed frame (unlike UIView SKNode can only convert points and not rects).

The physics system can do collisions and contacts for you and is the preferred way of doing this sort of thing, but the above example works if you don't want to opt into physics.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33
  • I am still beyond confused on what to do to make the game reset? Do I add the if statement I had before? Do I just add a function called startGame which contains a CGPoint that reset the position of the player? What do I do?? – secon19665 Dec 06 '18 at 14:39