2014-11-24 70 views
0

即时通讯设法做一个simpel比赛,我可以在其他巴尔斯球投篮,然后他们消失。我已经设法使动画工作,我可以拍摄球,但我不知道如何使他们碰撞。我如何获得两个画布动画碰撞?

我试图在72-74行做一些事情,但我得到错误“无法读取属性'未定义的”。

看到试玩游戏点击链接DEMO

var canvas = document.getElementById("canvas"); 
    ctx = canvas.getContext("2d"); 
    var tileldig = Math.floor((Math.random() * 300) + 1); 

    var kuler = [ 
     {r: 10, x: canvas.width/2, y: canvas.height-100, f: "red", dy:0}, 
     //{r: 50, x: tileldig, y: 50, vx:0 , vy: 3, f: "green"}, 
    ] 



    var fiender = [ 
     {r: 10, x: tileldig, y: 50, vx:0 , vy: 1, }, 
    ] 
    var skudder = [ 
     {r: 10, x:0+kuler.x, y: 0+kuler.y, f: "black"}, 
    ] 




    function spill() { 

     ctx.clearRect(0, 0, canvas.width, canvas.height); 

     for (var i = 0; i < kuler.length; i++) { 
      kuler[i].x += 0; 
      kuler[i].y += kuler[i].dy; 


      ctx.fillStyle = kuler[i].f; 
      ctx.beginPath(); 
      ctx.arc(kuler[i].x, kuler[i].y, kuler[i].r, 2*Math.PI, 0); 
      ctx.closePath(); 
      ctx.fill(); 


      if (kuler[0].x >= canvas.width-kuler[0].r) { 
       kuler[0].x = canvas.width-kuler[0].r 
      }; 
      if (kuler[0].x <= 0+kuler[0].r) { 
       kuler[0].x = 0+kuler[0].r 
      }; 
      if (kuler[0].y >= canvas.height-kuler[0].r) { 
       kuler[0].y = canvas.height-kuler[0].r 
      }; 
      if (kuler[0].y <= 0+kuler[0].r) { 
       kuler[0].y = 0+kuler[0].r 
      }; 


     }; 


     document.onkeydown = function tast (e) {    
      switch (e.keyCode) { 
      case 37: 
       kuler[0].x -= 10; 
       break; 
      case 39: 
       kuler[0].x += 10; 
       break; 
      case 38: 
       kuler[0].y -= 10; 
       break; 
      case 40: 
       kuler[0].y += 10; 
       break; 
      case 32: 
       newskudd() 
       console.log("hit space") 
       if(fiender[i].y >= skudder[1].y){ 
       alert(); 
       }; 
       break; 
      } 
     }; 

     for (var i = 0; i < fiender.length; i++) { 
      ctx.fillStyle = "blue"; 
      ctx.beginPath(); 
      ctx.arc(fiender[i].x, fiender[i].y, fiender[i].r, 2*Math.PI, 0); 
      ctx.closePath(); 
      ctx.fill(); 

      fiender[i].y += fiender[i].vy; 

      if (fiender[i].y >= canvas.height) { 
       fiender.splice(i,1); 
       console.log("ute"); 
      }; 
     } 



     requestAnimationFrame(spill); 
    } 

    function newskudd() { 
     var nyttskudd = 
     {x:kuler[0].x, y:kuler[0].y, r:5, dy:-5, f:"black"}; 
     kuler.push(nyttskudd); 
    }; 

    setInterval(function(){ 
     fiender.push({r: 10, x: Math.floor((Math.random() * 300) + 1), y: 0, vx:0 , vy: 1, f: "green"}); 
    }, 1000); 

    spill(); 


    /*if (circles.x >= canvas.height- circles.r){ 
     circles.splice(i,1); 
    }*/ 
+1

我建议你用一点点时间来提高编码风格。此外,非英文函数和变量不是要走的路:) – Nenotlep 2014-11-24 12:51:14

回答

0

UPDATE:CODE sample as I promised

对不起,我不得不重写了很多你的代码,使事情对我来说更干净。我试图保留你的原名。

线72-74不大的地方,在那里你可以计算出碰撞;)

我会尽量给你一些提示。

删除这部分,你不需要它。

console.log("hit space") 
if(fiender[i].y >= skudder[1].y){ 
    alert(); 
}; 

每次你拍,你,这已经是你的函数newskudd()做“kulers的阵列”添加新的子弹。

我想你不想在空间被击中时杀死你的敌人,但是当一个球遇到另一个球时。

所以在spill()去thrue所有“子弹”,并试图找出,如果任何敌人被击中。 ==>每重绘你的程序将测试,如果有什么被击中

JS我作出更新

// constants 

var TILELDIG = Math.floor((Math.random() * 300) + 1); 
var NEW_ENEMY_INTERVAL = 1000; 

var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 


// basic graphic constructs (prototypes) 

var EntityBall = function (position, color, delta, speed, size) { 
    this.size = size || 10; 
    this.position = position || [0, 0]; 
    this.delta = delta || [0, 0]; 
    this.speed = speed || [0, 0]; 
    this.color = color || "black"; 
}; 


var Kuler = function (position) { 
    EntityBall.call(this, position, "black", [0, -10], [0, 0], 5); 
}; 


var Fiender = function (position) { 
    EntityBall.call(this, position, "blue", [0, 1]); 
}; 


var Skudder = function (position) { 
    EntityBall.call(this, position, "red", [0, 0], [5, 5]); 
} 


// Program constructor 

var Program = function (ctx, canvas) { 
    this.ctx = ctx; 
    this.canvas = canvas; 
    this.init(); 
}; 

// Program inicialization 
Program.prototype.init = function() { 
    // these arrays store living things 
    this.kulers = []; 
    this.fienders = []; 
    this.skudders = []; 
    this.hordeUpdate = null; 

    var player1 = new Skudder([canvas.width*0.5, canvas.height*0.75 ]); 
    this.skudders.push(player1); 
    // here you bind keys for moving main ball (player1) 
    document.addEventListener("keydown", this.player1Actions.bind(this, player1)); 
}; 

// handle player moves 
Program.prototype.player1Actions = function (player1, e) { 
    switch (e.keyCode) { 
     case 37: 
      player1.position[0] -= player1.speed[0]; 
      break; 
     case 39: 
      player1.position[0] += player1.speed[0]; 
      break; 
     case 38: 
      player1.position[1] -= player1.speed[1]; 
      break; 
     case 40: 
      player1.position[1] += player1.speed[1]; 
      break; 
     case 32: 
      this.attack(player1); 
      console.log("hit space"); 
      break; 
    } 
    if(player1.position[0] < 0) { 
     player1.position[0] = 0; 
    } 
    if(player1.position[0] > canvas.width) { 
     player1.position[0] = canvas.width; 
    } 
    if(player1.position[1] < 0) { 
     player1.position[1] = 0; 
    } 
    if(player1.position[1] > canvas.height) { 
     player1.position[1] = canvas.height; 
    } 
}; 

// only spawns bullet, but doesnt calculate collision 
Program.prototype.attack = function (player) { 
    var kulerPosition = [player.position[0], player.position[1]]; 
    var kuler = new Kuler(kulerPosition); 
    this.kulers.push(kuler); 
}; 

// main Program thread (refreshed 60 times per second) 
Program.prototype.refresh = function() { 
    var canvas = this.canvas; 
    this.ctx.clearRect(0, 0, canvas.width, canvas.height); 

    // update all positions (delta move of each entity) 
    var list = [this.kulers, this.fienders, this.skudders]; 
    for (var i = 0; i < list.length; i++) { 
     for (var j = 0; j < list[i].length; j++) { 
      // find new position 
      list[i][j].position[0] += list[i][j].delta[0]; 
      list[i][j].position[1] += list[i][j].delta[1]; 

      // delete entity if it is out of canvas (else all browser memory will be eaten by blue balls) 
      if (list[i][j].position[1] > canvas.height || list[i][j].position[1] < 0 || list[i][j].position[0] > canvas.width || list[i][j].position[0] < 0) { 
       list[i].splice(j, 1); // this delete it 
      }; 
     } 
    } 

    // calculate bullets collision 
    for (var i = 0; i < this.kulers.length; i++) { // go thrue all bullets 
     for (var j = 0; j < this.fienders.length; j++) { // for each bullet go thrue all enemies 
      // find if this enemy is being hit 
      // (5 + 2.5), 5 is r of blue ball, 2.5 is r of black ball, you dont want to find perfect hit, scratches counts too 
      if ((this.kulers[i].position[0] >= this.fienders[j].position[0] - (5 + 2.5) && this.kulers[i].position[0] <= this.fienders[j].position[0] + (5 + 2.5)) && this.kulers[i].position[1] <= this.fienders[j].position[1]) { 
       this.kulers.splice(i, 1); // delete bullet that hits 
       this.fienders.splice(j, 1); // delete dead enemy 
       break; 
      } 
     } 
    } 

    // after all removes, draw all living entities 
    for (var i = 0; i < list.length; i++) { 
     for (var j = 0; j < list[i].length; j++) { 
      this.ctx.fillStyle = list[i][j].color; 
      ctx.beginPath(); 
      ctx.arc(list[i][j].position[0], list[i][j].position[1], list[i][j].size, 2 * Math.PI, 0); 
      ctx.closePath(); 
      ctx.fill(); 
     } 
    } 

    // repeat 
    requestAnimationFrame(this.refresh.bind(this)); 
}; 

// this will start blue enemies coming 
Program.prototype.hordeComing = function() { 
    this.hordeUpdate = setInterval(this.addEnemy.bind(this), NEW_ENEMY_INTERVAL); 
}; 

// this spawn enemy 
Program.prototype.addEnemy = function() { 
    var position = [Math.floor(Math.random() * canvas.width/2) * 2, 0]; 
    var fiender = new Fiender(position); 
    this.fienders.push(fiender); 
}; 


// run program with params 

var program1 = new Program(ctx, canvas); 
program1.refresh(); 
program1.hordeComing(); 
+0

我该怎么做? – morten 2014-11-24 13:00:18

+0

我可以尝试做出简短的解决方案,但我现在必须马上行动:(我会在2小时内回来... – 2014-11-24 13:06:14

+0

总之,你需要2个阵列。一排黑色子弹和敌人阵列。每一个drawcall,你做两个forloops像:'for(var i = 0; i = fiender [j]的.Y){// 除去的Kuler [i]和fiender [ j] } }; }' 溢出()函数是您drawcall – 2014-11-24 13:09:16

1

这就是问题之行:

if(fiender[i].y >= skudder[1].y){ 

您是这里的循环之外,如此fiender[i]是没有意义的。最快的解决方法是使用for循环遍历所有fiender项目,就像之后做5-6行。就像这样:

for (var i = 0; i < fiender.length; i++) { 
    if(fiender[i].y >= skudder[1].y){ 
     alert(); 
    }; 
} 

此外,skudder[1]似乎并不存在,也许它应该是skudder[0]

您需要提供更多信息才能获得更准确的答案。

+0

是的,“如果”是问题 – morten 2014-11-24 12:45:05

+0

我给了你一个总体思路如何解决它,但我不知道那是什么代码......我假设射手是敌人,而手杖是子弹,对吧? – Shomz 2014-11-24 12:48:26

+0

是的,我想让它像蜜蜂一样,在那里你拍摄astroids – morten 2014-11-24 12:58:04