函数在JavaScript

时间:2016-05-30 04:26:51

标签: javascript

我试图用JavaScript编写游戏,它有一些简单的汽车物理特性来确定RPM,扭矩,汽车的速度等等。我有一些功能可以将RPM / hp值转换成不同的单位,当RPM超过某一点时,齿轮会增加。然而,由于某种原因,当汽车通过它应该在最后档位上升的RPM时,功能开始返回NaN并且物体从画布上消失。可能是什么问题呢?这是代码:

function gameObject(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y; 
    this.weight = 2500;
    this.health = 100;
    this.hp = 276;
    this.accelTime = 1;
    this.gearRatios = [1.98, 1.64, 1.4, 1.23, 1.04];
    this.gear = 1;
    this.RPM = 0;
    this.shiftUpRPM = 7000;
    this.shiftDownRPM = 1100;
    ctx = game.canvas.getContext('2d');
    ctx.fillStyle = color;
    ctx.fillRect(this.x, this.y, this.width, this.height);
    this.update = function() {
      if (this.RPM >= this.shiftUpRPM && this.gear < 5) {
        this.RPM = 1400;
        this.gear += 1;
      } else if (this.RPM <= this.shiftDownRPM && this.gear > 1) {
        this.RPM = 5200;
        this.gear -= 1;
      } 

      ctx = game.context;
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
      this.x += this.speedX;
      this.y += this.speedY;
    };
}

var torqueToNewton = function(torque) {
  return torque * 1.36;
}


var calculateTorqueOutput = function(hp, RPM) {
  console.log("cTO:" + (hp * 5252) / RPM)
  return (hp * 5252) / RPM;
}

var velocity = function(kn_energy, mass) {
  var v = Math.sqrt(2*kn_energy/mass);
  return v;
}

var detectCollision = function(object1, object2) {
  object1.left = object1.x;
  object1.right = object1.x + object1.width;
  object1.bottom = object1.y
  object1.top = object1.y + object1.height;
  object2.left = object2.x;
  object2.right = object2.x + object2.width;
  object2.bottom = object2.y
  object2.top = object2.y + object2.height;
}

var updateGame = function() {
    game.clear();
    if (window.key && window.key == 83) {
      yourObject.speedX = -1; 
    }
    if (window.key && window.key == 87) {
        yourObject.RPM += 1;
        nm_s = torqueToNewton(calculateTorqueOutput(yourObject.hp, yourObject.RPM));
        yourObject.speedX += velocity(nm_s, yourObject.weight)/10;
        yourObject.RPM /= (1/yourObject.gearRatios[yourObject.gear]);
        console.log(yourObject.speedX + ":" + yourObject.RPM + ":" + yourObject.gear);

    }                              
    else {
        if (yourObject.RPM > 0 && yourObject.speedX > 0) {
        yourObject.RPM -= yourObject.speedX * (.25 / yourObject.speedX);
        yourObject.speedX -= .25;
        console.log(yourObject.speedX + ":" + yourObject.RPM + ":" + yourObject.gear);
        } else {
          yourObject.RPM = 0;
          yourObject.speedX = 0;
        }
    }
    if (window.key && window.key == 38) {yourObject.speedY = -1; }
    if (window.key && window.key == 39) {yourObject.speedY = 1; }
    yourObject.newPos();
    yourObject.update();

1 个答案:

答案 0 :(得分:1)

虽然我没有检查你的所有代码,但肯定有一个错误:

 && this.gear < 5

这将允许档位达到5,而您定义的最后一个档位为4(您总共有5个档位,并且由于索引为零,最后一个不是5,而是4)。

要修复它,请将其更改为

 && this.gear < 4