Jump to content

Extending Phaser.Group and overriding Update function


eralpkaraduman
 Share

Recommended Posts

Hi,

 

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) {    Phaser.Group.call(this,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 :)
 

Thanks.

 

Link to comment
Share on other sites

You can call parent method like this:

Phaser.Group.prototype.update.call(this);

or

Phaser.Group.prototype.update.apply(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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...