当我以纵向模式播放时,数组返回错误

时间:2019-02-19 08:46:04

标签: javascript flappy-bird-clone

我做了一个飞扬的鸟克隆。当我在正常屏幕模式下播放时,效果很好。 (1920×1080)。但是,当我以potrait模式播放时,我的数组出现“未定义”错误。

由于我使用windowHeight和windowWidth,所以我没有问题。 我在p5框架中做到了。

this.hits = function (bird) {
         if (bird.y < this.top || bird.y > windowHeight - this.bottom) {
             if (bird.x > this.x && bird.x < this.x + this.w) {
                 return true;
             }
         }
         return false;
  }

以下是使用的变量:(不是实际代码)

鸟:

bird.y = windowHeight/2;
bird.x = windowWidth*0.2;

管道:

 var pipeSpace = random(250,600);
    var centerY = random(0,windowHeight/3.5);
    pipes.top = centerY - pipeSpace/2;
    pipes.bottom = windowHeight - centerY - (centerY + pipeSpace/2);
    pipes.x  = windowWidth*1.5; (this is the x  position(starting position) of the pipes)
    this.w = 40; (this is the width of the pipes)

此函数每帧调用一次,以查看是否发生碰撞,但是在我通过第一个“管道”后,游戏中断并显示上述错误。

编辑:错误:pipes[i] is undefined

这是它发生的部分: 实际上有更多的代码,但这与我的问题无关。

var pipes = [];

function setup() {
  bird = new Bird();
  pipes.push(new Pipe());

}
function draw(){
    for (var i = pipes.length-1; i >= 0; i--){
      if(pipes[i].hits(bird)){
          gameOver = true;
        }
    }
}

0 个答案:

没有答案