I'm making a game on p5.js and I want to Merge the image of each component to get the background of the game, but using createGraphics doesn't work. Here is the code:
createGameMapImg() {
let gameImg = createGraphics(this.width * this.boxSize, this.height * this.boxSize);
gameImg.background(color('white'));
for (let component of this.components) {
image(component.img, component.x * this.boxSize, component.y * this.boxSize, component.width * this.boxSize, component.height * this.boxSize);
if (component.img != undefined) gameImg.image(component.img, component.x * this.boxSize, component.y * this.boxSize, component.width * this.boxSize, component.height * this.boxSize);
}
return gameImg;
}
EDIT
After reading my code for hours I finally understand what was happening:
- I have a JSON with the definitions of all the components (incluiding the URL img)
- I send this JSON to a class that manage the render of componets.
- I call the constructor on the preload, but the components images won't load ulless i do this:
this.componetsJSON = loadJSON(componetsJSON, () => this.components = this.createComponets());
The idea is after load the JSON the method createComponents load the diferent images.