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

2015/2/10

Cant update Info from a methods

joandvgv joandvgv

2015/2/10

#
Ok, first of all this is my first post here. I start this discussion because I need some help with a project i'm developing. I'm creating a simulation of urban sanitation, where trucks find trash and collect it. One of the requirements is I need to update the truck capacity (it starts in 1000kg and when it collects 74kg, the truck capacity should be 926) So I ask for the info to the class Punto (the class for the trash) but i can't get it updated. There could be hundreds of "puntos" with different values in cantidad but I still get the first one's value. Ill show you the truck class:
public class Recolector extends Auto
{
    int capacidad=1000; //capacity
    int cantidadbasura; //trashamount
     public Informacion informacion;
     public Infoparada infoparada;
     public Punto punto;
     private Ambiente ambienteaux;
    public void act() 
    {
       actuar();
        startUp();
        moverse();
        move(1);
        pasos++; //steps ++
        
       if(haybasura()) { // found trash, collects it. 
            recoger();
        }
    }    
public void actuar(){ //getting some info
       ambienteaux=(Ambiente)getWorld();
       informacion = ambienteaux.getInformacion();
       punto=ambienteaux.getPunto();
       infoparada=ambienteaux.getInfoparada();
       cantidadbasura=punto.getCantidad();
    }
public boolean haybasura() //s
    {
        Actor punto = getOneObjectAtOffset(0, 0, Punto.class);
        if(punto != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    /**
     *Collects trash
     */
    public void recoger()
    {
        Actor punto = getOneObjectAtOffset(0, 0, Punto.class);
        if(punto != null) {
           actuar();
           Greenfoot.delay(retraso);
           getWorld().removeObject(punto);
           capacidad =  capacidad - cantidadbasura; 
           informacion.contador(capacidad);
        }
    }   
    
}
I've tried so many things so far. Like call the method
 punto.getCantidad(); 
just when it founds trash but it doesn't compile. Tells me that can't find punto.getCantidad symbol. I've also tried using and auxiliar "ambiente" variable (cause there's already one in the superclass Vehicle) But none of what i try works. Anny suggestions? Thanks in advance. If you don't understand any part of the code just ask.
danpost danpost

2015/2/10

#
It looks like you have mixed in some initialization code with the action code. By calling 'actuar' every act cycle, you are constantly resetting the fields within this class. You only want to initialize those values once -- when the 'Recolecter' object is created. Initialization code belongs in a constructor code block ( 'public Recolecter()' ). The call to 'startUp' also, if it also does some initializing of the object, may also need to be moved to the constructor block. I cannot say where 'moverse' should be called from -- depends on what it does. Sidenote: if you have code that needs to be processed in the constructor of the 'Vehicle' class ( 'public Vehicle()' ), you need to add 'super();' as the first line in the Recolecter constructor.
joandvgv joandvgv

2015/2/10

#
Ok, well... Yes, StartUp does some initialization rutines; but i copied that from an scenario called "Skeleton Invasion" that works perfectly. I also copied the movement logic he used and thats what moverse() does. I deleted the actuar method and put the initializations in the beginning of the Recolector constructor. But i don't have any constructor in Recolector class neither in the superclass Auto (vehicle). May that be the one that's causing me the issue?
danpost danpost

2015/2/10

#
Anything you need done just once, when the Recolecter object is created, should be placed in the constructor of the Recolecter class. That is,
public Recolecter()
{
    actuar();
    startup();
}
Only those things that need constant doing (moving, checking for trash, etc.) need be in the 'act' method.
joandvgv joandvgv

2015/2/10

#
Ok! Thank you so much. I'll try and let you know how that goes.
You need to login to post a reply.