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

2014/4/22

Can someone help me with this code from an old game?

RagingAsian RagingAsian

2014/4/22

#
public void spread(){ try{ java.util.List objects_list=getObjectsInRange((getWidth()/5)-2,CombatObject.class); //CombatObject object_to_right=(CombatObject)getOneObjectAtOffset(getWidth()/4,0,CombatObject.class); if(objects_list!=null&&objects_list.size()>0){ if(((Actor)objects_list.get(0)).getWidth()>=getWidth()){ if(((Actor)(objects_list.get(0))).getX()<getX()&&getOneObjectAtOffset(1,0,Doodad.class)==null){ setLocation(getX()+1,getY()); } else if(((Actor)(objects_list.get(0))).getX()>getX()&&getOneObjectAtOffset(-1,0,Doodad.class)==null){ setLocation(getX()-1,getY()); } else if(Greenfoot.getRandomNumber(2)==1&&getOneObjectAtOffset(0,-1,Doodad.class)==null){ setLocation(getX()-1,getY()); } else if(getOneObjectAtOffset(0,-1,Doodad.class)==null){ setLocation(getX()+1,getY()); } if(((Actor)(objects_list.get(0))).getY()<getY()&&getOneObjectAtOffset(0,1,Doodad.class)==null){ setLocation(getX(),getY()+1); } else if(((Actor)(objects_list.get(0))).getY()>getY()&&getOneObjectAtOffset(0,-1,Doodad.class)==null){ setLocation(getX(),getY()-1); } else if(Greenfoot.getRandomNumber(2)==1&&getOneObjectAtOffset(0,-1,Doodad.class)==null){ setLocation(getX(),getY()-1); } else if(getOneObjectAtOffset(0,-1,Doodad.class)==null){ setLocation(getX(),getY()+1); } } } } catch(IllegalStateException e){ the problem is the getWidth part plz help.
danpost danpost

2014/4/22

#
I think the problem is the use of 'if (objects_list!= null && objects_list.size()>0)'. (1) 'getObjectsInRange' will always return a list and never will be 'null' (2) rather than 'size', use 'isEmpty' (it is quicker as the elements in the list do not have to be counted; just need to know that one element is there); Therefore, use 'if(!objects_list.isEmpty())' Also, I do not see a need for the 'try-catch' in this code. If there is a chance this method could be run while the actor is not in the world, then add the following at the beginning of the method: if (getWorld() == null) return;
danpost danpost

2014/4/22

#
There also may be a problem with the last 'else if' conditions of both 'if' segments. It would appear the condition is not checking the proper direction for the movement (the '-1's possibly should be '1's). No. That is not what was funny about it. It is not quite right and I cannot yet describe what it is. I think it is just the use of the same condition in the last two 'else if's in each 'if' segment. I think the following will do the same thing, but looks better:
public void spread()
{
    if (getWorld() == null || getObjectsInRange((getWidth()/5)-2, CombatObject.class).isEmpty()) return;
    Actor nearActor = getObjectsInRange((getWidth()/5)-2, CombatObjct.class).get(0);
    if (nearActor.getWidth() >= getWidth())
    {
        if (nearActor.getX() < getX() && getOneObjectAtOffset(1, 0, Doodad.class) == null)
            setLocation(getX()+1, getY());
        else if (nearActor.getX() > getX() && getOneObjectAtOffset(-1, 0, Doodad.class) == null)
            setLocation(getX()-1, getY());
        else if (getOneObjectAtOffset(0, -1, Doodad.class) == null) // not sure why this condition is here
            setLocation(getX()+1-Greenfoot.getRandomNumber(2), getY());
        if (nearActor.getY() < getY() && getOneObjectAtOffset(0, 1, Doodad.class) == null)
            setLocation(getX(), getY()+1);
        else if (nearActor.getY() > getY() && getOneObjectAtOffset(0, -1, Doodad.class) == null)
            setLocation(getX(), getY()-1);
        else if (getOneObjectAtOffset(0, -1, Doodad.class) == null) // not sure why this condition is here
            setLocation(getX(), getY()+1-Greenfoot.getRandomNumber(2));
    }
}
You need to login to post a reply.