我是javascript的新手,是的,这是我的作业(我很抱歉)。
TL; DR VERSION:对象具有对象数组和对象数组,第一个数组在画布中绘制,第二个数组不是。 (我是C#程序员,它不是缺少一个循环)
完整版:太阳能系统。对象太阳系仅包含行星阵列
function SolarSystem() { this.Planets = new Array(); }
Object Planet有一些属性并且拥有" moons的数组,"这也是Planet对象:
function Planet(name, timeOfOrbit, radiusOfOrbit, radiusOfPlanet, spinningSpeed, texture) {
this.name = name;
this.spinningSpeed = spinningSpeed;
this.timeOfOrbit = timeOfOrbit;
this.radiusOfPlanet = km2au(radiusOfPlanet);
this.radiusOfOrbit = radiusOfOrbit;
this.texture = texture;
this.moons = new Array();
//Private variable created in constructor, accessible through getter
var angularVelocity = (2 * Math.PI) / timeOfOrbit;
var THREEObject = new THREE.Mesh(new THREE.SphereGeometry(this.radiusOfPlanet * 200, 32, 32), texture);
this.getTHREEObject = function () {
return THREEObject;
}
this.getTHREEObject().position.x = this.radiusOfOrbit;
//TO DO: calculate positions of moons from planet.moons
this.move = function () {
var theta = angularVelocity * dayOfAnimation;
var x = this.radiusOfOrbit * Math.cos(theta);
var y = this.radiusOfOrbit * Math.sin(theta);
this.getTHREEObject().position.x = x
this.getTHREEObject().position.y = y;
if (spinningSpeed != -1) this.getTHREEObject().rotation.z = dayOfAnimation * (Math.PI * 2) / spinningSpeed;
this.moons.forEach(function (moon, index, array) {
var moonTheta = moon.angularVelocity * dayOfAnimation;
array[index].getTHREEObject().position.x = x + moon.radiusOfOrbit * Math.cos(moonTheta);
array[index].getTHREEObject().position.y = y + moon.radiusOfOrbit * Math.sin(moonTheta);
});
}
}
这是我添加星球的方式:
var Mars = new Planet("Mars", 687, 1.381 * 2, 3541 * 10, 24, materials.Mars());
var Phobos = new Planet("Phobos", 0.319, km2au(9376), 27 * 10, -1, materials.Moon());
var Deimos = new Planet("Deimos", 1.263, km2au(23463), 15 * 10, -1, materials.Moon());
Mars.moons.push(Phobos);
Mars.moons.push(Deimos);
solarSystem.Planets.push(Mars);
这是我将可绘制对象添加到场景的方式:
solarSystem.Planets.forEach(function (planet) {
obj.add(planet.getTHREEObject());
planet.moons.forEach(function (moon) {
obj.add(moon.getTHREEObject());
});
});
这是我运行计时器的方式:
window.setInterval(function () {
solarSystem.Planets.forEach(function (planet)
{
planet.move();
});
dayOfAnimation++;
}, intervalConstant);
我想问题是我以前常常使用引用传递的参数,而JS在某种程度上......对我来说很奇怪......为什么我不能引用我称之为foreach的对象(planet.Move => foreach(月亮)不能回叫星球?)?你能否(/ * FU **** G GOD D ** N IT * /)请指出解决方案/向我展示如何解决这个问题的正确方法?
P.s。:我已经阅读了一些关于在JS中传递参数的文章,但说实话......我不太确定我理解......这该死的东西:D
P.s.s。:我甚至尝试创建物体月亮,它保持(不)引用父行星。好吧,也不行。