Extending Phaser.Group and overriding Update function

Recommended Posts



I'm coming from flash/flixel perspective. When I'd need spawn enemies or track some objects and draw something on them repeatedly, I used to create a class instance which contains and watches objects, and mostly use it as kind-of factory pattern as well to generate objects too. I was doing this by extending groups in Flixel. I know groups are pretty similar in Phaser too. But possibly because of my lack of experience on OOP javascript, I haven't found a nice way to extend groups.


Im extending groups this way;

var ElementalSpawner = function(game,squad) {,game);};ElementalSpawner.prototype = Object.create(Phaser.Group.prototype);ElementalSpawner.prototype.constructor = ElementalSpawner;

Until this point everything is great but when I want to override Phaser.Group's update function, I had to do this;

ElementalSpawner.prototype.update = function() {    // Elemental Spawner update code here    // super.update    var i = this.children.length;    while (i--)    {        this.children[i].update();    }};

This works for sure, but I'm not comfortable with it. (So what if I was overriding a 200 line code function :) )


As you see I had to copy original group class's update functions contents into the overridden function. What I intended to do is similar to calling super.update() in AS3/Flash/Flixel.
I'd like to ask for suggestions on how to extend and override functions of javascript/phaser classes? And more generally how would you design such structures like, spawners, healthbar renderers, GUI containers-updaters etc?
I'm working on our "incomplete" LD48 game :)



Share this post

Link to post
Share on other sites

You can call parent method like this:;



You can also pass arguments as a second parameter. call() need them to be listed one by one and apply() allows them to be in a single array.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.