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

2013/7/16

Getting one object of certain class via coordinates

Segara Segara

2013/7/16

#
Hello, It seems I cannot get the idea behind of this method: "getOneObjectAtOffset(int,int,CLASS.class)" So I made a very simple example: World's subclass:
import greenfoot.*;
public class X extends World
{
    A a;
    Back back;
    int T;
    static int K = 0;
    public X()
    {    
        super(10, 10, 10); 
        T = 10;
        for(int x = 0; x < T; x++){
            for(int y = 0; y < T; y++){
                a = new A();
                addObject( a, x, y);
            }
        }
        back = new Back();
        addObject( back, 8,8);
    }
}
Actor's subclasses:
import greenfoot.*;
public class A extends Actor
{
    int ID = 0;
    public A(){
        ID = ++((X) getWorld()).K;
    }
    
    public void getID(){
        System.out.println("ID:"+ID);
    }
}
import greenfoot.*;
public class Back extends Actor
{
    public void act() 
    {
        if(Greenfoot.isKeyDown("K")){
            ((A) this.getOneObjectAtOffset(4,4,A.class)).getID();
        }
    }    
}
Basically, the code will create 10x10 field of pixels where 1 pixel's size is equal to 10 pixels. Each of the field's pixels gets to get 1 object from A class. The user is given an opportunity to press key "K" to make an attempt of finding one of the A classes' objects via specified in the code coordinates . Why would it return me java.lang.NullPointerException ? Correct me if I am wrong, but wasn't this method supposed to find one object at certain coordinates and return it? I obviously miss something... Thanks. ( Greenfoot API )
danpost danpost

2013/7/16

#
The problem you are having is understanding what the int parameters in 'getOneObjectAtOffset' refer to. They do not make it look at the location (4, 4), but at (getX()+4, getY()+4). As far as the NullPointerException error, if there is no object of the specified class at that location, the method return a null value and you cannot get an ID from a non-existent object. Line 7 in the Back class code (if worked) would get the ID of the A object found, but you are not doing anything with it (not saving it in a field or using it anywhere). Once the statement has executed the value is lost. You may want to use the World class method 'getObjectsAt' which looks at a specific location. It, however, returns a list object, so you will need to check to see if it is empty and then, if not, get the first object from the list, cast it to the class it is and then execute the method ( 'getID' ) on it.
if (!getWorld().getObjectsAt(4, 4, A.class).isEmpty())
    int id = ((A)getWorld().getObjectsAt(4, 4, A.class).get(0)).getID();
Segara Segara

2013/7/16

#
Hi danpost, Thank you for the reply. I've forgotten about "getObjectsAt". That's a green light for me.
As far as the NullPointerException error, if there is no object of the specified class at that location, the method return a null value and you cannot get an ID from a non-existent object.
Well, as you saw in the code, I do create objects of A class, but don't save them anywhere. So this is why I would not get why I end up hitting a null, but then you say:
They do not make it look at the location (4, 4), but at (getX()+4, getY()+4).
Currently a bit confused about "getX()"'s origin, but I will get through it in a moment. Also, thanks for the code part, I would eventually get down to writing it. ^^ Thank you.
danpost danpost

2013/7/16

#
Segara wrote...
Well, as you saw in the code, I do create objects of A class, but don't save them anywhere.
I matters not whether you have saved them or not, or even whether you create and add them to the world or not. If the method does not find any objects of the given class whose image overlaps the location given, then the method will return null. It is the location that is important. If you place a Back object at location (8, 8), then using getX() (or getY()) in the back class will return 8 for that Back object. If you use 'getOneObjectAtOffset(4, 4, null)' for that Back object, then it will look at location (12, 12) for an object. If no object has its image overlapping that location, null is returned.
You need to login to post a reply.