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
tilePosArrayXadd1defined? how many items are in that array?tilex0andtiley0should, after the loop finishes, have a length equal totilePosArrayXadd1.length... moveconsole.log(tilex0.length)after the loop to verify