Looping through array to get distance between objects
So im trying to loop through an array of 'enemies' which are sprites, and get their X, Y, Width, Height values (i think my actual collision code is right) for a collision response but having trouble doing that. I know why im having a problem (the element[i] is just taking away from its own value )I just cant figure out how to get past it. Ive tried doing nested for loops but have not had success. Here is the initial patch of code:
for(var i = 0; i < enemies.length; i++)
{
if (enemies[i].x < enemies[i].x + enemies[i].width &&
enemies[i].x + enemies[i].width > enemies[i].x &&
enemies[i].y < enemies[i].y + enemies.height &&
enemies.height + enemies.y > enemies.y)
{
console.log("Colliding");
}
}
Thanks to any help in advance!
javascript for-loop
|
show 2 more comments
So im trying to loop through an array of 'enemies' which are sprites, and get their X, Y, Width, Height values (i think my actual collision code is right) for a collision response but having trouble doing that. I know why im having a problem (the element[i] is just taking away from its own value )I just cant figure out how to get past it. Ive tried doing nested for loops but have not had success. Here is the initial patch of code:
for(var i = 0; i < enemies.length; i++)
{
if (enemies[i].x < enemies[i].x + enemies[i].width &&
enemies[i].x + enemies[i].width > enemies[i].x &&
enemies[i].y < enemies[i].y + enemies.height &&
enemies.height + enemies.y > enemies.y)
{
console.log("Colliding");
}
}
Thanks to any help in advance!
javascript for-loop
2
please addenemiesand a wanted result.
– Nina Scholz
Nov 20 at 12:06
Yea sorry that was a mistake its supposed to be enemies[i], must have ctrlZ too far.
– user7201716
Nov 20 at 12:08
you have an array (probably) calledenemies, then inside loop you try to accesselementand alsoelementp, please, take some time to read your code and edit the question with a Minimal, Complete, and Verifiable example to help people that is trying to help you
– Calvin Nunes
Nov 20 at 12:08
1
Sorry but, this is a horrible and non-sense code.enemiescan't haveheightproperty because it's not an object. Plus,enemies[i].x < enemies[i].x + enemies[i].widthandenemies[i].x + enemies[i].width > enemies[i].xhas no difference between each other.
– Rıdvan Sumset
Nov 20 at 12:16
1
What collisions are you trying to check? Between each of the enemies (each vs each) or between enemy vs hero?
– Daniil Andreyevich Baunov
Nov 20 at 12:21
|
show 2 more comments
So im trying to loop through an array of 'enemies' which are sprites, and get their X, Y, Width, Height values (i think my actual collision code is right) for a collision response but having trouble doing that. I know why im having a problem (the element[i] is just taking away from its own value )I just cant figure out how to get past it. Ive tried doing nested for loops but have not had success. Here is the initial patch of code:
for(var i = 0; i < enemies.length; i++)
{
if (enemies[i].x < enemies[i].x + enemies[i].width &&
enemies[i].x + enemies[i].width > enemies[i].x &&
enemies[i].y < enemies[i].y + enemies.height &&
enemies.height + enemies.y > enemies.y)
{
console.log("Colliding");
}
}
Thanks to any help in advance!
javascript for-loop
So im trying to loop through an array of 'enemies' which are sprites, and get their X, Y, Width, Height values (i think my actual collision code is right) for a collision response but having trouble doing that. I know why im having a problem (the element[i] is just taking away from its own value )I just cant figure out how to get past it. Ive tried doing nested for loops but have not had success. Here is the initial patch of code:
for(var i = 0; i < enemies.length; i++)
{
if (enemies[i].x < enemies[i].x + enemies[i].width &&
enemies[i].x + enemies[i].width > enemies[i].x &&
enemies[i].y < enemies[i].y + enemies.height &&
enemies.height + enemies.y > enemies.y)
{
console.log("Colliding");
}
}
Thanks to any help in advance!
javascript for-loop
javascript for-loop
edited Nov 20 at 12:14
asked Nov 20 at 12:02
user7201716
2
please addenemiesand a wanted result.
– Nina Scholz
Nov 20 at 12:06
Yea sorry that was a mistake its supposed to be enemies[i], must have ctrlZ too far.
– user7201716
Nov 20 at 12:08
you have an array (probably) calledenemies, then inside loop you try to accesselementand alsoelementp, please, take some time to read your code and edit the question with a Minimal, Complete, and Verifiable example to help people that is trying to help you
– Calvin Nunes
Nov 20 at 12:08
1
Sorry but, this is a horrible and non-sense code.enemiescan't haveheightproperty because it's not an object. Plus,enemies[i].x < enemies[i].x + enemies[i].widthandenemies[i].x + enemies[i].width > enemies[i].xhas no difference between each other.
– Rıdvan Sumset
Nov 20 at 12:16
1
What collisions are you trying to check? Between each of the enemies (each vs each) or between enemy vs hero?
– Daniil Andreyevich Baunov
Nov 20 at 12:21
|
show 2 more comments
2
please addenemiesand a wanted result.
– Nina Scholz
Nov 20 at 12:06
Yea sorry that was a mistake its supposed to be enemies[i], must have ctrlZ too far.
– user7201716
Nov 20 at 12:08
you have an array (probably) calledenemies, then inside loop you try to accesselementand alsoelementp, please, take some time to read your code and edit the question with a Minimal, Complete, and Verifiable example to help people that is trying to help you
– Calvin Nunes
Nov 20 at 12:08
1
Sorry but, this is a horrible and non-sense code.enemiescan't haveheightproperty because it's not an object. Plus,enemies[i].x < enemies[i].x + enemies[i].widthandenemies[i].x + enemies[i].width > enemies[i].xhas no difference between each other.
– Rıdvan Sumset
Nov 20 at 12:16
1
What collisions are you trying to check? Between each of the enemies (each vs each) or between enemy vs hero?
– Daniil Andreyevich Baunov
Nov 20 at 12:21
2
2
please add
enemies and a wanted result.– Nina Scholz
Nov 20 at 12:06
please add
enemies and a wanted result.– Nina Scholz
Nov 20 at 12:06
Yea sorry that was a mistake its supposed to be enemies[i], must have ctrlZ too far.
– user7201716
Nov 20 at 12:08
Yea sorry that was a mistake its supposed to be enemies[i], must have ctrlZ too far.
– user7201716
Nov 20 at 12:08
you have an array (probably) called
enemies, then inside loop you try to access element and also elementp, please, take some time to read your code and edit the question with a Minimal, Complete, and Verifiable example to help people that is trying to help you– Calvin Nunes
Nov 20 at 12:08
you have an array (probably) called
enemies, then inside loop you try to access element and also elementp, please, take some time to read your code and edit the question with a Minimal, Complete, and Verifiable example to help people that is trying to help you– Calvin Nunes
Nov 20 at 12:08
1
1
Sorry but, this is a horrible and non-sense code.
enemies can't have height property because it's not an object. Plus,enemies[i].x < enemies[i].x + enemies[i].width and enemies[i].x + enemies[i].width > enemies[i].x has no difference between each other.– Rıdvan Sumset
Nov 20 at 12:16
Sorry but, this is a horrible and non-sense code.
enemies can't have height property because it's not an object. Plus,enemies[i].x < enemies[i].x + enemies[i].width and enemies[i].x + enemies[i].width > enemies[i].x has no difference between each other.– Rıdvan Sumset
Nov 20 at 12:16
1
1
What collisions are you trying to check? Between each of the enemies (each vs each) or between enemy vs hero?
– Daniil Andreyevich Baunov
Nov 20 at 12:21
What collisions are you trying to check? Between each of the enemies (each vs each) or between enemy vs hero?
– Daniil Andreyevich Baunov
Nov 20 at 12:21
|
show 2 more comments
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392590%2flooping-through-array-to-get-distance-between-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392590%2flooping-through-array-to-get-distance-between-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
please add
enemiesand a wanted result.– Nina Scholz
Nov 20 at 12:06
Yea sorry that was a mistake its supposed to be enemies[i], must have ctrlZ too far.
– user7201716
Nov 20 at 12:08
you have an array (probably) called
enemies, then inside loop you try to accesselementand alsoelementp, please, take some time to read your code and edit the question with a Minimal, Complete, and Verifiable example to help people that is trying to help you– Calvin Nunes
Nov 20 at 12:08
1
Sorry but, this is a horrible and non-sense code.
enemiescan't haveheightproperty because it's not an object. Plus,enemies[i].x < enemies[i].x + enemies[i].widthandenemies[i].x + enemies[i].width > enemies[i].xhas no difference between each other.– Rıdvan Sumset
Nov 20 at 12:16
1
What collisions are you trying to check? Between each of the enemies (each vs each) or between enemy vs hero?
– Daniil Andreyevich Baunov
Nov 20 at 12:21