2015-04-28 97 views
2

我的帆布游戏存在问题。我试图让它跳跃,但我有一些麻烦。一切工作正常,但如果我从底部触摸一个物体,它会将我抛到物体上。Javascript帆布游戏

问题可能与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() 
} 
+0

你不像除非你提供更多关于你已经尝试过的事情以及错误的信息,否则你会得到更多的风格挑剔。 – JoshWillik

回答

-1

我不知道很多关于帆布的权利,但我注意到,你没有任何分号结束你的语句...

例如:

var x = this.x; 
etc. 

另一个东西,我注意到...分数检查都大于或等于和小于或等于200 ...

if (score >= 200 && score <= 200) 
{ 
    ... 
} 
+1

JS中的分号是可选的;有一些少数情况下不插入它们会导致问题,但我在这里没有看到它们中的任何一个。 – Retsam

+0

在实践中,最好做可选的东西,只是为了确保...即使在Swift(IOS)中,你也可以不用,但你仍然应该添加它们。 – DUUUDE123

+0

有没有什么办法可以测试这个来看看游戏中的错误? – DUUUDE123