'e 未定义'调用 image() 时出错

发布于 2025-01-11 22:50:04 字数 1236 浏览 0 评论 0原文

当我从 p5.js 库调用 image() 时,它给出了类型错误“e is undefined”,尽管给了它图像。我对 javascript 相当陌生,对 p5.js 也完全陌生。我的想法是让以下图像从随机的 x 和 y 坐标落在屏幕上。

var yVal; 
var accel; 
var velocity; 
var mass; 
var pieces = []

function preload () {
   images = ["/images/L.png", "/images/LL.png", "/images/T.png"]
   img_amount = getRandomInt(21);
   for (var i = 1; i <= img_amount; i++) {
       var img = loadImage(images[getRandomInt(3)]);
       pieces[i-1] = img;
   }
}

function setup() {
 createCanvas(window.innerWidth, 700);

 yVal = [];  
 velocity = []; 
 mass = 10; 
 accel = mass * 0.1; 

 for (var i = 0; i <= img_amount; i++) {
   var randX = getRandomInt(window.innerWidth);
   var randY = getRandomInt(700);
   image(pieces[i], randX, randY, mass, mass);
 }
}

function draw() {
 background(25);
 fill(255,0,0);

  for (var i = 0; i <= img_amount; i++) {
    velocity[i] += accel;
    yVal[i] += velocity[i]; 
    image(pieces[i], pieces[i].xvalue, yVal[i], mass, mass);
    if (yVal[i] > height - mass/2) {
        // A little dampening when hitting the bottom
        velocity[i] *= -0.6;
        yVal[i] = height - mass/2;
    }
  }
}


function getRandomInt(max) {
    return Math.floor(Math.random() * max);
}

When I call image() from p5.js library, it gives me the type error 'e is undefined', despite giving it the image. I am fairly new to javascript, and completely new to p5.js. The idea is that I would get the following images to fall on the screen from a random x and y coordinate.

var yVal; 
var accel; 
var velocity; 
var mass; 
var pieces = []

function preload () {
   images = ["/images/L.png", "/images/LL.png", "/images/T.png"]
   img_amount = getRandomInt(21);
   for (var i = 1; i <= img_amount; i++) {
       var img = loadImage(images[getRandomInt(3)]);
       pieces[i-1] = img;
   }
}

function setup() {
 createCanvas(window.innerWidth, 700);

 yVal = [];  
 velocity = []; 
 mass = 10; 
 accel = mass * 0.1; 

 for (var i = 0; i <= img_amount; i++) {
   var randX = getRandomInt(window.innerWidth);
   var randY = getRandomInt(700);
   image(pieces[i], randX, randY, mass, mass);
 }
}

function draw() {
 background(25);
 fill(255,0,0);

  for (var i = 0; i <= img_amount; i++) {
    velocity[i] += accel;
    yVal[i] += velocity[i]; 
    image(pieces[i], pieces[i].xvalue, yVal[i], mass, mass);
    if (yVal[i] > height - mass/2) {
        // A little dampening when hitting the bottom
        velocity[i] *= -0.6;
        yVal[i] = height - mass/2;
    }
  }
}


function getRandomInt(max) {
    return Math.floor(Math.random() * max);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文