0

I am new to GameplayKit as most are given its lifespan, and have been trying to figure out the following..

I have an SKNode *enemy that has already been given quite a bit of work and customization from movement to style, habits, etc. all from SpriteKit. My question is such:

  • How can I "link" my enemy node to a GKAgent2D so that it controls this nodes movements?

I understand that you must create a GKComponent and represent this component with a Sprite Node perhaps with a certain texture of choice. But I wanted to know if there was a way to use the built in "control movement" methods that GameplayKit offers on an already existing node. If the answer is "No" please just say so and explain why perhaps? I appreciate any help. Thanks!

Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42

1 Answers1

1

Is your enemy a GKEntity? If its not I believe you cannot add a GKComponent to it.

If your enemy is a GKEntity than you simply add a GKComponent as an agent. Otherwise you will have to change your enemy class to make it a GKEntity.

The way apple does it in DemoBots is to create a class

class AgentComponent: GKAgent2D { }

(GKagent is a subclass of GKComponent so its like adding a component)

Than in the entities they need the agents they do this

class Enemy: GKEntity ...

let agentComponent = AgentComponent()
    agentComponent.delegate = self
    agentComponent.radius = Float(texture.size().width * 0.3)
    addComponent(agentComponent)

and than set the delegates

agentDidUpdate

angentWillUpdate

These are some good tutorials about this

http://www.raywenderlich.com/119959/gameplaykit-tutorial-entity-component-system-agents-goals-behaviors

http://code.tutsplus.com/tutorials/an-introduction-to-gameplaykit-part-1--cms-24483

crashoverride777
  • 10,581
  • 2
  • 32
  • 56