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

2012/9/5

How can i make a global integer

ChilloManiac ChilloManiac

2012/9/5

#
I want to "track" an object in greenfoot (crab) by putting its (x,y) in integers with getX and getY. I want these integers to be used by another object (lobster) so i can make them face the crab. But how do i make the lobsters use a value returned by another object? Thanks in advance :)
danpost danpost

2012/9/5

#
You can get the x and y locations of the crab through 'getX()' and 'getY()', however you need a reference to the crab to do so, i.e 'crab.getX()' and 'crab.getY()'. There are several ways to go about this. In this case, I believe the easiest way is to have an instance Crab variable in the Lobster class and get a reference to the crab after the Lobster is added to the world. Make sure you add the Crab object to the world before the Lobster objects and start your Lobster class code with:
import greenfoot.*;

public class Lobster extends Actor
{
    Crab crab = null;

    public void addedToWorld(World world)
    {
        crab = world.getObjects(Crab.class).get(0);
    }
Now anywhere in the Lobster class you can use 'turnTowards(crab.getX(), crab.getY());' to have the lobster face the crab
ChilloManiac ChilloManiac

2012/9/5

#
Okay so i put this in:
public class Lobster extends Animal
{
    Crab crab = null;
    
    public void addedToWorld(CrabWorld wrold) {
        crab = CrabWorld.getObjects(Crab.class).get(0);
    }
    
    public void turnToCrab() {
        turnTowards(getX(Crab),getY(Crab));
    }
But i makes a compile error in the line: crab = CrabWorld.getObjects(Crab.class).get(0); "non-static method getObjects(java.lang.Class) cannot be referenced from a static context"
Gevater_Tod4711 Gevater_Tod4711

2012/9/5

#
not quite so. try this:
public class Lobster extends Animal { 
Crab crab = null; 

public void addedToWorld(CrabWorld wrold) { 
    crab = (Crab) CrabWorld.getObjects(Crab.class).get(0); //I don't know if this cast is necessary but it'll work I think
}

public void turnToCrab() { 
    turnTowards(crab.getX(), crab.getY()); 
}
but there is another problem: if there is no crab in the world CrabWorld.getObjects(Crab.class).get(0) will throw an IndexOurOfBoundsException. You could do it with a try catch finally block but then maybe the Lobster doesn't know the reference to the crab. I would use that:
import greenfoot.*;
import java.util.List; // this is important;

public Lobster ...
    private boolean crabInitialised = false;
    
    privat Crab crab;
    
    public void act() {
        if (!crabInitialised) {
            List<Crab> c = getWorld().getObjects(Crab.class);
            if (c.size() > 0) {
                crab = c.get(0);
                crabInitialised = true;
            }
        }
    }

    public void turnToCrab() { 
        if (crabInitialised) {
            turnTowards(getX(Crab),getY(Crab));
        }
    }
If the crab is removed you then have to find a new one (if there is one) by using this;
public void act() {
    ...
    if (getWorld().getObjects(Crab.class).size() == 0) {
        crabInitialised = false;
    }
}
Hope this will help.
danpost danpost

2012/9/5

#
You need to use the reference to the world, not the class name of the world in line 6 -- change CrabWorld.getObjects... to wrold.getObjects (that is how you spelled it).
You need to login to post a reply.