【发布时间】: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。因此,您将-1和Infinity传递给calc_vector() -
谢谢 Luís,我想我会尝试更改斜率函数以输出角度并在必要时/如果需要进行转换。
标签: javascript simulation droidscript