简体   繁体   中英

how to make an Array of a for loop variable with Javascript?

I am new to this forum and I have searched and after a few hours of banging my head on the wall and even registering back at TeamTreeHouse for a detailed course on Arrays I couldn't find what I was looking for. I love teamTreehouse though.

I got a problem with trying to switch from C# to Javascript with lists and Arrays beeing different. It is for RPG Maker MV. I want to make a list of positions X and Y of a "CharacterID" and calculate the distance to each tiles that I have put inside an Array of X positions and Y positions. The ID of the Character is 0 or more specific it is eventArray[ID, x, y] for it's ID and its x and y positions. So I got the position of the CharacterID but I don't understand for the life of me how to loop the thing so that I get max 10 lines of code instead of 36 lines (length of my Array). In C# Vector3's are added automatically to a list or a dictionary and are separate "items" but if I try with the same principle with a Javascript Array I get 1 item containing the whole list of entry and I don't know how to retrieve them.

var eventArray = [];

var tilePosArrayXadd1 = [];
var tilePosArrayYadd1 = [];

Game_Event.prototype.initialize = function (mapId, eventId) {
Game_Character.prototype.initialize.call(this);

this._mapId = mapId;
this._eventId = eventId;

this.locate(this.event().x, this.event().y);
var eventX = this.event().x;
var eventY = this.event().y;
this.refresh();
eventArray.push([eventId, eventX, eventY]);
};

Game_Player.prototype.locate = function (x, y) {
Game_Character.prototype.locate.call(this, x, y);

actorX = $gamePlayer.x;
actorY = $gamePlayer.y;
main(actorX, actorY);
};



function main(x, y) {

var maxTileDist = 2;

///TOPRIGHTTILES
for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y);
};   

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y + 1);


};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y + 2);

};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y + 3);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y + 4);
};


///TOPLEFTTILES
for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y);

};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y + 1);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y + 2);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y + 3);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y + 4);
};


///BOTTOMRIGHTTILES
/*for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push([[i + x+1], y]);
};*/

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y - 1);
};


for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y - 2);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y - 3);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i + x);
    tilePosArrayYadd1.push(y - 4);
};


///BOTTOMLEFTTILES
/*for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push([[i - x], y]);
};*/

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y - 1);

};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y - 2);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y - 3);
};

for (var i = 0; i < maxTileDist ; i++) {
    tilePosArrayXadd1.push(i - x);
    tilePosArrayYadd1.push(y - 4);
};





var tilex0 = [];
var tiley0 = [];

 for (var j = 0; j < tilePosArrayXadd1.length; j++) {

  tilex0.push(eventArray[0][1] - tilePosArrayXadd1[j]);
  tiley0.push(eventArray[0][2] - tilePosArrayYadd1[j]);

  console.log(tilex0[0])
  console.log(tilex0.length)
 }

the code above gives me a length of 1... when I thought it would give me a list of tilePosArrayXadd1.length as declared with the "j" variable. So lol can anyone explain what I am doing wrong or guide me to a very good tutorial containing explanations of this issue. Thank you guys for your time. Ninekorn

You're new here. If you haven't looked over the Help Center , it would probably be a good idea, especially the section on creating a minimal, complete, and verifiable example

When I try to do that for your code, I come up with something that works as I presume is expected:

var events = [[10, 20]]
var foo = [];
var bar = [1, 2, 3]

for (var j = 0; j < bar.length; j++) {
  foo.push(events[0][1] - bar[j]);
}
console.log(foo); //=> [19, 18, 17]
console.log(foo.length); //=> 3

It is very often the case that when trying to create such an example, you will find the error on your own. But if not, can you build this simple example back out to your code and see when it first breaks? That should help solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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