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

2013/9/15

Help me plz

RedFox92 RedFox92

2013/9/15

#
How could i get one acter run away from another
You can accomplish this several different ways. You could have the running actor have a set path to go if the other actor gets close, or you can make it go exactly 180 degrees away from the other actor at a specific length. I'll explain how to do the second. You can use this method to find if specific actors are around another actor:
getActorsInRange(num, Actor.class);
The above method will return a List of actors around the actor calling the method up to the value of num, and you must replace Actor.class with whatever the object you want to look for. If your object is of the Elephant class, you should put it "Elephant.class" instead of Actor.class. You should only put in Actor.class if you want to look for ALL actors of any kind around the main actor. You can use getActorsInRange within the Running actor's class to see if the Chasing actor is nearby a specific amount of cells. To do that, you could do something like this:
if (getActorsInRange(num, Chasing.class).size() > 0)
{
     runAway(); //This method will only get called if the size of the Chasing actors in range is greater than 0
}
...
the .size() method will find the amount of objects inside the List returned by getActorsInRange(). If there is a Chasing actor near enough to the Running Actor, the size of getActorsInRange() will become 1, which is greater than 0. If you want to turn exactly 180 degrees away from the Chasing actor, you need to first know the rotation when the Running actor is looking exactly at the Chasing actor. You can use the following methods to figure that out:
turnTowards(x,y);
turn(amount);
setRotation(rot);
getRotation();
move(amount);
turnTowards() will make the actor look at that specific cell in the world. turn() will turn the actor the amount specified (positive number will turn the actor right, negative will turn it left). setRotation() will set the rotation of the actor to the integer specified. getRotation() will return the current rotation of the actor, from 0-359. move() will move the character forward in the current direction it's facing the amount of cells specified. There are two ways you can go from here to accomplish moving exactly 180 degrees away from the Chasing actor. Both involve using the turnTowards() method. You first need to use turnTowards() to turn your actor exactly towards the Chasing actor (You'll find out why we're looking at him). But you need a reference to the actor, you can do that by using the getActorsInRange() method again, but this time, by using a different method from the List returned:
//Make sure that the size of getActorsInRange is greater than 0!
Actor act = getActorsInRange(num, Chasing.class).get(0);
The get() method will return the object at the specified index, starting with 0 and going up to size()-1. If you only have one Chasing actor, use this method. Now with this act object you have, you can use it's X and Y points to turn towards it, like so:
turnTowards(act.getX(), act.getY());
Now you're Running actor is facing the Chasing actor. Now from here, you can either use turn(), or setRotation() and getRotation() to turn 180 degrees around.
turn(180); //or turn(-180) both do the same

OR

setRotation(getRotation()+180); //or getRotation()-180)
This will turn your actor exactly 180 degrees from the Chasing actor. Now just use move to make your actor move away, make it seem like it's running.
move(amount);
There you go, lengthy, but if you want to have an actor move away from another actor by itself, this is a simple way of doing that. Hope it helped!
RedFox92 RedFox92

2013/9/15

#
Not working plz help
   public void act() 
    {
        move(9);
        
        if (atWorldEdge())
        {
            turn(68);
        }
        
        if (Greenfoot.getRandomNumber(100) < 20)
        {
          turn( Greenfoot.getRandomNumber(100)-50 );  
        }
        
        if (getActorsInRange(1, Frog.class).size() > 0)  
        {  
           runAway();
        }  
        

    } 
RedFox92 RedFox92

2013/9/15

#
Line 15
Sorry for later response than usual. I accidentally typed the wrong thing. Instead of getActorsInRange, it should be getObjectsInRange(). Sorry for the confusion.
skyhighh skyhighh

2013/10/16

#
hello i have a question how could i use this to have a actor chase another actor when the other actor is in range of the chasing actor?
skyhighh skyhighh

2013/10/16

#
i already have my chasing actor(enemy) moving randomly (left,right,up and down), but i want it to forget this when my other actor(hero) gets near the enemy and make the enemy chase the hero
danpost danpost

2013/10/16

#
@skyhighh, do the same as above, but skip over the 'turn(180)' part. Also, use the following 'if' clause:
if (getObjectsInRange(num, Hero.class).isEmpty())
{
    // normal random movement
}
else
{
    // get object reference to retrieve location from
    // use location in 'turnTowards' method
    // move
}
skyhighh skyhighh

2013/10/18

#
tyvm:)
You need to login to post a reply.