When I add graphics to a scene with this.add.graphics inside of the create function, it works fine, graphics are added. When I try the same code inside of a custom function, say a function called redrawGame(), I get an error message saying:
Undefined is not an object
How can add graphics to a scene outside of the create function?
Depening on your code, you probably will have to pass the scene ( in this case this) to the custom function, since the this object in the custom function referes to a different object.
// if you want to call it from the "create" function
function create(){
redraw(this);
...
}
...
// Or if you want to call it from the "update" function
function update(){
redraw(this);
...
}
...
function redraw(scene){
scene.add.graphics();
...
}
But without seeing your code, I can't say, if this is your issue, or if there is a different problem.
Update, for custom function call on window resize-event :
To check for the resize Event of the browser, I would use the phaser events (link to documentation:
// in the create function
this.scale.on('resize', resizeGame, this);
the third parameter is the scope*(=scene)*, so your resizeGame function would have to call redraw, like this:
function resizeGame(){
...
redraw(this); // due to the scope passed third parameter of 'this.scale.on'
...
}