繁体   English   中英

我想将未命名或动态命名的对象(类实例)推送到数组

[英]I want to push unnamed or dynamically named objects (class instances) to an array

背景:“Umo Space”是一款 javascript 游戏,我正在开发 Umo Space是一款 javascript 游戏,我正在开发一款 class “Umo”(通用移动对象,其中包含所需的变量和成员函数)一个 object 在二维空间中移动。 目前游戏世界已经预定义了行星和敌舰,但我正在尝试创建随机系统。 在这样做时,我需要用随机数量的“Umo”实例填充“行星”数组。 我以为我可以将一个未命名的新“Umo”实例 push() 到数组中,当我在随机行星循环之前在系统中心创建第一个 Umo(基本上是太阳)时,我似乎可以。 但是,在循环内部,我做了似乎相同的事情,不久之后,当我尝试使用成员函数或分配给 Umo 实例的成员变量时,我得到一个错误。

未捕获的类型错误:无法在 UmoSpace68.html:591 的 System.randomplanets (UmoSpace68.html:567) 处读取未定义的属性“setorbit”

如果我注释掉该特定行(它使用 Umo 的成员 function),则分配“.name”或“.parentid”成员变量时会出错。 我想也许我需要使用“eval()”来分配 Umo 动态变量名称,而不是未命名的推送它,但这并没有解决任何问题(而且似乎是一种......不受欢迎的做法)。 无论如何,似乎 push() 在循环内失败而没有生成错误消息。

这个问题还以某种方式在下游导致另一个错误,在看似不相关的代码中,未定义我的“ships”数组(不是任何东西的成员变量,只是一个预定义的“Umo”数组)。

UmoSpace68.html:789 Uncaught ReferenceError: Cannot access 'ships' before initialization at update (UmoSpace68.html:789)

如果我注释掉 function 调用“testsystem.randomplanets();” 所有问题都消失了,并且所有 Umo 功能都在其他地方使用而没有问题,因此问题几乎可以肯定是 randomplanets() 成员 function 或至少是“系统” class (尚未有意义地使用)。

这是我能收集到的最小的相关代码片段:

class System{
constructor(index, name){
this.index = index; //integer identifying system 
this.name = name; //name of system for display
this.planets = []; //list of planets (to be generated)
this.ships = []; //list of ships (to be generated)
this.bombs = []; //list of bombs used in system
this.difficulty = 1; //Scales ship generation attributes
}
randomplanets(){
    var numplanets = Math.floor(Math.random()*16+2);//random number of planets, 2-17
    var orbitradius = 0; //randomized in the loop
    var planetsize = 0; //randomized in the loop
    this.planets.push( new Umo(0,0,Math.floor(Math.random()*3000+1000), "orange") ); //make the sun 
    this.planets[0].name = this.name; // Star name is same as system name
    i=0;
    while (i<numplanets-1){
        i=i+1; //planets[0] is already the sun, so we can skip index 0;
        orbitradius = Math.floor( (Math.random()*500)*(Math.random()*500) + 2000); //Minimum radius 2000, 0-500^2 more concentrated in center
        planetsize = Math.floor( Math.random()*1000 + 80); //Even distribution of sizes, 80-1080
        this.planets.push( new Umo(0,0,planetsize, randcolor() ) );//this is where the planet gets added to the array
        this.planets[i].name = randname(4);//random 4 character name
        this.planets[i].setorbit(this.planets[0], orbitradius, Math.random()*6.28, 1);
        this.planets[i].parentid = 0; //establishes star (planet[0] as parent planet
        //this.randommoons(i);
        }
    }
randommoons(index){//index is of planet
    var nummoons = Math.floor(Math.random()*planets[index].s/150 )//Planets < 150 in size have 0 chance of a moon, planet 300 in size has 50% chance of 1 moon, etc.
    var moonsize = 0; //randomized in loop
    var moonorbitr = 0;//randomized in loop
    var moonindex = 0; //set in loop
    i = nummoons;
    while (i>0){
        i=i-1;
        moonsize = Math.floor(Math.random()*this.planets[index].s/3+10);//radius is 10 plus up to 1/3 of parent planet
        moonorbitr = Math.floor(this.planets[index].s*(Math.random()*3+1.5)); //orbit radius is 1.5x parent planet radius + up to 3x parent planet radius
        moonindex = this.planets.length;
        this.planets.push( new Umo(0,0,moonsize, randcolor()) );
        this.planets[moonindex].name = randname(4);
        this.planets[moonindex].parentid = index;
        this.planets[moonindex].setorbit(this.planets[index],moonorbitr,Math.random()*6.28, 1);//orbit direction is 1, not random
        }
    }
}//end of system class////////////////////////////////////////////////////////////////////////////////////////////////////////////////
let testsystem = new System(2,"thetestsystem");
testsystem.randomplanets();

毕竟问题似乎不在 randomplanets() function 中。 @Sal 建议将 while 循环更改为 for 循环,这确实解决了问题,但更重要的是,我后来在开发随机船舶生成时发现了一个更根本的问题。 我最终将问题隔离到 randname(namelength) function,它启动了它自己的 while 循环:

i=namelength;

我改成

var i=namelength;

我认为省略“var”会导致randname function 在randname function 之外的“i”上运行,在randomplanets ZC1C425268E68385D14AB5074C17A 内(以及全球范围内) 我认为使用 for 循环必须保护 for 循环中使用的“i”免受此 scope 错误的影响。

故事的寓意:记住每次使用它时都用“var”声明你的迭代器,特别是如果你习惯性地使用 i、j、k 等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM