此代码使返回的Ball保持未定义状态

时间:2019-02-11 01:18:13

标签: javascript class new-operator p5.js

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(){
    }
  }
}

1 个答案:

答案 0 :(得分:3)

据我所知,您的Ball类在您的draw()函数的内部 内。您可能希望它在外面。像这样:

let ball;

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

function draw(){
  background(0);
}

class Ball{
  constructor(){
  }
}

请注意,适当的缩进可以帮助您发现这样的错误。