My code is fine but it doesn't work

let tree = this.physics.add.group()

function generateTree() {
    let coordx = Phaser.Math.Between(40, 260)
    tree.create(coordx, 120, 'tree')
}

this.time.addEvent({delay: 1000, callback: generateTree, loop: true})

let star = this.physics.add.sprite(240, 620, 'star').setScale(0.2)
star.setInteractive()

star.on('pointerup', function() {
this.scene.physics.moveToObject(star, tree, 1000);
})

With the code above, when you touch the star it disappears and does not move towards the tree. Could you help me?

Hi @buqer_ok,
tree is an object of the class Phaser.Physics.Arcade.Group and this class hasn’t the x and y properties required for the second parameter of the method moveToObject().
You have to access to the elements of the physics group using some of the tree methods for this purpose like, for example, getFirst().

Regards.

Could you help me by adding that code and share it here? Sorry for the inconvenience.

Try this:

star.on('pointerup', () => {
    this.scene.physics.moveToObject(star, tree.getFirst(), 1000);
});

It doesn’t work :frowning:

star.on('pointerup', function() {
    this.scene.physics.moveToObject(star, tree.getFirstAlive(), 1000)

I did it!