Declaration

Overview

Polygon obstacles serve two purposes in GameplayKit: You can use polygon obstacles to construct a navigability graph of your game world (a GKObstacleGraph object) for use in pathfinding. You can also use polygon obstacles to define regions for agents (GKAgent objects) to avoid, using the GKGoal method init(toAvoid:maxPredictionTime:).

To easily create obstacles for use with a SpriteKit game, create and arrange a set of nodes that define the non-navigable regions of your game world. You can create such nodes programmatically, or use the SpriteKit Scene Editor in Xcode. If you’re already using nodes with physics bodies to keep sprites from entering those regions, you can reuse those nodes. Then, use the obstacles(fromNodeBounds:), obstacles(fromSpriteTextures:accuracy:), or obstacles(fromNodePhysicsBodies:) method to generate a set of GKPolygonObstacle objects.