this.game.renderer.snapshotArea callback lose context?

I am using this.game.renderer.snapshotArea to get an area of the game screen. I pass the context “this” to have it inside the function callback, but it does not work:

this.game.renderer.snapshotArea(xt, yt, 10*tam, 10*tam, function(image){
            if (this.textures.exists('area')){
                this.textures.remove('area');
            }
            this.textures.addImage('area', image);                
            for(var yy=0; yy<10; yy++){                                                            
                for(var xx=0; xx<10; xx++){ 
                    console.log(this.textures.getPixel(xx*tam, yy*tam, 'area'));
                }
            }
        }, this);

I can solve it using a var to get the context before the callback, but I think it is better to fix it.
(I can not explain better, sorry for my English)

I think there’s no context parameter: snapshotArea().

1 Like

I have seen this url https://rexrainbow.github.io/phaser3-rex-notes/docs/site/snapshot/ and there appears image

Sorry, I have mistaken, this is the correct capture
image

https://rexrainbow.github.io/phaser3-rex-notes/docs/site/snapshot/
And I have forgotten to thank you for the quick reply…

The type parameter controls the format of the created image. snapshotArea can’t set the context for you. You have to store a reference to this, bind the function before passing it as a callback, or use an arrow function.

1 Like