Jump to content

how to add a group in constructor ?


espace
 Share

Recommended Posts

hi,

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.

https://jsfiddle.net/espace3d/qb512t7r/

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');
    group.add(this);
    this.anchor.setTo(.5,.5)
    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)
    game.add.existing(wabbit);
    thisgroup.visible=false
}

 

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);

    game.add.existing(wabbit);
    thisgroup.addChild(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

 Share

  • Recently Browsing   0 members

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