Understanding this code

I’m going through a tutorial but I don’t fully understand this piece of code. The method makes a group and stores some properties but I’m not really sure about how it iterates, especially the ‘child =>’ part.

Thanks
Steve

createStars()
{
const stars = this.physics.add.group({
key: STARS_KEY,
repeat: 11,
setXY: {x: 12, y: 0, stepX: 70}
})

    stars.children.iterate((child) => {
        child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8))
    })

    return stars
}
1 Like

Hi,
Iterate is like a js forEach.
And the callback (child) => { … } is an anonymous arrow function. It’s like function(child){ … }

I wasn’t familiar with arrow functions but now I am. I put this code in which produced the same result. Thanks for your help.

stars.children.iterate(function(child){
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8))
})