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

2015/8/24

Need to make two objects of the same class face each other every compile/reset?

cactusButt cactusButt

2015/8/24

#
Please bear with me, I have no idea what I'm doing. Basically, what needs to be done is to have two variables(??) of the same class face each other every compile/reset, and with every compile/reset, the two objects spawn at random directions. I've looked all over the net, and it seems that any code I try either mysteriously yields a syntax error or a series of errors after compiling (I usually get the ones for repeating a declaration, or that the object cannot act because it's not even in the program yet, I've forgotten what they're called but yes). Here's the code I'm using for the world class:
import greenfoot.*;

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Toot toot = new Toot();
        addObject(toot, Greenfoot.getRandomNumber(1000), Greenfoot.getRandomNumber(600) );
        
        
        Toot toots = new Toot();
        addObject(toots, Greenfoot.getRandomNumber(1000), Greenfoot.getRandomNumber(600) );
        
    }
    
    
}
and here's one one I'm using for the actor class:
import greenfoot.*;


/**
 * Write a description of class Toot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Toot extends Actor
{
    
    /**
     * Act - do whatever the Toot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void addedToWorld(World world)
    {
        Toot toot;
        Toot toots;
        
        
        
        
       
        if (!world.getObjects(Toot.class).isEmpty())
        {
            
            toot = (Toot)world.getObjects(Toot.class).get(0);
            toot.turnTowards(toots.getX(), toots.getY());
        }
        
        if (!world.getObjects(Toot.class).isEmpty())
        {
            
            toots = (Toot)world.getObjects(Toot.class).get(0) ;
            toots.turnTowards(toot.getX(), toot.getY());
        }
    }

    
}
Right now with the current code, my problem seems to be that I need to initialize the variables I declared, but I don't know what I can equate the variables to in order to initialize them? Please help, it's honestly been hours since I started this simple project, and I'm just getting frustrated. Thank you!
danpost danpost

2015/8/25

#
In the 'addedToWorld' method, you already have a reference to the object being added into the world, 'this'. You cannot use 'isEmpty' to determine if a Toot object is in the world as there will always be the one that was just added. You could use 'size() == 2' to see if both are in the world.
public void addedToWorld(World world)
{
    java.util.List<?> tootList = world.getObjects(Toot.class);
    if (tootList.size() < 2) return;
    Actor toot = (Actor)tootList.get(0);
    Actor toots = (Actor)tootList.get(1);
    toot.turnTowards(toots.getX(), toots.getY());
    toots.turnTowards(toot.getX(), toot.getY());
}
However, easier would be to have the last two lines of code in the 'prepare' method of your world class after adding the two Toot objects into the world (then there would be no need for the 'addedToWorld' method).
cactusButt cactusButt

2015/8/26

#
Thank you! My grade and I greatly appreciate your help :)
You need to login to post a reply.