Javascript画布游戏

时间:2015-04-28 08:33:14

标签: javascript html canvas

我的画布游戏有问题。我试着让它跳,但我有一些麻烦。一切都工作正常,但如果我从底部触摸一个物体,它会把我扔到物体上。

LastColison可能存在问题。有人能帮我吗 ? GIF Image link

function Block(x, y) {
  var size = 80
  GameObject.call(this, x*size, y*size, size)
  this.img = document.getElementById("block")
  this.class = "block"
}

// Dedi vlastnosti z GameObject
Block.prototype = Object.create(GameObject.prototype)

Block.prototype.draw = function() {
  ctx.fillStyle = "green"
  ctx.drawImage(this.img,this.x,this.y,this.size,this.size)
}

function Player(x, y) {
  var size = 120
  this.dx = Math.random() * 50 - 25
  this.dy = Math.random() * 50 - 25
  GameObject.call(this, x*size, y*size, size)
  // this.player = document.getElementById("player")
  this.img = [document.getElementById("player"),document.getElementById("ball_r"),document.getElementById("ball_l"),document.getElementById("ball_j")]
  this.player = this.img[0]
  this.class = "player"

}

// Dedi vlastnosti z GameObject
Player.prototype = Object.create(GameObject.prototype)

Player.prototype.move = function() {
    var x = this.x
    var y = this.y
    //ak dam this.y = y -5 môžem  pohnuť  aj  so  stlačenou  sipkou  dole 
   this.player = this.img[0]
    // Posun
      if ( keys[37] ) 
      {
          if(level == 0){
              x -= 4;
          }
          if (level == 1) {
              x -= 8;
          }
          this.player = this.img[2];
          this.y = y;
      }

      if ( keys[39]) 
      {
          if (level == 0) {
              x += 4;
          }
          if (level == 1) {
              x += 8;
          }
          this.player = this.img[1];
      }
      if ( keys[38] ) 
      { this.player = this.img[3], this.dy = -10;  }



// ak  nedam  else  if  mozem  ouzzivat naraz viac  tlacidiel  takze  upravit  potom

    // Test novej pozicie

    var collision = false

    for (i in scene) {
     var obj = scene[i]
     if (obj.class == "cloud") { continue; }
     if (obj.class == "ladder") { continue; }

     if (obj.class == "touched") { continue; }
     if (obj.class == "dirt") { this.x = x; this.y = y }
     if (obj.class == "block") { this.x = x; this.y = y }
     if (obj.class == "enemy") { this.x = x; this.y = y}
      var test = x +30 >= obj.x + obj.size  || x + this.size  - 40<= obj.x /* kolide right*/|| y >= obj.y + obj.size /*kolizia up*/|| y + 40 + this.size <= obj.y /*kolizia  bottom*/


          if (!test) {

            collision = true;
            var touch = 0;
            if (obj.class == "enemy") {
                touch = 1;
                if (touch == 1) {

                    health -= 20; console.log(health);
                    this.x = x - 250;
                    if (klik % 2 == 0){
                    var hit = new Audio('snd/Hit_Hurt15.wav')
                    hit.play()
                }
                }
                if (health == 0) { health = 0; console.log("GAMEOVER");scene = []}
            }
            if (obj.class == "coin") {
                score += 10; obj.class = "touched";
                if (klik % 2 == 0) {
                    var hrahra = new Audio('snd/pickcoin.wav')
                    hrahra.play()
                }
            }
            else { touch = 0; }
            if (obj.class == "touched") {}
            break;
          }
    }

    if (score >= 200 && score <= 200) {
        if (klik % 2 == 0) {
            var levelup = new Audio('snd/Powerup9.wav')
            levelup.loop = false;
            levelup.play()
        }
        level = 1;
        health = 100;
        score += 1;
    }



        // Ladder
     // if(collision){score += 1,scene.pop() }
    // Posun bez kolizie
    if (!collision) {
      this.x = x
      this.y = y + this.dy  
      this.dy += 0.3;



    }    

    **else {

        if (obj.class == this.LastColl) {
            this.dy = 0;
            this.y = obj.y -160
        }

    this.dy = 0;
    this.LastColl = obj.class
    }** 
}

Player.prototype.draw = function() {
  ctx.fillStyle = "blue"
  ctx.beginPath()
  ctx.drawImage(this.player,this.x, this.y, 110,160)
  ctx.shadowColor = "rgba( 0, 0, 0, 0.3 )";
  ctx.shadowOffsetX = -10;
  ctx.shadowOffsetY = 0
  ctx.shadowBlur = 3;
  ctx.drawImage(this.player,this.x,this.y,110,160)
  ctx.closePath()
  ctx.fill()
}

1 个答案:

答案 0 :(得分:0)

我目前无法访问您提供的GIF。但是从我的角度来看,这是您的问题:

if (!collision) {
  this.x = x
  this.y = y + this.dy  
  this.dy += 0.3;
}    

**else {

    if (obj.class == this.LastColl) {
        this.dy = 0;
        this.y = obj.y -160
    }

此行-this.y = obj.y -160 看起来您是在告诉它在画布上向上移动-160像素。

这能回答您的问题吗?

也请注意-我建议在每个语句的末尾使用分号。有时不要使用它们,而其他时候则不使用-这会给您带来麻烦,而且是不正确的做法:)