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

2015/2/18

Pausing movement when there's one equal actor.

1
2
3
4
5
6
7
joandvgv joandvgv

2015/2/19

#
Yes, but something is giving me troubles because necesitacarro becomes true because the random number is higher. But it does still pick up the garbage. Which it shouldnt because it is supossed to be higher than its capacity. Why? Because the random number changes and in some point it was higher but then it was not.
danpost danpost

2015/2/19

#
Please post the code of you act method of the truck as it currently is.
joandvgv joandvgv

2015/2/19

#
          
          move(0);      
          if (pauseTimer<=0 && (numruta!=0) ){
            moverse();
            move(1);
            pasos++;
            pasosaux++;
          }
          if(haybasura() && empezarrecoleccion()) {
             if (capacidad<randomNum) {
                  necesitacarro = true;
             }
            else {
            randomNum=Greenfoot.getRandomNumber(800);    
            pauseTimer=361;
            recoger(); //pick up
            }
          }
          if (pauseTimer > 0) pauseTimer -= 2; 
          if (pauseTimer < 0 && getOneObjectAtOffset(0, 0, Punto.class) == null) {
           pauseTimer = 0; // termina de recoger
          }
          if (foundrelleno()){
             if (necesitacarro==true) 
             ((Ambiente)getWorld()).despacharRecolector(numruta);
              descarga();
          }
Now it does the work as it should. I'm glad that finally my project is almost over. The problem about the random was where i was calling it. However, I saw in one of the several runs I've tried negatives values for the truck capacity. It does not happens always. Only happend like twice in 30 runs, but have no idea on why. Any guess? btw, it is a minor issue since it's not ver likely to happen.
danpost danpost

2015/2/19

#
I was going to say the following before I saw your last post: I think I know what is happening. Add the following field to the class:
private Punto malPunto;
Then, when setting 'necesitacarro' to 'true', also do this:
malPunto = (Punto)getOneObjectAtOffset(0, 0, Punto.class);
Finally, change the if conidition to this:
if ( haybasura() && getOneObjectAtOffset(0, 0, Punto.class) != malPunto && empezarreColeccion())
I would like to say that your approach was good. However, by assigning a value to it there, you are depriving yourself of any checks at all on the new value. From the looks of it, you cannot assign it after the check because the check would be on the wrong value; and you cannot assign it before the check because it could change the size of a previously oversized pile of garbage. I think you may have to try what I was going to suggest above.
joandvgv joandvgv

2015/2/19

#
Well, you're totally right. I'll try that and let you know if I have some issues with it again. Thank u so much for your time!
danpost danpost

2015/2/19

#
It probably would have been easier just to have the Punto object assign a random value for its size when created. Then, with a simple getter method, the size would be obtainable to the Recolector object (and it would not be changing on you !! ).
joandvgv joandvgv

2015/2/19

#
danpost wrote...
It probably would have been easier just to have the Punto object assign a random value for its size when created. Then, with a simple getter method, the size would be obtainable to the Recolector object (and it would not be changing on you !! ).
I've tried so. But I got a Null pointer exception. In the act method of my recolector class:
            cantidad=punto.getCantidad();
The punto Class:
public class Punto extends Actor
{
    public int randomNum = Greenfoot.getRandomNumber(250);
    public int cantidad = randomNum;
    public void act() 
    {
      
    }   

   public int getCantidad(){
       return randomNum;
      
}

}
      
In the superclass of recolector 'Auto':
Punto punto;
in the startUpmethod of auto:
       Punto punto=ambiente.getPunto();
So meanwhile. I'll try what you previously said.
danpost danpost

2015/2/19

#
joandvgv wrote...
in the startUpmethod of auto:
Punto punto=ambiente.getPunto();
Sounds like the startUpmethod is executed before the Auto object is placed into the Ambiente world instance. At any rate, you should not be setting a value to that field until the Auto object has a route number and has begun rolling (the best time to assign a value to it is when 'haybasura' returns true).
joandvgv joandvgv

2015/2/19

#
Ok! So, in my superclass the starUp method initializes the ambiente (which is an instance of a the world subclass) like this:
Class auto extends actor(){
//some code here
Ambiente ambiente; 
//some code here
  public void startUp(){
     ambiente = (Ambiente).getWorld(); //not sure if in the code it's like this (don't have it to check right now, but sure it's pretty close)  
   //some more initializations 
}
Then, in order to implement what you're saying. I thought of something like this:
//subclass Recolector (truck)
     //some code here
     public void act(){
        //some code
       if ((haybasura() && empezarrecoleccion()){
           punto=ambiente.getPunto();
           cantidadbasura=punto.getCantidad();
          
      }
Would this work? I'm not clear about the references in greenfoot (first to the world in order to get the actor)
danpost danpost

2015/2/19

#
In the punto class, you only need one of the two fields (you can remove 'cantidad' -- line 4 above).
joandvgv joandvgv

2015/2/19

#
danpost wrote...
In the punto class, you only need one of the two fields (you can remove 'cantidad' -- line 4 above).
Yes! My mistake. The idea was returning cantidad in order to make the code more clear. About the implementation in my previous post, would that work?
danpost danpost

2015/2/19

#
First, at the time you set 'ambiente', 'getWorld' is null. The auto is not yet in a world at that time. So, you are not setting 'ambiente' to any valid world value. At any rate, going to the world to get the Punto object would be quite a round-about way of doing it (going to the world to get information about the auto itself -- which Punto object it is currently at). Remove the 'startUp' method. Like I stated previously, you only need to get a Punto object when one is encountered (when 'haybasura' returns a true value). You can add a 'getPunto' method in the Auto (or in the Recolector) class that looks like this:
protected Punto getPunto()
{
    return (Punto)getOneObjectAtOffset(0, 0, Punto.class);
}
You will not need a 'private Punto punto' field in the class; just use local variables when needed:
// inside any method (at any time 'haybasura' returns a true value)
Punto punto = getPunto();
// or
int randomNum = getPunto().getCantidad();
Your 'if' condition can then be reduced to this:
if ( haybasura() && getPunto() != malPunto && empezarreColeccion())
joandvgv joandvgv

2015/2/19

#
Excuse me, why are the differences of Punto and malPunto? I don't get the porpouse of malPunto. If haybasura() is true, it is because it found garbage (punto). getPunto returns the punto itself so I can get its quantity. But I don't get what you're trying to do with malPunto.
danpost danpost

2015/2/19

#
I was using 'malPunto' to hold a Punto object that had too large a size for the Recolector object. Now that you have the size of the Punto objects set in the Punto objects themselves, that field may be obsolete (as well as that check), provided you are still using the 'necesitacarro' field.
joandvgv joandvgv

2015/2/19

#
danpost wrote...
I was using 'malPunto' to hold a Punto object that had too large a size for the Recolector object. Now that you have the size of the Punto objects set in the Punto objects themselves, that field may be obsolete (as well as that check), provided you are still using the 'necesitacarro' field.
Ok! So now, there's no 'malPunto' in the code and it works perfectly. I'm giving it some runs in order to see that everything it's fine.
There are more replies on the next page.
1
2
3
4
5
6
7