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

12 hours ago

problem with neighbor count

ttt ttt

12 hours ago

#
The Actor method getNeighbours() gives strange results in certain situations.
import greenfoot.*;  

public class DotWorld extends World {

    public DotWorld(){    
        super( 10, 10, 60 ); 
    
        for( int y=0; y<getHeight(); y++ ) {
            for( int x=0; x<getWidth(); x++ ) {
                addObject( new Dot(), x, y );
            }
        }
    }
}
import greenfoot.*;  

public class Dot extends Actor {
    
    public Dot() {
        setImage( "yellow-draught.png" );  // results depend on the image (!)
    }
    
    public void act() {
        int n = getNeighbours( 1, false, null ).size();  // wrong count for certain combinations of world.cellsize and size of actor image
        // int n = getObjectsInRange( 1, null ).size();  // works
        getWorld().showText( Integer.toString( n ), getX(), getY() );
    }

}
You need to login to post a reply.