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

2017/4/24

How do I make an object follow another under condition?

cryptxt cryptxt

2017/4/24

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ChaseKiller here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ChaseKiller extends Actor
{
    /**
     * Act - do whatever the ChaseKiller wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int a = UJANG.buttons2;
        do{
            move(1);
            if (getWorld().getObjects(UJANG.class).isEmpty()) return; // skips following if the ujang is not in world
            Actor ujang = (Actor)getWorld().getObjects(UJANG.class).get(0); // gets reference to ujang
            turnTowards(ujang.getX(), ujang.getY()); // turn toward ujang
        }while(a<=5);
        }
    }    
Is what I have in the class for the object that I would like to follow the class "UJANG" and the buttons2 is the condition so that it only starts following ujang when he collected 5 buttons... I'm guessing what I do wrong is how I'm trying to access the variable I declared in Ujang's class, but I honestly have no idea what to do to make it work... (And the following part apparently needs to be under a loop? Is what my class assignment's wanting of me) please help me out...! Just in case, this is the code in Ujang's class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class UJANG here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UJANG extends Actor
{
    private int buttons = 0;
    public static int buttons2 = 0;
    /**
     * Act - do whatever the UJANG wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {        
        if (Greenfoot.isKeyDown("left"))
        {
            move(-4);
        }    
        if (Greenfoot.isKeyDown("right"))
        {
            move(4);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-4);   
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+4);
        }

        Labirin a = (Labirin)getOneObjectAtOffset(0,0,Labirin.class);
        if(a!=null){
            World world = getWorld();
            world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.stop();
        }

        Barangbukti b = (Barangbukti)getOneObjectAtOffset(0,0,Barangbukti.class);
        if(b!=null){
            buttons++;
            getWorld().removeObject(b);
        }       

        Buttons2 d = (Buttons2)getOneObjectAtOffset(0,0,Buttons2.class);
        if(d!=null){
            UJANG.buttons2++;
            getWorld().removeObject(d);
        }       

        Actor Locked;
        if(buttons==5){
            World world = getWorld();
            getWorld().removeObjects(getWorld().getObjects(Locked.class));
        }

        Actor Locked2;
        if(buttons2>=5){
            World world = getWorld();
            getWorld().removeObjects(getWorld().getObjects(Locked.class));
        }

        Locked c = (Locked)getOneObjectAtOffset(0,0,Locked.class);
        if(c!=null){
            World world = getWorld();
            world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.stop();
        }

        Locked2 e = (Locked2)getOneObjectAtOffset(0,0,Locked2.class);
        if(e!=null){
            World world = getWorld();
            world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.stop();
        }

        wayout f = (wayout)getOneObjectAtOffset(0,0, wayout.class);
        if(f!=null){
            World world = getWorld();
            world.addObject(new victory(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.stop();
        }ChaseKiller ck = (ChaseKiller)getOneObjectAtOffset(0,0, ChaseKiller.class);

    }
}

danpost danpost

2017/4/24

#
First thing is buttons2 should not be a static field. How many buttons are collected is the state of the actor itself -- not of the class. You can add a getter method to make the value accessible from outside the class:
public int getButtons2()
{
    return buttons2;
}
Then, in the ChaseKiller act method, a 'do-while' is not what you want. A simple 'if' will do there:
public void act()
{
    if (!getWorld().getObjects(UJANG.class).isEmpty())
    {
        UJANG ujang = (UJANG) getObjects(UJANG.class).get(0);
        if (ujang.getButtons2() <= 5) turnTowards(ujang.getX(), ujang.getY());
    }
    move(1);
}
You need to login to post a reply.