Trouble with getData/setData

Hi everyone,

I have been experimenting with using Phaser 3 as a game engine and I wanted to turn off an emitter that is following a sprite if that sprite touches the ground.

I was trying to use setData to store the emitter in question but when I try to retrieve it in the collision function i get undefined.

I also can’t get this demo to work… it’s supposed to increment when you click right?
https://labs.phaser.io/edit.html?src=src/components\data\data%20values.js

I thought maybe you can’t store the emitter directly so I tried instead keeping an emitter ID and tracking them myself… but I can’t even retrieve an ID number.

sprite creation:
this.meteors = this.physics.add.group();

        let meteor = this.meteors.create(x, 16, 'meteor');
        meteor.setData('isMeteor', true); // Tag meteors
        id = this.currentMeteorId++;
        console.log(`Meteor created with id: ${id}`);
        meteor.setData('meteorId', id);

This is my collision function:

meteorTouch(meteor, platform) {
    let id = meteor.getData('meteorId');
    let emitter = this.meteorEmitters.get(id);

    if (emitter) {
        console.log(`Stopping the smoke for meteor ID ${id}`);
        emitter.stop();
        emitter.destroy();
        this.meteorEmitters.delete(id);
    } else {
        console.log(`No smoke emitter found for meteor ID ${id}`); // this prints undefined when it collides
    }
}

Any suggestions? Is it because i’m using a group?

You can store the emitter directly.

You may have the arguments reversed in the collision callback.

Wow, thanks for the help. I can’t believe I didn’t try that. :joy:

That was indeed the issue.