2017-07-11 8 views
-1

私のコードは、これまでのところ、私のヘビは食べ物にあり、食べ物が新しい場所を見つける必要がありますし、食べ物をすることになっている毎回、蛇や食品で、空白のキャンバスを作成することができるようになっています'グリッド'上のヘビと揃えられるが、今はそうではない。 floorp5.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); 
    } 
    } 
} 
+0

'Math.floor'はあなたが –

+0

なぜdownvotesが何をしたいのですか? – Presisor

+1

@Presisor私はあなたをdownvoteしていないが、あなたはおそらくカップルの理由のためにdownvotedなっている:まず、あなたは、単一のエラーのためにコードのトンを掲載しました。可能な限り数行のコードで問題を示す[mcve]を投稿するようにしてください。それでも、エラーを自分で確認するためにコピーして貼り付けることができる完全な例です。第二に、あなたは[tag:javascript]でこれをタグ付けしました。これはあなたが[tag:p5.js]について聞いたことがないJavaScript開発者を引きつけていることを意味します。彼らには、 'Math.floor()'関数を探していると思っているので、単純なタイプミスのように見えます。 –

答えて

2

他の回答ではJavaScript Math.floor()機能を使用している:

は、ここに私のコードです。 P5.js floor()関数を使用しようとしています。

あなたはsetup()関数が呼び出された後までP5.js機能を使用することはできません。つまり、次のようなことをしたいとします。

var columns; 
var rows; 

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); 

    columns = floor(width/scl); 
    rows = floor(height/scl); 

} 

さらに詳しい情報はthe referenceにあります。

(免責事項:。。。P5.js floor()機能は舞台裏Math.floor()機能を使用して、ほぼ間違いなくしかし、いつでもすることができます一般的に、あなたは、彼らがそこにいる理由ですP5.js機能を使用する必要があります)

また、あなたがwidthheight変数に関連した同様の理由setup()機能でこれを行う必要があることに注意してください:彼らはちょうどcreateCanvas()が呼び出された後まで、デフォルト値を持っています。

も参照してください:Why can't I assign variables using p5 functions and variables before setup()?

あなたがフォローアップの質問がある場合は、独自の質問の記事でそれらを投稿してください。スケッチ全体を転記するのではなく、MCVEに問題を絞り込んでください。がんばろう。

+0

p5.jsが大量のグローバルをグローバルな環境にダンプすると言っていますか?ブリーチ。 –

+1

@ T.J.Crowderデフォルトでは、うまくいっているものは地球環境の中に入っています。彼らはこの "グローバルモード"と呼ぶ。変数が変数の中に入る「インスタンスモード」でも実行できます:https://github.com/processing/p5.js/wiki/Global-and-instance-mode –

+1

@ T.J.Crowder FAQへのリンクを追加しました。 ( 'Math.floor()'のものとよく似ています。私はコピー・ペーストを責めます:p) –

関連する問題