Adding class instances to a group.

Recommended Posts

So I have a class that extends from Sprite, that works fine. I would like to be able to add multiple instances of this class to a group.


What I am currently doing is giving me some errors that I can't figure out.

// Creating instances of this class works fine.Goblin = function (game, x, y) {, game, x, y, 'goblin');    game.add.existing(this);    game.physics.enable(this, Phaser.Physics.ARCADE);};Goblin.prototype = Object.create(Phaser.Sprite.prototype);Goblin.prototype.constructor = Goblin;Goblin.prototype.update = function () {    // Goblin functionality code};// Trying to create a group that will hold a bunch of Goblins.GoblinSpawner = function (game, x, y, amount) {, game);    this.classType = Goblin;    for (var i = 0; i < amount; i+=1) {        var gob = this.create(game, x, y);    }    // Can't get this.createMultiple() to work like that either, to avoid the for loop.};GoblinSpawner.prototype = Object.create(Phaser.Group.prototype);GoblinSpawner.prototype.constructor = GoblinSpawner;GoblinSpawner.prototype.update = function () {    // Spawner functionality code};

I found the docs to be vague when it comes to what I should do to add my own classes to groups.


How should I be doing this?

Share this post

Link to post
Share on other sites

group.create is used to add sprite with a frame name. To add an extended sprite, you need to use group.add and manually add the sprite.

var gob = new Goblin(game, x, y);this.add(gob);

Also you shouldn't add the sprite in the constructor so remove this line:


Share this post

Link to post
Share on other sites

Thanks that works.


I still don't understand why the group.create() description says


"You can change Group.classType to any object and this call will create an object of that type instead, but it should extend either Sprite or Image."


Or how this can be used. Is this just a remnant of an older version of Phaser?

Share this post

Link to post
Share on other sites

I have the very same question and couldn't find an answer so far: group.createMultiple(20, 'spriteImageReference') adds 20 sprites by specifying the sprite key, but how to addMultiple or createMultiple custom Phaser.Sprite instances? 

Let's say i created var spider = new Spider(game, 100, 50); and I want to add twenty of this spider, can i do it with addMultiple or createMultiple, without manually iterating for loops? 

The classType description suggests this, but it is very vague and I haven't found any example in the group examples. 

Share this post

Link to post
Share on other sites

Create group, then set the classType member of that group to the constructor function of your sprite, then call createMultiple:

function MyAwesomeSprite(game, x, y) {, game, x, y, 'playerKey');

// All that custom sprite class stuff...

var myGroup =;
myGroup.classType = MyAwesomeSprite;
// myGroup now has 20 instances of MyAwesomeSprite.


Share this post

Link to post
Share on other sites

Hi everyone.

I am newbie. I has a question with using function.

When i use it, i must add my sprite to a group to make the sprite appear.

While if i use game.add.sprite, the sprite appears without group.

What is difference?

Please help me clear my mind. 

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.