Jump to content

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

Link to comment
Share on other sites

  • 1 year later...

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. 

Link to comment
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) {
  Phaser.Sprite.call(this, game, x, y, 'playerKey');

// All that custom sprite class stuff...

var myGroup = game.add.group();
myGroup.classType = MyAwesomeSprite;
// myGroup now has 20 instances of MyAwesomeSprite.


Link to comment
Share on other sites

  • 1 year later...

Hi everyone.

I am newbie. I has a question with using Phaser.Sprite.call 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. 

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.

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.


  • Recently Browsing   0 members

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