0

I have parent node that is created as

var rect = SKShapeNode(points: &points, count: points.count)

Initially it has a shape of rectangle (points contain 4 CGPoint) but it can be any polygon.

And I have another node, e.g.

var square = SKShapeNode(rectOf: CGSize(width: 19, height: 19))

That is added as a child of the first one%

rect.addChild(square)

I need to be able to move square left, right, up and down by pressing corresponding buttons. I don't understand how can I move square inside the parent node that it cannot leave parent node boundaries. Once square reaches border of parent shape it should stop. Could someone help me understand how to achieve what I need?

disciple
  • 11
  • 2

1 Answers1

0

You can use SKPhysicsBody

On your rect

rect.physicsBody = SKPhysicsBody(polygonFrom: rect.path)

Set a category bitmap to it and configure the collision.

https://developer.apple.com/documentation/spritekit/skphysicsbody