p5.js 代码在加载 2 个图像时不运行

问题描述 投票:0回答:1

基本上就像它听起来的那样,每当我尝试加载单个图像时它工作得很好,但是当我尝试两个时它给我一个未定义的变量错误。两张图片都在同一个文件夹中并正确上传。

代码(工作):

let img
function preload() {
  img = loadImage('images/mario.png');
}

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);

  //this loads in mario

  image(img, 0, 350, 50, 50)
}

代码(不工作):

let img;
let img2;
function preload() {
  img = loadImage('images/mario.png');
}

function preload() {
  img2 = loadImage('images/level.png');
}

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);

  //this loads in mario
  image(img2, 0, 0, 400, 400)  
  image(img, 0, 350, 50, 50)
}
javascript drawing p5.js
1个回答
0
投票

你只能有一个

preload
回调函数。
prelaod
是一个异步函数,在
setup
之前被调用:

let img;
let img2;
function preload() {
  img = loadImage('images/mario.png');
  img2 = loadImage('images/level.png');
}
© www.soinside.com 2019 - 2024. All rights reserved.