phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Incorrect TS Types for EmitterOpOnUpdateCallback

Open Thisura98 opened this issue 2 years ago • 2 comments

Version

  • Phaser Version: 3.55.2
  • Operating system: macOS

  • IDE: Visual Studio Code

  • Browser: Not browser specific

Description

I am a beginner to using Phaser and I am trying to create a custom EmitterOperator (EmitterOp) to change the rotation of emitted particles on each update by using my own onUpdate callback function.

The type of the onUpdate property is as follows in the generated TypeScript Definitions file.

type EmitterOpOnUpdateCallback = (
    particle: Phaser.GameObjects.Particles.Particle, 
    key: string, 
    t: number, 
    value: number
) => void;

According to this, an Update Callback should not return a value, as the return type is void. However, in order for the update of a property to work the new value must be returned. The default value for the onUpdate property is as follows from the JavaScript code.

var EmitterOp = new Class({
    // ...
    defaultUpdate: function (particle, key, t, value)
    {
        return value;
    }
    // ...
})

Example Test Code

In the below example, hovering over the onUpdate property and following the 'Go to Definition' menu item (on VSCode) will eventually land you in the 'phaser.d.ts' file. That is where the incorrect definitions are specified.

// Create the particle
const leaf_1_particles = scene.add.particles('leaf1');

// Create the Emitter
const leaf_1_emitter = leaf_1_particles.createEmitter({
    speed: 50,
    accelerationX: 2,
    accelerationY: 2,
    lifespan: 10000,
    x: 0,
    y: 0,
    radial: false,
    frequency: 3000,
    scale: { start: 0.2, end: 0.3 },
    blendMode: 'NORMAL'
});

// Set top edge of screen as emitting zone
leaf_1_emitter.setEmitZone({
    source: {
        getRandomPoint: (point) => {
            point.x = (Math.round((Math.random() * 100)) / 100) * 800;
            point.y = 0;
        }
    },
    type: 'random'
})

// Custom EmitterOp
let customRotate = new Phaser.GameObjects.Particles.EmitterOp({}, 'rotate', 0, false);
let counter = 0;
customRotate.onUpdate = (p, k, v) => {
//              ^
// Hover mouse over `onUpdate` above
    return counter++;
}

leaf_1_emitter.rotate = customRotate;

Additional Information

This is not a breaking issue. After finding that I need to return a value instead of updating the passed in v value I was able to rotate the angle. However, it would be better if the TypeScript Definitions reflected the actual JavaScript code to be used.

I apologize if this is not the correct place to report this kind of issue.

Thisura98 avatar Aug 29 '21 13:08 Thisura98

Hmm, I've looked at this and the types correctly specify the return value, but the generated defs ignore that. I don't know why, but I suspect this is an issue in the TS defs generator.

photonstorm avatar Sep 28 '21 13:09 photonstorm

#5954 is caused by the same I think.

samme avatar Feb 05 '22 21:02 samme

Sorry, this was fixed a while ago, and I forgot to mention it here.

photonstorm avatar Nov 18 '22 21:11 photonstorm