espace Posted October 10, 2016 Share Posted October 10, 2016 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 More sharing options...
squilibob Posted October 10, 2016 Share Posted October 10, 2016 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. samme 1 Link to comment Share on other sites More sharing options...
espace Posted October 10, 2016 Author Share Posted October 10, 2016 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 More sharing options...
espace Posted October 10, 2016 Author Share Posted October 10, 2016 finded http://phaser.io/examples/v2/groups/extending-a-group Link to comment Share on other sites More sharing options...
espace Posted October 10, 2016 Author Share Posted October 10, 2016 i start a another post about that because my next question is different. Link to comment Share on other sites More sharing options...
samme Posted October 10, 2016 Share Posted October 10, 2016 game.add.existing adds an object to the World (removing it from your group). You don't need it; the sprite is already in-world since its parent is in-world. Link to comment Share on other sites More sharing options...
Recommended Posts