【问题标题】:Javascript Html5 canvas problemsJavascript Html5画布问题
【发布时间】:2014-04-06 05:56:12
【问题描述】:

这是我正在制作的游戏。我无法弄清楚为什么它不起作用。我这里有 JS 小提琴http://jsfiddle.net/aa68u/4/

    // Create the canvas
 var canvas = document.createElement("canvas");
 var ctx = canvas.getContext("2d");
 canvas.width = 512;
  canvas.height = 480;
 document.body.appendChild(canvas);



 // Background image
 var bgReady = false;
 var bgImage = new Image();
 bgImage.onload = function () {
     bgReady = true;
 };
 bgImage.src = "http://6269-9001.zippykid.netdna-cdn.com/wp-content/uploads/2013/11/Woods-Wallpaper.jpg";

 // Ship image
 var shipReady = false;
 var shipImage = new Image();
 shipImage.onload = function () {
     shipImage = true;
 };
 shipImage.src = "http://s29.postimg.org/3widtojzn/hero.png";

 // Astroid image
 var astroidReady = false;
 var astroidImage = new Image();
 astroidImage.onload = function () {
     astroidReady = true;
 };
 astroidImage.src = "http://s29.postimg.org/4r4xfprub/monster.png";

 // Game objects
 var ship = {
     speed: 256; 
 };
 var astroid = {};


 var health = 100;

 window.keyStates = {}; 

 addEventListener('keydown',  function (e) {
     keyStates[e.keyCode || e.key] = true;
     e.preventDefault();
     e.stopPropagation();
 }, true);

 addEventListener('keyup',  function (e) {
      keyStates[e.keyCode || e.key] = false;
     e.preventDefault();
     e.stopPropagation();
 }, true);



 var reset = function () {

     astroid.width = 10;
     astroid.height = 10;
     astroid.x = 32 + (Math.random() * (canvas.width - 64));
     astroid.y = 32 + (Math.random() * (canvas.height - 64));

     ship.speed = 256;

     for (var p in keyStates) keyStates[p]= false;
 };



 // Update game objects
 function update (modifier) {
     if (keyStates[38] ==true) { // Player holding up
         astroid.x -= ship.speed * modifier;
     }
     if (keyStates[40]==true) { // Player holding down
         astroid.x += ship.speed * modifier;
     }
     if (keyStates[37]==true) { // Player holding left
         astroid.y -= ship.speed * modifier;
     }
     if (keyStates[39]==true) { // Player holding right
         astroid.y += ship.speed * modifier;
     }
     if (astroid.width) < 200{
         astroid.width +=10;
         astroid.height += 10;
     }
     if (astroid.width) > 200{
         reset();
     }
     // Are they touching?
if (keyStates[32] == true && ship.x <= (astroid.x + 32) && astroid.x <= (ship.x + 32) && ship.y <= (astroid.y + 32) && astroid.y <= (ship.y + 32)) {
    monstersCaught += 1;
    reset();
}



 };


 // Draw everything
 var render = function () {
    if (bgReady) {
    ctx.drawImage(bgImage, 0, 0);
}



if (shipReady) {
    ctx.drawImage(heroImage, hero.x, hero.y);
}

if (astroidReady) {
    ctx.drawImage(monsterImage, monster.x, monster.y);
}

// Score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Your Health: " + health, 32, 32);

 };

 // The main game loop
 var main = function () {
var now = Date.now();
var delta = now - then;
if (delta > 20) delta = 20;

update(delta / 1000);
render();

then = now;
 };

 // Let's play this game!
 reset();
 var then = Date.now();
 setInterval(main, 1); // Execute as fast as possible

游戏应该是一艘船(鞋子图像),它会避开变大的小行星(蚂蚁),但是当你移动你的船(鞋子)时,你的船(鞋子)会停留在同一个地方,小行星(蚂蚁)也会移动。蚂蚁/小行星也会变得更大,就像您靠近它们一样。

【问题讨论】:

  • 你的代码有什么问题?
  • 您应该在此处发布相关代码。如果你的链接消失了,这个问题就没用了。
  • 它不起作用怎么办?只是发表评论,因为它不会让您发布带有代码的链接,而不是发布代码。它需要代码是有原因的。
  • 画布不显示@bic
  • 您的问题严重缺乏帮助所需的信息。请修改您的问题。

标签: javascript html canvas html5-canvas


【解决方案1】:
var ship = {
    speed: 256; 
};

去掉值后面的;

if astroid.width < 200{

if astroid.width > 200{

if 条件周围需要括号。

错误控制台很有帮助!但现在它只是陷入了monsterImage is not defined 的无限循环。只需...返回,更仔细地编写代码,然后使用错误控制台!它的存在是有原因的!

【讨论】:

  • 您正在定义astroidImage(拼写错误,顺便说一句)但尝试使用monsterImage
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2011-07-31
  • 2022-01-05
  • 2021-10-13
  • 2011-10-28
  • 2011-11-01
  • 1970-01-01
相关资源
最近更新 更多