私のコードは、これまでのところ、私のヘビは食べ物にあり、食べ物が新しい場所を見つける必要がありますし、食べ物をすることになっている毎回、蛇や食品で、空白のキャンバスを作成することができるようになっています'グリッド'上のヘビと揃えられるが、今はそうではない。 floor
がp5.jsによって定義されることになっていることをJavaScriptのp5.js:にReferenceError
ReferenceError: floor is not defined
注:それは、私はこのエラーを得るか、コンソールでも、私の第一の課題となります。これを行うにはどのようにしてエラーを修正する方法を
?
var snake;
var scl = 10;
var food;
var columns = floor(width/scl);
var rows = floor(height/scl);
function setup()
{
//Sets the Canvas
createCanvas(700, 700);
//Creates a new object using the variable snake
snake = new Snake();
food = new Food();
//Sets the frame rate
frameRate(10);
}
function draw()
{
//Sets the Background, number implies the colour
background(45);
//Adds all the values set within the function to the snake
snake.updateSnake();
snake.showSnake();
snake.keyPressed();
food.showFood();
food.updateFood();
if(snake.eatFood(food))
{
food.updateFood();
}
}
function Food()
{
this.x = random(0,700);
this.y = random(0,700);
this.updateFood = function()
{
this.pos = createVector(floor(random(columns)), floor(random(rows)));
this.pos.mult(scl);
}
this.showFood = function()
{
fill(255, 0, 10);
rect(food.x, food.y, scl, scl);
}
}
function Snake()
{
this.x = 0;
this.y = 0;
this.xspeed = 0;
this.yspeed = 0;
this.updateSnake = function()
{
this.x = this.x + this.xspeed * scl;
this.y = this.y + this.yspeed * scl;
this.x = constrain(this.x, 0, width - scl);
this.y = constrain(this.y, 0, height - scl);
}
this.showSnake = function()
{
fill(255);
rect(this.x, this.y, scl, scl);
}
this.direction = function(x, y)
{
this.xspeed = x;
this.yspeed = y;
}
this.eatFood = function(pos)
{
var distance = dist(this.x, this.y, pos.x, pos.y);
if(distance < 1)
{
return true;
console.log("WITHIN RANGE");
}else
{
return false;
console.log("OUTSIDE RANGE");
}
}
this.keyPressed = function()
{
if (keyCode === UP_ARROW)
{
snake.direction(0, -1);
} else if (keyCode === DOWN_ARROW)
{
snake.direction(0, 1);
} else if (keyCode === RIGHT_ARROW)
{
snake.direction(1, 0);
} else if (keyCode === LEFT_ARROW)
{
snake.direction(-1, 0);
}
}
}
'Math.floor'はあなたが –
なぜdownvotesが何をしたいのですか? – Presisor
@Presisor私はあなたをdownvoteしていないが、あなたはおそらくカップルの理由のためにdownvotedなっている:まず、あなたは、単一のエラーのためにコードのトンを掲載しました。可能な限り数行のコードで問題を示す[mcve]を投稿するようにしてください。それでも、エラーを自分で確認するためにコピーして貼り付けることができる完全な例です。第二に、あなたは[tag:javascript]でこれをタグ付けしました。これはあなたが[tag:p5.js]について聞いたことがないJavaScript開発者を引きつけていることを意味します。彼らには、 'Math.floor()'関数を探していると思っているので、単純なタイプミスのように見えます。 –