Jump to content

Adding Sprite to a Group makes it disappear?


mrvinegar
 Share

Recommended Posts

Hi, I'm trying to add sprites to a group, but when I do the sprite disappears off screen...

 

I'm doing this...where am I going wrong?

	var firstGroup;        function create () 		{					firstGroup = game.add.group();						game1 = game.add.sprite(200, 0, 'g1');			game1.inputEnabled=true;			game1.events.onInputDown.add(listener1,this);			firstGroup.add(game1);
Link to comment
Share on other sites

Do any of the Group examples work for you? (there are loads of them, just pick a couple and see). I can't see anything obviously wrong with your code at all, which makes me think it's something else going on (that perhaps isn't shown in the code above).

Link to comment
Share on other sites

I'm having the same issue.  If I remove the line to add the sprite to the group the sprite shows on screen as it should.

 

curiously this also fails to show anything on screen:

	var firstGroup;        function create () 		{					firstGroup = game.add.group();						game1 = firstGroup.create(200, 0, 'g1');			game1.inputEnabled=true;			game1.events.onInputDown.add(listener1,this);
Link to comment
Share on other sites

Do any of the Group examples work for you? (there are loads of them, just pick a couple and see). I can't see anything obviously wrong with your code at all, which makes me think it's something else going on (that perhaps isn't shown in the code above).

 

The group examples do work yes...curious

 

Here is my full create

function create () 		{			//removing a preloader			el.style.display = 'none';						 //  Here we'll create a new Group			menuGroup = game.add.group();                        var bcgmain = game.add.sprite(game.world.centerX, game.world.centerY, 'bcg');                        bcgmain.anchor.setTo(0.5, 0.5);						game1 = game.add.sprite(200, 0, 'g1');			game1.inputEnabled=true;			game1.events.onInputDown.add(listener1,this);			menuGroup.add(game1);						game2 = game.add.sprite(230, 150, 'g2');			game2.inputEnabled=true;			game2.events.onInputDown.add(listener2,this);			//menuGroup.add(game2);						game3 = game.add.sprite(260, 300, 'g3');			game3.inputEnabled=true;			game3.events.onInputDown.add(listener3,this);			//menuGroup.add(game3);						game4 = game.add.sprite(290, 450, 'g4');			game4.inputEnabled=true;			game4.events.onInputDown.add(listener4,this);									audioMenu = game.add.audio('audioMenu');			audioMenu.play('',0,1,true);						audioMenuId = game.add.audio('audioMenuId');			audioMenuId.play();						audioMenuSelect = game.add.audio('audioMenuSelect');			        }
Link to comment
Share on other sites

How large is that sprite? The anchor shouldn't have any bearing on if a Sprite appears in a Group or not (you'll find most of them in the Groups Examples have anchors)

 

I fixed it by adding that sprite to the group, so this works

var bcgmain = game.add.sprite(0, 0, 'bcg');menuGroup.add(bcgmain);
Link to comment
Share on other sites

I understand the issue now!  It had to do with the background image being on top of the group.  I was creating the group (but not creating the sprites or populating it) until later.  Creating the sprites alone was fine as they were created above the group and background, but once I added them to the group their ordering changed to that of the group which was under the background and hidden from view as the background covers the whole game screen.  Creating the group above the background fixes the issue for me.

 

Thanks for the help!

Link to comment
Share on other sites

  • 1 month later...

I have this setup:

State
-UIGroup extends Phaser.Group
--MenuGroup extends Phaser.Group

On UIGroup I create a new MenuGroup and I need to pass a Phaser Game instance.  If I say:  new MenuGroup(this.game, this)  I would expect UIGroup to be the parent of MenuGroup.

But:

var menu: MenuGroup = new MenuGroup(this.game, this);
console.log(menu.group)  //null

So I am not sure what my menu is parented to.  If I say this.remove(this.menu)  I get an infinite loop. 

Any ideas?
 

Link to comment
Share on other sites

  • 3 months later...

I understand the issue now!  It had to do with the background image being on top of the group.  I was creating the group (but not creating the sprites or populating it) until later.  Creating the sprites alone was fine as they were created above the group and background, but once I added them to the group their ordering changed to that of the group which was under the background and hidden from view as the background covers the whole game screen.  Creating the group above the background fixes the issue for me.

 

Thanks for the help!

 

Thanks for this. It makes sense that things are layered that way. I was having the same problem. Sometimes things are just "too simple."

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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