我正在使用p5.js制作游戏,希望将每个组件的图像合并以获取游戏的背景,但使用createGraphics无法实现。以下是代码:
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;
}
编辑
在阅读我的代码数小时后,我终于理解了发生了什么:
- 我有一个包含所有组件定义(包括URL img)的JSON。
- 我将此JSON发送到负责组件渲染的类中。
- 我在预加载中调用构造函数,但是组件图像不会加载,除非我执行以下操作:
this.componetsJSON = loadJSON(componetsJSON, () => this.components = this.createComponets());
在加载JSON后,createComponents方法会加载不同的图像。
this.components
和this.boxsize
吗?另外你是怎么调用这个方法的? - Jacob Stuligrossimage()
函数,这会在p5默认的画布上进行渲染:相反,你应该将其渲染到你的p5.Graphics
实例中:gameImg.image(...)
。Paul的详细示例演示了这一点(+1)。 - George Profenza