Jump to content

How to check all item in group, not only last


Bobcp
 Share

Recommended Posts

Hi, i try to create script which destroy all item in group, who leaves of screen

	if	(snowball.x<-200){				snowball.destroy();				console.log('KILL');			}		else{				snowball.body.velocity.x=-500;				snowball.angle -= 3;			}

It work, but when i create another snowball in group, x position check only in last item

snowball = snowballGroup.create(1000,this.game.world.height-100-heightGround, '/snowball.png');

How to check position of all item in group?

 

Thank you

Link to comment
Share on other sites

I'm not sure what your question is. Do you have a loop somewhere that is going through all the group's children?

 

I have a code where i create a group, and start events loop.

this.snowballGroup = this.game.add.group();this.snowballGroup.enableBody = true;this.snowballGenerator = this.game.time.events.loop(Phaser.Timer.SECOND * 3, snowballCreate, this); 
function snowballCreate() {  	this.snowball = this.snowballGroup.create(1000,this.game.world.height-100-heightGround, '/snowball.png');	this.snowball.body.gravity.y = 2000;   	this.snowball.body.bounce.y = 0.1;	this.snowball.anchor.setTo(0.5, 0.5);	}

 I need a script where the children are destroy if they go over the screen.

if(snowball.x<-200){	snowball.destroy();	console.log('KILL');}else{	snowball.body.velocity.x=-500;	snowball.angle -= 3;}

My script does not work, it only removes the first children.

Link to comment
Share on other sites

I try this

this.snowballGroup.forEach(function(snowball) {	if(this.snowball.x<-200){		this.snowball.destroy();		this.snowballGroup.remove(this.snowball);		console.log('KILL');	}	else{		this.snowball.body.velocity.x=-500;		this.snowball.angle -= 3;	}},this);

but when i create a new child, check from snowball.x begins with new child, and stops with previous

Link to comment
Share on other sites

I solved the problem with this code

snowballGroup.forEachAlive(function(snowball) {				if(snowball.x<-200){		snowball.destroy();								console.log('KILL');	}	else{		snowball.body.velocity.x=-500;		snowball.angle -= 3;	}});

forEachAlive find all children and check it.

 

Thank you, all

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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