2014-12-07 75 views
-2

我正在创建一个互动游戏,并且对编程非常新颖。我想知道如何增加一个变量然后把它放到一个警告框中。我想找到foodCaught和food的计算方法,并在游戏结束时将它显示在一个警戒框中。另外我该如何做到这一点,所以它是在一个计时器上,并在这个计时器的结束时,与foodCaught和foodWasted警报框出现?如何在警报中创建计算?

$(document).ready(function(){ 
    var cnv = $("#myCanvas")[0]; 
    var ctx = cnv.getContext("2d"); 
    var catcherX = ctx.canvas.width/2; 
    var catcherY = ctx.canvas.height - 100; // set the initial location of the catcher's y position 
    var numFoods = 5; 
    var catcherSpeed = 30; 
    var foodCaught = 0; 
    var foodWasted = 0; 

    function Food(){ // the name of the constructor usually begins with a captial letter 
     this.radius = 30; 

     this.x = Math.floor(Math.random()*ctx.canvas.width); 
     this.y = 0 - this.radius; 
     this.speed = 1+ Math.floor(Math.random()*5); 
     var imageToUse = new Image(); 
     this.width = 50; // default values 
     this.height = 50; // default values 

     var randomNum = Math.floor(Math.random()*2); // create a random number to choose the image 
     if(randomNum == 0){ 

      imageToUse.src = "corn.png"; 
      this.width = 27; // width of corn.png 
      this.height = 100; // height of corn.png 

     } else if(randomNum == 1){ 
      imageToUse.src = "straw.png" 
      this.width = 83; // width of straw.png 
      this.height = 100; // height of straw.png 
     } 

     this.moveFood = function(){ 

      if(this.y > ctx.canvas.height){ 
       this.x = Math.floor(Math.random()*ctx.canvas.width); 
       this.y = 0; 
       footWasted += 1; 
      } 

      this.y += this.speed; // add speed to location 
     } 

     this.drawFood = function() { 
      ctx.drawImage(imageToUse, this.x, this.y); 
     } 

     this.intersectFood = function(targetX, targetY, targetR) { 

      if(this.x + this.width > targetX && this.x < targetX + targetR && this.y + this.height > targetY && this.y < targetY + targetR){ 
       foodCaught += 1; 
       return true; 
      } 

      /* 
      var distanceX = this.x - targetX; 
      var distanceY = this.y - targetY; 
      var distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY); 

      if(distance < targetR + this.radius){ 
       return true; 
      } 
      */ 
     } 
    } 
    // create an Array of Foods 
    var FoodArray = new Array(); 
    for(var i=0; i<numFoods; i++) { 
     FoodArray[i] = new Food(); 
    } 

    // get mouse Postion 

    $(document).keydown(function(e){ // attach the event to the entire document 

     switch(e.keyCode){ 
      case 37: // left 
       catcherX-= catcherSpeed; 
       break; 
      case 39: // right 
       catcherX+= catcherSpeed; 
       break; 
     } 

    }); 

    var interval = setInterval(gameLoop,10); // call draw every 10 milliseconds 
    var counter = 0; 

    function gameLoop(){ 
     ctx.clearRect(0,0, ctx.canvas.width, ctx.canvas.height); //clears previous circles 
     for(var i=0; i < FoodArray.length; i++) { 
      FoodArray[i].moveFood(); 
      FoodArray[i].drawFood(); 
      if(FoodArray[i].intersectFood(catcherX, catcherY, 15)){ 
       FoodArray.splice(i,1); 
       console.log(i); 
      } 
     } 

     // draw catcher 
     ctx.beginPath(); 
     ctx.fillStyle="#119933"; 
     ctx.arc(catcherX,catcherY,15,0,Math.PI*2,true); 
     ctx.closePath(); 
     ctx.fill(); 
+1

使用在javascript取向而不的基本变量存储和警报知识对象A新手。这很讽刺。 – 2014-12-07 05:49:19

+0

*“我正在创建”* - 我发现不可能相信您编写了上述代码,但需要帮助增加一个变量。显示的代码已经有很多增量变量的例子。它也有一个计时器,但为了提醒你需要'setTimeout'而不是'setInterval'。 – nnnnnn 2014-12-07 05:54:20

+0

请在下面找到我的答案,并将其标记为接受,如果它解决您的问题。 – 2014-12-07 12:10:51

回答

1

更新回答你的游戏已经结束

后,执行:

alert("foodCaught = " + foodCaught + "<br>foodWasted = " + foodWasted); 

只要确保这个alert是withing foodCaughtfoodWasted变量的作用域。

编辑:

你的游戏实际上是永无止境的。 setInterval,你每10毫秒一直呼叫gameLoop(),无限!

为了保持foodCaughtfoodWasted变量的轨道,在gameLoop()函数的末尾添加console.log()声明如下图所示的代码:

function gameLoop(){ 
    ctx.clearRect(0,0, ctx.canvas.width, ctx.canvas.height); //clears previous circles 
    for(var i=0; i < FoodArray.length; i++) { 
     FoodArray[i].moveFood(); 
     FoodArray[i].drawFood(); 
     if(FoodArray[i].intersectFood(catcherX, catcherY, 15)){ 
      FoodArray.splice(i,1); 
      //console.log(i); 
     } 
    } 

    // draw catcher 
    ctx.beginPath(); 
    ctx.fillStyle="#119933"; 
    ctx.arc(catcherX,catcherY,15,0,Math.PI*2,true); 
    ctx.closePath(); 
    ctx.fill(); 

    console.log("foodCaught = " + foodCaught + "\nfoodWasted = " + foodWasted); 

} 

有了这个,你将得到的foodCaughtfoodWasted值变量每次调用gameLoop函数。

如果您不知道如何使用控制台,请检查this post

EDIT2:

我对结束这场游戏的想法。在下面的更新代码中,我使用setTimeout()将游戏设置为运行10秒(仅作为示例,以便您可以快速更改)。您已将其更改为任何您想要的时间。在setTimeout()中,我也提醒你需要的值。

这里是更新的Javascript/jQuery代码:

$(document).ready(function(){ 
    var cnv = $("#myCanvas")[0]; 
    var ctx = cnv.getContext("2d"); 
    var catcherX = ctx.canvas.width/2; 
    var catcherY = ctx.canvas.height - 100; // set the initial location of the catcher's y position 
    var numFoods = 5; 
    var catcherSpeed = 30; 
    var foodCaught = 0; 
    var foodWasted = 0; 



    function Food(){ // the name of the constructor usually begins with a captial letter 
     this.radius = 30; 

     this.x = Math.floor(Math.random()*ctx.canvas.width); 
     this.y = 0 - this.radius; 
     this.speed = 1+ Math.floor(Math.random()*5); 
     var imageToUse = new Image(); 
     this.width = 50; // default values 
     this.height = 50; // default values 

     var randomNum = Math.floor(Math.random()*2); // create a random number to choose the image 
     if(randomNum == 0){ 

      imageToUse.src = "corn.png"; 
      this.width = 27; // width of corn.png 
      this.height = 100; // height of corn.png 

     } else if(randomNum == 1){ 
      imageToUse.src = "straw.png" 
      this.width = 83; // width of straw.png 
      this.height = 100; // height of straw.png 
     } 




     this.moveFood = function(){ 

      if(this.y > ctx.canvas.height){ 
       this.x = Math.floor(Math.random()*ctx.canvas.width); 
       this.y = 0; 
       foodWasted += 1; 
      } 

      this.y += this.speed; // add speed to location 

     } 

     this.drawFood = function() { 
      ctx.drawImage(imageToUse, this.x, this.y); 
     } 

     this.intersectFood = function(targetX, targetY, targetR) { 

      if(this.x + this.width > targetX && this.x < targetX + targetR && this.y + this.height > targetY && this.y < targetY + targetR){ 
       foodCaught += 1; 
       return true; 
      } 
      /* 

      var distanceX = this.x - targetX; 
      var distanceY = this.y - targetY; 
      var distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY); 

      if(distance < targetR + this.radius){ 
       return true; 
      } 
      */ 

     } 


    } 

    function gameLoop(){ 
     ctx.clearRect(0,0, ctx.canvas.width, ctx.canvas.height); //clears previous circles 
     for(var i=0; i < FoodArray.length; i++) { 
      FoodArray[i].moveFood(); 
      FoodArray[i].drawFood(); 
      if(FoodArray[i].intersectFood(catcherX, catcherY, 15)){ 
       FoodArray.splice(i,1); 
      } 
     } 

     // draw catcher 
     ctx.beginPath(); 
     ctx.fillStyle="#119933"; 
     ctx.arc(catcherX,catcherY,15,0,Math.PI*2,true); 
     ctx.closePath(); 
     ctx.fill(); 

    } 

    $(document).keydown(function(e){ // attach the event to the entire document 

     switch(e.keyCode){ 
      case 37: // left 
       catcherX-= catcherSpeed; 
       break; 
      case 39: // right 
       catcherX+= catcherSpeed; 
       break; 
     } 

    }); 


    // create an Array of Foods 
    var FoodArray = new Array(); 
    for(var i=0; i<numFoods; i++) { 
     FoodArray[i] = new Food(); 
    }  

    var interval = setInterval(gameLoop,10); // call draw every 10 milliseconds 

    setTimeout(function(){ 
     clearInterval(interval);   
     alert("Time UP!\n\nfoodCaught = " + foodCaught + "\nfoodWasted = " + foodWasted); 
    }, 10000); 

}); 
+0

请勿将其放入文档中。警报应该在游戏结束时显示,而不是在页面加载后五秒钟。 – nnnnnn 2014-12-07 05:52:04

+0

@nnnnnn谢谢,但变量'foodCaught'和'foodWasted'在'$(document).ready()'中。 – 2014-12-07 05:59:17

+0

@Helen回答更新。理想情况下,如果你将这个'alert'放在这些变量的范围内,你的代码就不会中断。我不知道你的游戏在哪里完全结束,所以请在适当的地方添加'alert'。或者你可以在jsFiddle演示中重现你的问题,如果你愿意,我可以进一步帮助你。 – 2014-12-07 06:21:44