A Behavior System plugin for Phaser games.

Article by Richard Davey. Posted on 24th May 2016.   @phaser_

image

This is the start of a Behavior System plugin for Phaser. It allows you to define Actors and Behaviors in your game logic. See the small example below:

// initialize the plugin
var behaviorPlugin = game.plugins.add(BehaviorPlugin)
/*
 * or (if Phaser is a global)
 * var behaviorPlugin = game.plugins.add(Phaser.Plugin.Behavior)
 */

// a dummy
var myObject = game.create.sprite(0, 0, 'dummy')

// add the behavior system to any object (not just sprites)
behaviorPlugin.enable(myObject)


// all behavior instances must have a identifier/key. This id just need to be unique.
var key = 'my awesome key' 

// You can to override the default options a behavior instance, if you want/need (optional)
var customOptions = { key: 'another value' }

// add a behavior
myObject.behaviors.set(key, Behavior, customOptions) // `.create` is called

// checks if a object has a behavior instance
// returns true if the object has a behavior with the key
myObject.behaviors.has(key)

Find out more and get the source on its GitHub page.

Read More