此代码不断回球没有定义

问题描述 投票:-1回答:1
let ball;

// declared 

function setup(){

  createCanvas(500, 500); 

  ball = new  Ball ();
  // this is the problem keeps saying ball is not defined
  // i have defined it
  // can anyone point out the mistake
}
function draw(){
  background(0);
  class Ball{
    constructor(){
    }
  }
}
javascript class new-operator p5.js
1个回答
3
投票

从我可以告诉你的Ball类是你的draw()函数中。你可能希望它是外面。事情是这样的:

let ball;

function setup(){
  createCanvas(500, 500);
  ball = new  Ball ();
}

function draw(){
  background(0);
}

class Ball{
  constructor(){
  }
}

需要注意的是正确的缩进可以帮助你发现这样的错误。

© www.soinside.com 2019 - 2024. All rights reserved.