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:
Actor's subclasses:
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 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 | import greenfoot.*; public class A extends Actor { int ID = 0 ; public A(){ ID = ++((X) getWorld()).K; } public void getID(){ System.out.println( "ID:" +ID); } } |
1 2 3 4 5 6 7 8 9 10 | import greenfoot.*; public class Back extends Actor { public void act() { if (Greenfoot.isKeyDown( "K" )){ ((A) this .getOneObjectAtOffset( 4 , 4 ,A. class )).getID(); } } } |