This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jonche
Jonche wrote ...

2013/11/13

Compare locations, doesn't work properly...

Jonche Jonche

2013/11/13

#
Hi guys, I'm just programming a game as a project. I'm planning to create my own RPG game. So there I am writing down in a monster class an algorithm that should check if the hero (Joe in my game) is near 300 pixels. So I wrote this :
public void checkIfJoeIsNear()
       {
        if (getX() - Joe.joeX < 300 && Joe.joeY - getY() < 300 
                && getX() - Joe.joeX > 0 && Joe.joeY - getY() > 0) 
                {joeIsNear = true;} else {joeIsNear = false;}
        if (getX() - Joe.joeX < 300 && getY() - Joe.joeY < 300 
                && getX() - Joe.joeX > 0 && getY() - Joe.joeY > 0) 
                {joeIsNear = true;} else {joeIsNear = false;}
        if (Joe.joeX - getX() < 300 && Joe.joeY - getY() < 300 
                && Joe.joeX - getX() > 0 && Joe.joeY - getY() > 0) 
                {joeIsNear = true;} else {joeIsNear = false;}
        if (Joe.joeX - getX() < 300 && getY() - Joe.joeY < 300 
                && Joe.joeX - getX() > 0 && getY() - Joe.joeY > 0) 
                {joeIsNear = true;} else {joeIsNear = false;}
       }
The problem is, the monster considers the boolean joeIsNear as true only if he is on the upper left side of the monster itself... I can't solve this alone ^^ please help me !
danpost danpost

2013/11/13

#
There is an Actor class method called 'getObjectsInRange' that you can use in the class of the monster to see if any Joe objects are near.
Jonche Jonche

2013/11/13

#
I can't get it to work, do I need to insert it in a "if" condition? I does only return a list, I don't know what I can do with it...
danpost danpost

2013/11/13

#
Well, if the list returned after plugging in the proper arguments for the method is not empty, then Joe would be in range. This condition (using an 'if') would determine whether you do whatever you want done when Joe is in range or not.
Jonche Jonche

2013/11/14

#
This won't work, since I need to get also the direction of where the object is. I'll make it clearer : I also want to know where an object is comparing to Joe's location. I need to have four conditions in order to know whether an object is in attack range, because I also want to be able to implement an attack method... Meaning that I need to get the unique object in order to substract health from it.
davmac davmac

2013/11/14

#
Try some sample values and go through your code to see what it does. Suppose the monster is at 100,100 and that Joe is at 105,105.
        if (getX() - Joe.joeX < 300 && Joe.joeY - getY() < 300   
                && getX() - Joe.joeX > 0 && Joe.joeY - getY() > 0)   
getX() - joeX should be -5 (which is less than 300). joeY - getY() should be 5, which is also less than 300. getX() - joeX will be -5, which is NOT > 0, so the condition is false. So it sets joeIsNear to false (line 5).
        if (getX() - Joe.joeX < 300 && getY() - Joe.joeY < 300   
                && getX() - Joe.joeX > 0 && getY() - Joe.joeY > 0
getX() - joeX is -5 (is < 300). getY() - joeY is -5 (is < 300). getX() - joeX is still -5, however, so this condition is also false, and it sets joeIsNear to false - again - on line 8.
        if (Joe.joeX - getX() < 300 && Joe.joeY - getY() < 300   
                && Joe.joeX - getX() > 0 && Joe.joeY - getY() > 0) 
joeX - getX() is 5, which is < 300. joeY - getY() is also 5, which is < 300. joeX - getX() is also > 0 as is joeY - getY(), so in this case, it will set joeIsNear to true on line 11. This is what you wanted. However, another line follows:
        if (Joe.joeX - getX() < 300 && getY() - Joe.joeY < 300   
                && Joe.joeX - getX() > 0 && getY() - Joe.joeY > 0) 
joeX - getX() is 5, which is < 300. getY() - joeY is -5, which is < 300. joeX - getX() is 5, which is > 0. getY() - joeY is -5, which is NOT > 0. So, the condition is false. So, joeIsNear will be set back to false on line 14. So you see, the last 'if' statement is the only one that controls whether joeIsNear is true or false. The other 'if' statements are effectively ignored. You should probably be using 'else if' in each case, and only set joeIsNear false in the final 'else' clause:
        if (getX() - Joe.joeX < 300 && Joe.joeY - getY() < 300   
                && getX() - Joe.joeX > 0 && Joe.joeY - getY() > 0)   
                {joeIsNear = true;}
        else if (getX() - Joe.joeX < 300 && getY() - Joe.joeY < 300   
                && getX() - Joe.joeX > 0 && getY() - Joe.joeY > 0)   
                {joeIsNear = true;}
        else if (Joe.joeX - getX() < 300 && Joe.joeY - getY() < 300   
                && Joe.joeX - getX() > 0 && Joe.joeY - getY() > 0)   
                {joeIsNear = true;}
        else if (Joe.joeX - getX() < 300 && getY() - Joe.joeY < 300   
                && Joe.joeX - getX() > 0 && getY() - Joe.joeY > 0)   
                {joeIsNear = true;}
        else {joeIsNear = false;}  
You need to login to post a reply.