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

2013/5/25

Help

FCG FCG

2013/5/25

#
I want my "bob" actor to follow my "player" actor if you are in range of 75 pixels. How would I do that?
danpost danpost

2013/5/25

#
Just add the following in your act method of the Bob class:
if (!getObjectsInRange(75, Player.class).isEmpty())
{
    Actor player = getObjectsInRange(75, Player.class).get(0);
    turnTowards(player.getX(), player.getY());
    move(3); // however fast you want it to move
    setRotation(0); // optional (if you want your actor to stay up-right
}
danpost danpost

2013/5/25

#
You may want to have your Bob class extends the SmoothMover class; or add instance double fields to the class to hold more accurate x and y location of the Bob objects. This would help the Bob objects move in more the proper direction, especially at slower speeds.
FCG FCG

2013/5/25

#
danpost wrote...
Just add the following in your act method of the Bob class:
if (!getObjectsInRange(75, Player.class).isEmpty())
{
    Actor player = getObjectsInRange(75, Player.class).get(0);
    turnTowards(player.getX(), player.getY());
    move(3); // however fast you want it to move
    setRotation(0); // optional (if you want your actor to stay up-right
}
I currently get the error incompatible types - found java.lang,object but expected greenfoot.actor
danpost danpost

2013/5/25

#
Sorry, I was thinking it returned a list of Actors, where, in fact, it returns a list of Objects. Change line 3 to: Actor player = (Actor) getObjectsInRange(75, Player.class).get(0);
FCG FCG

2013/5/25

#
danpost wrote...
Sorry, I was thinking it returned a list of Actors, where, in fact, it returns a list of Objects. Change line 3 to: Actor player = (Actor) getObjectsInRange(75, Player.class).get(0);
How can I make the picture flip if its in range and I'm behind the picture instead of in front of it.
danpost danpost

2013/5/25

#
Check the GreenfootImage class documentation for a method that might help.
FCG FCG

2013/5/25

#
danpost wrote...
Check the GreenfootImage class documentation for a method that might help.
I know how to flip image but how would I do it so that when my "player" passes "bob" the image flips
danpost danpost

2013/5/25

#
So, you do not need to know how to do it, but how to control it! I see! When in range of the player, compare the x values of the two object. If the player has a lower x, use the flipped image; if not, use the normal image.
FCG FCG

2013/5/26

#
danpost wrote...
Just add the following in your act method of the Bob class:
if (!getObjectsInRange(75, Player.class).isEmpty())
{
    Actor player = getObjectsInRange(75, Player.class).get(0);
    turnTowards(player.getX(), player.getY());
    move(3); // however fast you want it to move
    setRotation(0); // optional (if you want your actor to stay up-right
}
How can I have it so my "bob" actor is moving at a speed of -3 and then if "player" is in range and in front of "bob" he follows the "player" the speed of -3. And if "player" is in range and behind "bob" , "bob" changes speed to 3 and flips image. Could you please include code, I'm not that good in coding.
FCG FCG

2013/5/26

#
danpost wrote...
So, you do not need to know how to do it, but how to control it! I see! When in range of the player, compare the x values of the two object. If the player has a lower x, use the flipped image; if not, use the normal image.
Help Please ^^READ Previous Comment
You need to login to post a reply.