Итак, я сделал очень простую игру, используя P5. js. Игра работает до тех пор, пока игрок 2 не забьет гол и вся игра не остановится. Там написано, что player2.show не является функцией, но я сделал это как единое целое.
function setup()
{
createCanvas(720, 460);
//this allows the video to capture
video = createCapture(VIDEO);
video.hide();
// video.size(320,240);
video.size(320,240);
//This is the classification
classifyVideo();
function classifyVideo()
{
classifier.classify(video, gotResults);
}
//Getting the classification
function gotResults(error, results)
{
if(error)
{
console.error(error);
return;
}
label = results[0].label;
controlBlock();
classifyVideo();
}
player = new Player();
ball = new Ball();
player2 = new Player2();
for(let y = dSize/2; y<height; y+=dSize*2)
{
for(let y = dSize/2; y<height; y+= dSize*2)
{
dots.push(createVector(width/2 - dSize/2, y));
}
}
}
Отсюда код не будет работать
function draw()
{
ball.edges();
ball.update();
player.update();
player2.update();
ball.scores();
noStroke();
fill(255,20,147);
drawSquares();
ball.show();
player.show();
player2.show(); //this is the code that is not being recognized
drawScores();
}
Это говорит о том, что игрок 2 не является функцией, когда вы можете видеть ниже, что это функция
function Player2()
{
this.w = player.w;
this.h = player.h;
this.pos = createVector(width - this.w*3, height/2 - this.h/2);
this.acc = createVector(0,0);
this.spd = 10;
this.maxSpd = 10;
this.show = function()
{
noStroke();
fill(255);
rect(this.pos.x, this.pos.y, this.w, this.h);
}
this.update = function()
{
let d1 = dist(ball.pos.x, ball.pos.y, this.pos.x, this.pos.y);
let d2 = dist(ball.pos.x, ball.pos.y, this.pos.x, this.pos.y + this.h);
let d = (d1 + d2)/2;
this.pos.add(this.acc);
this.pos.y = constrain(this.pos.y, 0, height - this.h);
if(d < 450)
{
if(ball.pos.y < this.pos.y - this.h/2)
{
this.acc.y -= this.spd;
}
else
{
this.acc.y += this.spd;
}
this.acc.y = constrain(this.acc.y, -this.maxSpd, this.maxSpd);
}
else{
this.acc.y += random(-this.spd*0.9, this.spd);
}
}}
Вот так, я могу использовать функцию player2