Sign in to follow this  
Titus

attach an emitter as child of sprite

Recommended Posts

Hi Guys,

I'm having some issues with an emitter that I am trying to attach to a sprite using the addChild method. The emitter should be a child of the player's arm so that it moves and rotates correctly. When I add the emitter to the game without being added as a child it works fine. As soon as I add it as a child to the arm it disappears from the game world. I've played around with a bunch of different coordinates  in case it was being moved slightly off screen but I've come to the conclusion that something is going wrong here. I've also run tests in a new state with just a single sprite and an emitter and as soon as I add the emitter asChild of the sprite it is gone. I'm probably missing the thing that is going wrong here, so any help would be appreciated. I'll put my code snippet below if that helps at all

 

create: function() {

	
		this.game.stage.backgroundColor = "#000";

		

		this.playerContainer = this.game.add.sprite(300, 600, null);
		this.player = this.game.add.sprite(0, 0, 'player');	
		this.player.scale.setTo(0.75);
		this.backArm = this.game.add.sprite(-10, -180, 'backArm');	
		this.backArm.scale.setTo(0.75);	
		this.frontArm = this.game.add.sprite(-10, -180, 'frontArm');
		this.frontArm.scale.setTo(0.75);

		this.emitter = this.game.add.emitter(0, 0, 5000);
		this.emitter.makeParticles('whiteParticle'); 
		this.emitter.minParticleSpeed.setTo(200, 0);
        this.emitter.maxParticleSpeed.setTo(2000, 0);
        this.emitter.gravity = 200;
        this.emitter.lifespan = 0;

		
		this.playerContainer.addChild(this.backArm);
		this.playerContainer.addChild(this.player);	
		this.playerContainer.addChild(this.frontArm);		

		this.frontArm.addChild(this.emitter);	
  	

		this.game.physics.arcade.enable(this.playerContainer);
		this.game.physics.arcade.enable(this.player);
		
		this.playerContainer.body.collideWorldBounds = true;
	
		this.player.anchor.setTo(0.5);
		this.frontArm.anchor.setTo(0.4866, 0.2925);	
		this.backArm.anchor.setTo(0.4866, 0.2925);	
		

	},

 

Share this post


Link to post
Share on other sites

Usually you don't want to give an emitter a parent because the parent's movement would also move the particles. If you do

game.debug.spriteCoords(this.emitter.children[0]);

you may be able to find where it went.

Try instead something like

function update () {
    this.emitter.emitX = this.frontArm.world.x;
    this.emitter.emitY = this.frontArm.world.y;
}

 

Share this post


Link to post
Share on other sites

@samme Thanks you. That was very helpful. I had tried using the update function to attach the emitter to another sprite but I was using this.emitter.x instead of this.emitter.emitX and it didn't work.

Interestingly when debugging the emitter (I had to use children[1] to get any debug info) I get coordinates of the first particle when it is not a child. As soon as I add it as a child of a sprite there is no debug information. Do you think there could be a bug in the current version of phaser?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.