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

2015/10/14

Make an actor follow another using getObjectsinRange

Vishis Vishis

2015/10/14

#
I am trying to make an actor follow the main player of my game by moving left when the player is in range but on the left side, then right when the player is on the right side and in range. Im using a separate counter (which works fine) to make the the enemy follow the player in different directions. When the player reached the middle of the range (so on top of the enemy) it should switch to the other movement pattern. Im confused as how to find the center of the radius. objects.size >0 detects if the player is in the range at all but i am not sure what to do to detect the center of the range. Any help is appreciated thank you!
    public void patternSet()
    {
        Actor Player;
        List <Player> objects = getObjectsInRange (200, Player.class);

        if(turnAmount == 0)
        {
            if ( objects.size() >0 && objects.size() < 100) 
            { 
                setLocation(getX() - 1 ,getY() );

            }
            else
            {
                turnCount(1);
            }
        }

        if(turnAmount == 1)
        {
            if ( objects.size() >0 && objects.size() <1) 
            { 
                setLocation(getX() + 1 ,getY() );

            }
            else
            {
                turnCount(-1);
            }
        }

    }
danpost danpost

2015/10/14

#
If you are just moving left and right, you can just compare the 'getX' values of both actors and the sign of the difference would tell you which way to move.
Vishis Vishis

2015/10/14

#
danpost wrote...
If you are just moving left and right, you can just compare the 'getX' values of both actors and the sign of the difference would tell you which way to move.
Oh, then my next question would be how do i directly compare the "getX" values of each actor? With the comparison being between the 'Player' class and the 'Enemy3' class and the code being inside the 'Enemy3' class. Would it be something along the lines of this:
    public void patternSet()
    {
        Actor Player;
        Player = new Player();
        if(Player.getX() < getX() - 200)
        {
            setLocation(getX() - 2, getY());
        }

        if(Player.getX() < getX() + 200)
        {
            setLocation(getX() + 2, getY());
        }
    }
danpost danpost

2015/10/14

#
Your line 3 above creates a new Player object which is not any Player object you have in your world. Any Player object you may have in the world and in range of this actor would be listed in your 'objects' List object created and assigned at line 4 of your original post. If 'objects' is not empty, then any and all objects within the list would be Player objects that are in your world. If you only have, at most, one Player object in the world, then you can get a reference to that object and follow it with the following:
public void patternSet()
{
    List players = getObjectsInRange(200, Player.class);
    if (!players.isEmpty())
    {
        Actor player = (Actor)players.get(0);
        int diffX = man.getX()-getX();
        setLocation(getX()+2*(int)Math.signum(diffX), getY());
    }
}
Vishis Vishis

2015/10/14

#
danpost wrote...
Your line 3 above creates a new Player object which is not any Player object you have in your world. Any Player object you may have in the world and in range of this actor would be listed in your 'objects' List object created and assigned at line 4 of your original post. If 'objects' is not empty, then any and all objects within the list would be Player objects that are in your world. If you only have, at most, one Player object in the world, then you can get a reference to that object with the following:
public void patternSet()
{
    List players = getObjectsInRange(200, Player.class);
    if (!players.isEmpty())
    {
        Actor player = (Actor)players.get(0);
        int diffX = man.getX()-getX();
        setLocation(getX()+2*(int)Math.signum(diffX), getY());
    }
}
Woah ok so now I have more questions about your code, since I havent used anything like that before, for the math what does the signum do? And the "man" in "man.getX" where is that supposed to come from (the compiler says it cannot find it as a variable), and what is the purpose of using it? would i not put "Enemy3" there or "this"? also the "(Actor)players.get(0)" what does that line do? I do understand the logic of using a list however. Thank you for the advice by the way!
danpost danpost

2015/10/14

#
Vishis wrote...
for the math what does the signum do?
Refer to the Math class API on the signum method (it returns the sign of the given number).
the "man" in "man.getX" where is that supposed to come from (the compiler says it cannot find it as a variable), and what is the purpose of using it?
The was missed in converting my test code to what you had. It should be "player.getX()".
the "(Actor)players.get(0)" what does that line do?
The 'get' method returns the element at the given index ('0' in this case -- the first element) from the 'players' list. The '(Actor)' in front tells the compiler to refer to the element as an Actor object (as opposed to an Object class object). This lets the compiler look in the Actor class for any methods or fields referenced on the object ('getX' would not be found as a method for the object if the compiler did not know it was indeed an Actor type object).
Vishis Vishis

2015/10/14

#
danpost wrote...
Vishis wrote...
for the math what does the signum do?
Refer to the Math class API on the signum method (it returns the sign of the given number).
the "man" in "man.getX" where is that supposed to come from (the compiler says it cannot find it as a variable), and what is the purpose of using it?
The was missed in converting my test code to what you had. It should be "player.getX()".
the "(Actor)players.get(0)" what does that line do?
The 'get' method returns the element at the given index ('0' in this case -- the first element) from the 'players' list. The '(Actor)' in front tells the compiler to refer to the element as an Actor object (as opposed to an Object class object). This lets the compiler look in the Actor class for any methods or fields referenced on the object ('getX' would not be found as a method for the object if the compiler did not know it was indeed an Actor type object).
Oh my it works thank you so much for the code and the explanations as well!
You need to login to post a reply.