在 Phaser 3 中设置快照的位置

Set position of snapshot in Phaser 3

我正在使用 this Phaser 3 example,我需要在预定义的位置显示快照。目前每个快照都显示在正文下方 document.body.appendChild(image);

我试过 this.add.sprite(0, 0, image).setOrigin(0.5,0); 但是 returns 出错了:

Uncaught TypeError: Cannot read property 'add' of null

代码:

  this.input.on('pointerdown', function (pointer) {

    var textureManager = this.textures;

    this.game.renderer.snapshotArea(pointer.x, pointer.y, 128, 128, function (image)
    {
        document.body.appendChild(image);

        if (textureManager.exists('area'))
        {
            textureManager.remove('area');
        }

        textureManager.addImage('area', image);

        particles.setTexture('area');
    });

}, this);

我在正文末尾留下了附加图像的部分,但您可以根据需要随意编辑。

var snapshot
var lastpointer = {x: 100, y: 100} 
this.input.on('pointerdown', (pointer) => {
    var textureManager = this.textures;
    this.game.renderer.snapshotArea(pointer.x, pointer.y, 128, 128, (image) => {
        lastpointer.x = pointer.x
        lastpointer.y = pointer.y
        document.body.appendChild(image);
       
        if (textureManager.exists('area'))
        {
            textureManager.remove('area');
        }

        textureManager.addImage('area', image);

        particles.setTexture('area');

        // Add the snapshot to the texture cache
        if (textureManager.exists('snapshot'))
        {
            textureManager.remove('snapshot');
            snapshot.destroy();
            snapshot = null;
        }
        textureManager.addBase64('snapshot', image.src);
    });

}, this);

// When the snapshot is actually loaded, add it to the pointer coordinates (or simply fixed ones)
this.textures.on('addtexture', function (texture) {
    if('snapshot' === texture) {
        snapshot = this.add.image(lastpointer.x, lastpointer.y, 'snapshot');
    }
}, this);