Arcanorum Posted October 24, 2014 Share Posted October 24, 2014 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 More sharing options...
eguneys Posted October 25, 2014 Share Posted October 25, 2014 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:game.add.existing(this); Link to comment Share on other sites More sharing options...
Arcanorum Posted October 25, 2014 Author Share Posted October 25, 2014 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? Link to comment Share on other sites More sharing options...
pixelhijack Posted March 31, 2016 Share Posted March 31, 2016 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 More sharing options...
drhayes Posted March 31, 2016 Share Posted March 31, 2016 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.createMultiple(20); // myGroup now has 20 instances of MyAwesomeSprite. Link to comment Share on other sites More sharing options...
pixelhijack Posted April 4, 2016 Share Posted April 4, 2016 thanks drhayes, i wasn't aware of the 'playerKey' param Link to comment Share on other sites More sharing options...
Windy Posted May 20, 2017 Share Posted May 20, 2017 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 More sharing options...
Recommended Posts