Jump to content

how to add a group in constructor ?


Recommended Posts


i'm searching to add my sprite in a group directly in my constructor function, but that don't work.

Have you some advice ?

Thanks in advance.


var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:preload, create: create });

function preload() {
   game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png');
   game.load.image('rect', 'http://s10.postimg.org/6fa6mgrd5/motif.jpg');
//E for external module
var E = E || {}
MonsterBunny = function (game, x, y, rotateSpeed,group) {

    Phaser.Sprite.call(this, game, x, y, 'rect');
    this.rotateSpeed = rotateSpeed;


MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);
MonsterBunny.prototype.constructor = MonsterBunny;

MonsterBunny.prototype.update = function() {

    this.angle += this.rotateSpeed;


E=E || {}

function create() {
    var thisgroup = game.add.group();
    var wabbit = new MonsterBunny(game,100,100,2,thisgroup)


Link to comment
Share on other sites

I think the problem is that you are adding the wabbit to game after you try to add it to the group

either add it to the group after game.add.existing(wabbit);


or move the game.add inside the constructor.

Not sure if there's another way to do it but that's all I can see wrong.

Link to comment
Share on other sites

normally we can draw a sprite directly in a group, so i imagine it's possible to include that in a constructor ? No ?

    // You can directly create a sprite and add it to a group
    // using just one line.
    friendAndFoe.create(200, 240, 'ufo');


Link to comment
Share on other sites


  • Recently Browsing   0 members

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