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.

2
3
4
5
6
7
8
joandvgv joandvgv

2015/2/19

#
However, I'm still messed up with the refereces. For example, I've designed the project using UML and the class route had to contain the information about the routes (the lists you previously suggested to be changed to arrays) but I couldn't get that information from the route class, I had to do it from the world class. And in this case getOneObjectAtOffset would not work. Would it?
danpost danpost

2015/2/19

#
joandvgv wrote...
However, I'm still messed up with the refereces. For example, I've designed the project using UML and the class route had to contain the information about the routes (the lists you previously suggested to be changed to arrays) but I couldn't get that information from the route class, I had to do it from the world class. And in this case getOneObjectAtOffset would not work. Would it?
Why wouldn't it? You do place all the Punto objects in the world before the trucks start out, do you not? It is an Actor object and in your 'haybasura' method, you use the same thing !!
joandvgv joandvgv

2015/2/19

#
Ok then, Ill use the same but instead of Punto, gonna use the Class route . But you suggested this code for saving the directions.
public String[][] rutas = { { "Left", "Left", /** etc */ }, // route 1 directions
                            { /** ... */}, // route 2 directions
                            // ...
                            { /** ... */ } // route 5 directions
};
How would I get it from the truck class? My guess is:
//class ruta
public String[][] getDirections(){
   return rutas 
In class truck:
//truck class

private String[][] rutas;
// at some part of the code
rutas=getRuta().getDirections();

//and the method getRuta:

  protected Ruta getRuta()
    {
    return (Ruta)getOneObjectAtOffset(0, 0, Ruta.class);
   }
but then how do I switch between different routes of the array? Would it be something like this?:
ruta[1][progreso]=="Left"{ //Indicates the first movement of the route 1
    //some code
 }

and ruta[2][progreso]=="Left"{ //indicates the first movement of the route 2
   // some code
}
danpost danpost

2015/2/19

#
Since the routes will be constant, you can make them 'public static final'. Then, if in the Recolector class, you can use 'Recolector.rutas' from any class.
danpost danpost

2015/2/19

#
The declaration/assignment statement would look like this:
public static final String[][] rutas =
{
    { "Left", "Left", ... }, // first route
    { "Right", "Left", ... }, // second route
    // etc.
    { ... } // fifth route
};
If you declare/assign it in the Route class, you would use 'Route.rutas' to get any point on any route.
joandvgv joandvgv

2015/2/19

#
danpost wrote...
Since the routes will be constant, you can make them 'public static final'. Then, if in the Recolector class, you can use 'Recolector.rutas' from any class.
You meant from any method?
danpost danpost

2015/2/19

#
No, I meant from any class. However, the output of the post was incomplete. I was about to edit the post to this: The declaration/assignment statement would look like this:
public static final String[][] rutas =
{
    { "Left", "Left", ... }, // first route
    { "Right", "Left", ... }, // second route
    // etc.
    { ... } // fifth route
};
If you declare/assign it in the Route class, you would use, for example:
String direction = Route.rutas[/** route number */][/** direction number */];
to get any specific direction on any route.
joandvgv joandvgv

2015/2/19

#
Ok. Punto is the Garbage Class. I need to get any point on any route but in the truck Class. So Should I declare an auxiliar array string to get the Route.rutas? EDIT: I just saw you edited it.
joandvgv joandvgv

2015/2/19

#
Im getting this exception:
java.lang.ArrayIndexOutOfBoundsException: 4
	at Recolector.moverse(Recolector.java:196)
	at Recolector.act(Recolector.java:63)
This is the entire moverse() code:
 public void moverse(){
       switch (numruta){
            case 1:  { direction=Ruta.direcciones[0][progreso]; //here's the exception. 
                break;
            }
            case 2: {direction=Ruta.direcciones[1][progreso];
                break;}
            case 3: {direction=Ruta.direcciones[2][progreso];
                break;}
            case 4: {direction=Ruta.direcciones[3][progreso];
                break;
            }
            case 5: {direction=Ruta.direcciones[4][progreso];
                break;
            }
        
        }
        if(pasos == valor[j])
        {
            if (direction==("Left"))
            {
                turn(-90); 
                a++;
                if (a>1){
                    switch (numruta){
            case 1: {
                  myImage= new GreenfootImage("camionazulleft.png");
                  setImage(myImage);
                break;
            }
            case 2: {
                  myImage= new GreenfootImage("camioncyanleft.png");
                  setImage(myImage);
                
                break;
            }
            case 3: {
                  myImage= new GreenfootImage("camionamarilloleft.png");
                  setImage(myImage);
             
                break;
            }
            case 4: {
                  myImage= new GreenfootImage("camionverdeleft.png");
                  setImage(myImage);           
                break;
            }
            case 5: {
                  myImage= new GreenfootImage("camionrojoleft.png");
                  setImage(myImage);
                break;
            }
            
          }
        }
            }
            else if (direction==("Right"))
            {
                turn(90);
                a=0;
                switch (numruta){
            case 1: {
                  myImage= new GreenfootImage("camionazul.png");
                  setImage(myImage);
                break;
            }
            case 2: {
                  myImage= new GreenfootImage("camioncyan.png");
                  setImage(myImage);
                
                break;
            }
            case 3: {
                  myImage= new GreenfootImage("camionamarillo.png");
                  setImage(myImage);
             
                break;
            }
            case 4: {
                  myImage= new GreenfootImage("camionverde.png");
                  setImage(myImage);           
                break;
            }
            case 5: {
                  myImage= new GreenfootImage("camionrojo.png");
                  setImage(myImage);
                break;
            }
        }
            }
            pasos = 0;
            progreso++;
            j++;
        }
      } 
    
Every truck by itself works just fine. When I launch the auto Object the problem seems to be appear.
joandvgv joandvgv

2015/2/19

#
I Think I solved the problem by adding one more direction to the array. For example: From this:
{ "Left", "Left", "Right", "Right" }, // route 1 directions
to this:
{ "Left", "Left", "Right", "Right", "Right"}, // route 1 directions
But I'm pretty sure that's not good programming.
danpost danpost

2015/2/20

#
joandvgv wrote...
I Think I solved the problem by adding one more direction to the array. For example: From this:
{ "Left", "Left", "Right", "Right" }, // route 1 directions
to this:
{ "Left", "Left", "Right", "Right", "Right"}, // route 1 directions
But I'm pretty sure that's not good programming.
No. it is not. Let us simplify things. Lines 2 through 17 in the 'moverse' method can be replaced with this;
direction = Ruta.direcciones[numruta-1][progreso];
Now, lines 20 and 57 are trying to see if two objects are the SAME object -- they might contain the same characters, but they are not the same String objects. To compare their character arrays, use the Object class 'equals' method; for example:
if ("Left".equals(direccion))
Curious. Line 23, in the code for "Left" direction, incrrements 'a' and then line 24 will only allow the setting of the image if 'a' is greater than one; at line 60, for "Right", 'a' is sets to zero and there is no condition on setting the image.
joandvgv joandvgv

2015/2/20

#
danpost wrote...
Curious. Line 23, in the code for "Left" direction, incrrements 'a' and then line 24 will only allow the setting of the image if 'a' is greater than one; at line 60, for "Right", 'a' is sets to zero and there is no condition on setting the image.
I will check that and let you know what is happening. About the other, using this
if ("Left".equals(direccion))
instead of what it is in lines 20 and 57 just does the same thing but one is good programming and the other is not, right? And one more question. Now that almost everything is over. I've created the 'statistics' class whose code is:
public class Estadistica extends Actor
{
     int nVbNr; // Number of times garbage is not picked up. 
     int cPrC; // Number of times garbage is picked up
     int cEu; // Número of extra trucks dispatched.
   
    public void setEstadisticas(){
      {
        GreenfootImage img = new GreenfootImage ("background.png");
        img.setColor (java.awt.Color.BLACK);
         
        img.drawString("Número de veces basura no se recogío: " + nVbNr, 10, 20);
        img.drawString("Cantidad de puntos de recolección culminados: " + cPrC, 10, 35);
        img.drawString("Número de camiones extra usados:  " + cEu,  10, 50);
        setImage(img);
      }
    }
    
    public void nVbNr(){
        nVbNr++;
    }
    public void cPrC(){
        cPrC++;
    }
    public void cEu(){
        cEu++;
    }
}
I'm not so sure about what the 'setEstadisticas' method actually does. but whatever it is, it's not working. The program is supossed to show the statistics after the simulation is over (which is controlled by a timer that works perfectly) The timer let the world subclass know when it's over. And when this method is called I'll call the setEstadisticas method.
 public void alert(){
       numero=100;
       estadistica.setEstadisticas();
      } 
joandvgv joandvgv

2015/2/20

#
About this:
danpost wrote...
Curious. Line 23, in the code for "Left" direction, incrrements 'a' and then line 24 will only allow the setting of the image if 'a' is greater than one; at line 60, for "Right", 'a' is sets to zero and there is no condition on setting the image
I just cheked and I'll tell you what's happening there. When the truck turns twice to Left, its image does it too. I don't want that to happen because then the truck would be upside down. So, my logic to solve it was: create an upside down image version and assign it when it has turned left twice. so the effect will be reversed. In order to know when the truck turn left twice in a row, I created a counter, but since it is possible to turn Left and then Right and then Left again, that would set the counter to 2 and the movements weren't twice in a row. In the end, I just restart the counter when it turns Right so I just get a>1 when the movement were twice in a row. If you know a best way to implement this, please let me know. Because the simulation got a truck that covers all the routes, so the truck isn't always going up. It goes up, then goes down and then goes up again. In this case is way too hard to implement what I explained you above.
joandvgv joandvgv

2015/2/20

#
EDIT Statistics is now solved. I read what you said yesterday about the references and I dind't create a Estadistica field. I just return the object i created so the Truck class could use it. Now the main issue is the thing about changing images. And look at the world subclass act method:
{
      if (numero!=(100)){ //numero==100 when simulation is over. 
        if ((despachoTimer % (500*frecuencia) == 0) && i<5 ){
           despacharRecolector( despachoTimer/(500*frecuencia)+1 ); // add truck every 500 acts
           i++;
        }
       if (despachoTimer == (500*frecuencia)) despachoTimer = -1; // indicate timer has completed its course
       if (despachoTimer >= 0) despachoTimer++; // increment timer if has not complete its course
       if (i==5){ 
         despacharAuto();
         i=6;
        } 
      }
    }
    public void despacharRecolector(int rteNum){
      if ( ! getObjectsAt(38, 702, Recolector.class).isEmpty())
       {
        ((Recolector)getObjectsAt(38, 702, Recolector.class).get(0)).setnumRuta(rteNum);
      }
    }
I am supossed to dispatch an Auto object- which moves around all the routes- at any time in the simulation (I choose after the 5 truck is dispatched) But sometimes (not always, just in some runs) The 5 truck is never dispatched but. The auto is dispatched and the truck (recolector) that is dispatched is with routeNum == 6.
danpost danpost

2015/2/20

#
This should simplify things a bit more. Replace lines 18 through 94 with the following:
if (pasos == valor[j])
{
    String[] colors = { "azul", "cyan", "amarillo", "verde", "rojo" };
    if ("Left".equals(direction)) turn(-90); else turn(90);
    String suffix = getRotation() == 180 ? "left" : "";
    setImage("camion"+colors[numruta-1]+suffix+".png";
    pasos = 0;
    progreso++;
    j++;
}
There are more replies on the next page.
2
3
4
5
6
7
8