Alternatively, you can use GameplayKit along with SpriteKit. GameplayKit provides standard implementations of common algorithms for games and can be used in conjunction with SpriteKit.
Unfortunately, I am not too familiar with this, so I cannot give you the code. I would suggest taking a look at the Apple GameplayKit programming guide. There are functions that allow you to set the target of an agent (for example, an enemy) to move in a certain place, avoid obstacles, intercept another agent or flee from an agent, etc.
Thus, while your player is moving, some enemies may be programmed to try to catch the player, while other enemies may try to escape from the player. Other enemies can be made for random wandering, etc.
So, GameplayKit can add some pretty powerful functionality to a SpriteKit game. This may mean that you spend some time thinking about the architecture of your game, but in the end it may be worth it if you also use other GameplayKit features.
Johnv source share