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
joandvgv joandvgv

2015/2/18

#
danpost wrote...
By the way, all Recolector objects ARE also Auto type objects, because Recolector is a subclass of Auto.
Ok, well. It's my first time programming orientated to objects. This makes me feel confused. Can i create recolector objects without a field for it? I mean, just have a field for auto and then i create these recolectors from it?
joandvgv joandvgv

2015/2/18

#
By the way, the code you suggested did work. How can I show my gratitude? I mean, how can I +1 you or something like that? In the discussion forum theres no way to upvote.
danpost danpost

2015/2/18

#
No need for gratitude; but, if you want you can click on my user image icon and choose to follow (which makes you a fan; however, I have plenty). You could also, look at some of my scenarios and vote on the ones you like. As far as creating object with/without fields: you do not need fields to add new objects into your world at all. In other worlds, without 'Auto auto' and 'Recolector recolector', you can still do this:
addObject(new Recolector(), 38, 702);
The coded line does use the field 'Recolector recolector'; there is no reference to 'recolector' within the line..
joandvgv joandvgv

2015/2/18

#
danpost wrote...
No need for gratitude; but, if you want you can click on my user image icon and choose to follow (which makes you a fan; however, I have plenty). You could also, look at some of my scenarios and vote on the ones you like. The coded line does use the field 'Recolector recolector'; there is no reference to 'recolector' within the line..
Ok then! I'll do it. I have another question about the code you suggested. It worked with one actor if i place it into my World subclass constructor. Like this:
   despacharRecolector(1);
   despacharRecolector(2);
   despacharRecolector(3);
   despacharRecolector(4);
   despacharRecolector(5);   
But it just "dispatches" the first one. However, if I place within the act method it dispatches all of them but with the same route. What am I doing wrong? I know it has something to do with the order: 1 Placing it in the act method sets the same route value for all of them which is always 5. 2 Placing it in the Class cosntructor sets just a value which is the first one to the first truck and never sets it to the others.
danpost danpost

2015/2/18

#
You will not be able to dispatch them all at one time. The truck that is 'dispatched' needs to move before the next one is to be 'dispatched'. Otherwise, it will be 're-dispatched' (assigned a different route until that last route is assigned to it). That is also why they all get assigned the same route when dispatched from the act method. The solution is in the timer. The timer will give each one that is assigned a route a chance to move before the next truck is assigned a route.
// the timer field
private int dispatchTimer;
// in act
if (dispatchTimer % 500 == 0) despacharRecolector( dispatchTimer/500+1 ); // add truck every 500 acts
if (dispatchTimer == 2000) dispatchTimer = -1; // indicate timer has completed its course
if (dispatchTimer >= 0) dispatchTimer++; // increment timer if has not complete its course
joandvgv joandvgv

2015/2/18

#
Yes, of course. I forgot the timer. I had it in my "truck" class and thought it was going to do the work. I've forgot to change it to the world subclass! Thanks again :)
joandvgv joandvgv

2015/2/19

#
I didn't want to bother you, but it seems like I'm gonna need your help again. The thing did work, and the first five trucks starts 'rolling' when they should. But I couldn't make the DispatchAgain method work.
    private boolean despachardeNuevo(){
       if (((Recolector)getObjectsAt(831,63, Recolector.class).get(0)).getNecesitaCarro()){ //if the object at that point needs a new truck because its router was never completed. 
           return true;
       }
       else 
This didn't work at all. Was getting the IndexOutofBorders exception. then i decided to try with:
    private boolean despachardeNuevo(){
       if (((Recolector)getObjects(Recolector.class).get(0)).getNecesitaCarro()){ //if any of the objects need a new truck because its router was never completed. 
           return true;
       }
       else 
None of them worked. (Once it did work with the second one but if there were 3 trucks, all of them were dispatched when just one of them should) In the act method I got this:
public void act() 
    {
      generatePath();
       if ((despachoTimer % 500 == 0) && i<5 ){
           despacharRecolector( despachoTimer/500+1 ); // add truck every 500 acts  THIS WORKS!
           i++;
        }
       if (despachoTimer == 2000) despachoTimer = -1; // indicate timer has completed its course
       if (despachoTimer >= 0) despachoTimer++; // increment timer if has not complete its course
       if ((i>=5)) {
           if (despachardeNuevo()){ //THIS DOES NOT WORK. 
           despacharRecolector(((Recolector)getObjects(Recolector.class).get(0)).getnumRuta());
        }
       }
In the truck class everything seems to be fine. I've tried with some auxiliar variables and they worked. For example:
 if (capacidad<randomNum){
            necesitacarro=true; //this is true when it has to be true. 
            }
if (foundrelleno()){ //if found the end of the route
            if (necesitacarro=true) enviarcarro=true; /*if the truck at any point needed a car, at the end of its route he sends the information to the world. */
// this also was true only when it had to be. 
           descarga();
          }
And then I inform the world subclass that i need another truck:
   public boolean getNecesitaCarro(){
       return enviarcarro; //which is only true at the end of the route if at some point the truck needed a car.
    }
Note that at some point the truck wouldn't be able to clean-up the garbage because the garbage is not smaller than its capacity, but in any of the next points if the garbage is smaller, the truck can collect it. So, if at some point the truck didn't clean up, i save that information and at the end I send it to the world subclass So, it is sending the right information, but the World subclass does not receive it or process it as it should.
danpost danpost

2015/2/19

#
You do not need another method to dispatch a new truck. You can directly call 'despacharRecolector(routeNum)' method from the truck that could not finish. Something like this:
((Ambiente)getWorld()).despacharRecolector(routeNum);
Line 2 above in the middle code snippet, you have:
if (necesitacarra=true) ... 
You need a double equal sign '==' to compare for equality. Otherwise you are just setting the field to true and the 'if' block will always execute. Where is the information collected as far as what garbage was not picked up? (what field in what class, etc.) and how is the information supposed to be processed by the world? (show code you are trying to use)
joandvgv joandvgv

2015/2/19

#
Ok, well. If you're suggesting that I don't need another method, then I don't need that information to be processed by the world. Just in the same truck class,
if (capacidad<randomNum){ //if the truck capacity if smaller than the randomNum which is the amount of garbage.
           necesitacarro=true; //then it can't pick up the garbabe so another truck is needed to do so.
           }
When it reach the end of its route, then it should check if necesitacarro was ever true and then call the dispatch method:
      if (foundrelleno()){ //end of route
                descarga(); 
                if (necesitacarro==true){         
                     ((Ambiente)getWorld()).despacharRecolector(numruta);
                }
          }
All of this is in the act method of truck class. (Recolector)
danpost danpost

2015/2/19

#
As well as that line, you should probably reset the 'necesitacarra' field back to 'false' so that you do not continually dispatch new trucks:
if (necesitacarra) {
    ((Ambiente)getWorld()).despacharRecolector(numruta);
    necesitacarra = false;
}
danpost danpost

2015/2/19

#
What does the 'descarga' method do? Oh, sorry. I misspelled 'necesitacarro' in the previous post.
joandvgv joandvgv

2015/2/19

#
This does work. But not as it should. I mean, necesitacarro is always true. Like if always this condition is met
if (capacidad<randomNum){ 
           necesitacarro=true; 
           }
the act method in the truck class is this one:
 if (recogido){randomNum=Greenfoot.getRandomNumber(250);
          }
          if (pauseTimer<=0 && (numruta!=0) ){
            moverse(); //this is the method for turning depending on 'pasos'.
            move(1);
            pasos++;  //this is my guide for turnung
            pasosaux++; //this is for starting the route in different points. 
          }
          if(haybasura() && (capacidad>randomNum) && empezarrecoleccion()) { /*if theres garbage, it can pick up because capacity is bigger than the amount of garbage and it can start picking up*/
            pauseTimer=361;
             recoger(); //pick up
          }
          if (pauseTimer > 0) pauseTimer -= 2; 
          if (pauseTimer < 0 && getOneObjectAtOffset(0, 0, Punto.class) == null) {
           pauseTimer = 0; // stops picking up
          }
          if (foundrelleno()){
             if (necesitacarro==true) 
             ((Ambiente)getWorld()).despacharRecolector(numruta);
              descarga();
          }
          if (capacidad<randomNum){ //this condition is always met, why?
            necesitacarro=true;
            }
joandvgv joandvgv

2015/2/19

#
danpost wrote...
What does the 'descarga' method do? Oh, sorry. I misspelled 'necesitacarro' in the previous post.
Dont worry. I just placed descarga below the dispatch call as you could see in the previous post because I was getting NullPointerException. Descarga just sends the amount of garbage cleaned to a container and then delete the truck.
   private void descarga()
    {
       if (relleno!=null){ //relleno= container
       relleno.descargar(capacidad);
       eliminar(); //call to delete the truck

    }
   }
       
    private void eliminar()
   {
      if (isTouching(Relleno.class)) {
       getWorld().removeObject(this);
      }
   }
   
danpost danpost

2015/2/19

#
You should call 'descarga' last, then:
if (foundrelleno()){ //end of route
    if (necesitacarro) ((Ambiente)getWorld()).despacharRecolector(numruta);
    descarga(); 
}
EDIT: Just saw your last post (as you can see, I saw that as a problem).
joandvgv joandvgv

2015/2/19

#
Yes, now the problem is that necesitacarro is always true because the condition seems to always met. Any guess?
There are more replies on the next page.
1
2
3
4
5