• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED The problem of calculating the distance between objects

River

Member
I'm having problems calculating the distance of objects when doing in a platformer game:
I have an explosion object (referred to as: objExplosion) and 3 enemies including: objEnemyA, objEnemyB, objEnemyC (all these 3 enemies are children of objEnemiesParent).
I have objExplosion located at the first position 150 from objEnemyA; objExplosion is 250 distance from objEnemyB, objExplosion is 450 distance from objEnemyC. I want that any enemies that are 150 or more from objExplosion will be burned, enemies that are more than 150 and less than 250 will be thrown and if lying down other than the objExplosion distance above 250 nothing will happen. This means like the picture: objEnemyA will be burned, objEnemyB will be thrown and objEnemyC will be fine. So what should I do?
I used my code as follows, but they only work for the burnt case and not the thrown case? Please everyone help me. Thank you. Note: this objExplosion is just an object with an image sprite (not an explosion it's just an object).
 
Last edited:

chamaeleon

Member
Maybe this does what you want (the value of right might be the opposite of the desired value since the code is untested, adjust assignment as necessary)
GML:
with (objEnemiesParent) {
       if (distance_to_object(other) <= 150) {
           right = x < other.x ? 1 : -1;
           state = STATE_BURN;
       } else if (distance_to_object(other) <= 250) {
           right = x < other.x ? 1 : -1;
           state = STATE_THROWAWAY;
       }
}
 

River

Member
Maybe this does what you want (the value of right might be the opposite of the desired value since the code is untested, adjust assignment as necessary)
GML:
with (objEnemiesParent) {
       if (distance_to_object(other) <= 150) {
           right = x < other.x ? 1 : -1;
           state = STATE_BURN;
       } else if (distance_to_object(other) <= 250) {
           right = x < other.x ? 1 : -1;
           state = STATE_THROWAWAY;
       }
}
thanks, it worked
 
Last edited:
Top