javascript - 发生碰撞时如何修复玩家健康的随机减法?

标签 javascript processing collision-detection p5.js

我正在制作一个小型宇宙飞船游戏,其中宇宙飞船必须四处移动才能不被 meteor 击中。当 meteor 击中飞船时,玩家应该会失去生命值,但在我的示例中,玩家总是会失去大约 12 的数字,我不知道是什么原因造成的。

我尝试添加起始生命值而不是减去它撞击 meteor 时添加的生命值,但即便如此生命值还是增加了 10,而不是我想要的增加 1。我尝试使用 console.log 来查找错误,但没有任何效果

let x = 200;
let x1 = 258;
let score = 0;
let health = 5;
let meteors = [];
let ecllipseMeteors = [];
let meteor;
let ecllipseMeteor;
let spaceShipimg;
let meteorimg;

function Meteor() {
  this.x = random(0,600);
  this.y = random(-200,-190);
  this.speed = random(3,10);

  this.fall = function() {
    this.y = this.y + this.speed;
    if (this.y > height) {
      this.y = random(-200,-100);
      this.speed = random(3, 7);
    }
  };
  this.show = function() { image(meteorimg,this.x,this.y, 40, 40) };
}
function meteormodel() {
  this.x = random(0,600);
  this.y = random(-200,-190);
  this.speed = random(3,10);

  this.fall = function() {
    this.y = this.y + this.speed;
    if (this.y > height) {
      this.y = random(-200,-100);
      this.speed = random(3, 7);
    }
  };
  this.show = function() { ellipse(this.x,this.y, 20, 20) };
}

function setup() {
    // creates canvas
  createCanvas(600, 400);
  timer = createP('');
 meteor = new Meteor();
 ecllipseMeteor = new meteormodel();
 interval = setInterval(scoreCount, 500);
}

function gameOver() {
  textSize(20);
  text("GAME OVER YOUR SCORE: " + score, 200, 200);
  fill(255);
}
function preload() {
    //loads all teh necesarry material
    spaceShipimg = loadImage('assets/spaceShip.png');
  meteorimg = loadImage('assets/meteor.png');

}

function scoreCount() {
score++;
}
function draw() {
  background(11, 72, 170);


  hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
  if(hit == true) {
    health -= 1;
  //  console.log(health -= 1);
    if (health == 0) {
      gameOver();
      noLoop();

    }
  }

    if (keyIsDown(LEFT_ARROW) && x > -46) {
      x -= 5;
    }

    if (keyIsDown(RIGHT_ARROW) && x < 508) {
      x += 5;
    }
    if (keyIsDown(LEFT_ARROW) && x1 > 9) {
      x1 -= 5;
    }

    if (keyIsDown(RIGHT_ARROW) && x1 < 565) {
      x1 += 5;
    }
    rect(x1, 335, 20, 30)
    image(spaceShipimg,x,260,120,120)


        meteor.fall();
        meteor.show();




    textSize(20);
    text("Health: " + health, 10, 20);
    fill(255);
    textSize(20);
    text("Score: " + score, 10, 40);
    fill(255);
}

预期的结果是,当一颗 meteor 击中火箭时,玩家的生命值会下降 1。问题是它大约在 10 点左右。

最佳答案

问题是, meteor 不止一次击中船舶矿石。一颗 meteor 在从船顶到船底的过程中不断地撞击着船。
解决这个问题最简单的方法是强制游戏生成新的 meteor 。在方法 fall 中,生成新 meteor 的条件是 this.y > height。如果meteor.yheight+1,那么撞击飞船的 meteor 就会消失,并在窗口顶部设置一个新的位置:

function draw() {
    background(11, 72, 170);

    hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
    if(hit == true) {
        health -= 1;

        meteor.y = height+1; // <----------

        if (health == 0) {
            gameOver();
            noLoop();
        }
    }

    // [...]

}

关于javascript - 发生碰撞时如何修复玩家健康的随机减法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484303/

相关文章:

data-structures - 移动物体的空间数据结构?

IOS Swift Spritekit 碰撞检测

javascript - 在 JavaScript 中搜索字符串内部

javascript - 拆分而不丢失分隔符

javascript - Javascript正则表达式

javascript - Angular 项目构建成功但出现这些错误

java - 如何从处理草图外部调用处理方法?

java - (Java/处理) 如何在屏幕上的不同位置创建一个对象的多个实例?

javascript - Box2d.js 与 Processing.js

java - Libgdx如何存储碰撞检测数据