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

2012/4/6

TRacking ojects

-nic- -nic-

2012/4/6

#
can any one help me, how would you track another object(class) or can some one please show me a scenorio that does this.
tkiesel tkiesel

2012/4/6

#
You can make an Actor variable. Then, once you aquire your target, you can look at the getX() and getY() of that Actor to see where it is.
import greenfoot.*;
import java.util.List;

public class Tracker extends Actor
{

    Actor target = null; 

    public void act() 
    {
        // no target aquired, wait for one
        if ( target == null )
        {
            // Look for targets
            List<Actor> targets = getObjectsInRange(100, Actor.class);
            if ( !targets.isEmpty() )
            {
                // If possible targets seen, choose one at random.
                int choice = Greenfoot.getRandomNumber(targets.size() );
                target = targets.get(choice);
            }
        }
        //target acquired. Seek!
        else
        {
            // Close 90% of distance to target.  See Achilles/Tortoise.. ;)
            setLocation( getX() + (target.getX()-getX())/10 , getY() + (target.getY()-getY())/10 );
        }
    }    
}
-nic- -nic-

2012/4/6

#
thats great ==how would you code it if there was only one target eg player ?? sorry answerd my own question big help thx :):):):):)
SPower SPower

2012/4/6

#
If you're using Greenfoot 2.2, you can use the all new "turnTowards(int x, int y)" method, which is defined in Actor.
tkiesel tkiesel

2012/4/6

#
My //comment above should have read close TO 90% of distance to target. Oops. ;)
You need to login to post a reply.