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

2014/9/20

getOneObjectAtOffset() not working

Cyoce Cyoce

2014/9/20

#
Here's my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

[code]import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cell here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cell extends Actor
{
    public boolean alive = false;
    /**
     * Act - do whatever the Cell wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }
    public boolean living()
    {
        return alive;
    }
    public int neighbours()
    {
        int i = 0;  
        Actor j = getOneObjectAtOffset(32,32,Cell.class);
        if(j != null) {
        if(j.living())
        {
            i++;
        }}
        j = getOneObjectAtOffset(32,32,Cell.class);
        if(j != null){
        if(j.living())
        {
            i++;
        }}
        j = getOneObjectAtOffset(32,32,Cell.class);
        if(j != null) {
        if(j.living())
        {
            i++;
        }}
        j = getOneObjectAtOffset(32,32,Cell.class);
        if(j != null){
        if(j.living())
        {
            i++;
        }}
        return i;
    }
}
I always get the error "cannot find symbol — method living()" What is going on?
erdelf erdelf

2014/9/20

#
j references to an Actor object, not a cell replace line 29 with
Cell j = (Cell) getOneObjectAtOffset(32,32,Cell.class);  
danpost danpost

2014/9/20

#
What is happening is that 'j' is assigned to an 'Actor' field and the Actor class does not contain a 'living' method. EDIT: do what erdelf suggests. Also, lines 35, 41 and 47 will need adjusted similarly.
Cyoce Cyoce

2014/9/20

#
Thanks for the help! It works now!
You need to login to post a reply.