【问题标题】:My force calculating code won't print to the console我的力计算代码不会打印到控制台
【发布时间】:2020-07-12 16:50:22
【问题描述】:

我在 droidscript 中编写了一些 javascript 代码,但它不起作用。我多次尝试修复它,甚至删除了不相关的代码,但它仍然不会向控制台打印任何内容,这是什么问题?

var Vector = function(x, y) {
  this.x = typeof x == "number" ? x : 0;
  this.y = typeof y == "number" ? y : 0;
  console.log("vector created");

  this.length = function() {
    return Math.sqrt(x * x + y * y);
  };

  this.sub = function(vec) {
    return new Vector(this.x - vec.x, this.y - vec.y);
  };
};

function slope(pos1, pos2) {
  console.log("slope calculated");
  return (pos1.y - pos2.y) / (pos1.x - pos2.x);
}

function calc_vector(f, s) {
  console.log("vector calculated");
  return new Vector(f * Math.cos(s), f * Math.sin(s));
}

function normal_force(forcevec, s) {
  var f = calc_vector(-forcevec.length(), slope(forcevec, new Vector()) - s);
  console.log("normal force calculated: " + f.y);
  return f.y;
}

//Called when application is started.
function OnStart() {
  var force = normal_force(new Vector(0, 1), 45);
  console.log(force);
}


OnStart();

【问题讨论】:

  • 确实确实将内容打印到控制台...
  • 是的——当你运行你发布的 sn-p 时,它会打印出来。如果您正在使用更多您认为可能会造成干扰的代码,请也发布!
  • 你在OnStart() 上得到NaN,因为在slope() 中你被零除,即(pos1.x - pos2.x) === 0。因此,您将 -1Infinity 传递给 calc_vector()
  • 谢谢 Luís,我想我会尝试更改斜率函数以输出角度并在必要时/如果需要进行转换。

标签: javascript simulation droidscript


【解决方案1】:

console.log() 是可以接受的,但在 DroidScript 中,它不显示任何内容。以下代码覆盖控制台对象并为您的 sn-p 创建适当的布局上下文。 选择创建一个“新 JavaScript 应用程序”,在指定位置插入代码,它会立即在 DroidScript 中运行。

if (typeof(app) == "object") 
  var console = new CONSOLE();

// INSERT YOUR CODE HERE \/

console.log("TEST");
console.log("TEST");
console.log("TEST");

// INSERT YOUR CODE HERE /\

function CONSOLE() {
  this.txt = "";
  this.log = function con_log(str) {
    this.txt += (this.txt.length == 0 ? "" : "\r\n") + str;
    this.edt.SetText(this.txt);
  }
  this.create = function() {
    this.lay = app.CreateLayout("linear", "TopCenter,FillXY");
    this.edt = app.CreateTextEdit("", 1, .95, "NoSpell");
    this.edt.SetBackColor("#000000");
    this.edt.SetTextSize(12);
    this.lay.AddChild(this.edt);
    app.AddLayout(this.lay);
    this.edt.SetText("");
  }
  this.create();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多