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.

3
4
5
6
7
8
joandvgv joandvgv

2015/2/20

#
danpost wrote...
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++;
}
Please, could yo explain me what line 5 does? This does not change the image though it should. Rotation ==180 when it has turned left twice in a row. but it does not change the image.
joandvgv joandvgv

2015/2/20

#
I realized why this didn't work. At the beginning of the act method i set the image and when the counter if a!=2, it would do it again. (if it was 2 it should change the image)
danpost danpost

2015/2/20

#
Line 5 can be expanded to show what it does more clearly. You could replace line 5 with the following and get the same result:
String suffix = null;
if (getRotation() == 180)
{
    suffix = "left";
}
else
{
    suffix = "";
}
The 'suffix' variable is used on line 6 to create the String of the image filename to be set to the actor.
joandvgv joandvgv

2015/2/20

#
danpost wrote...
Line 5 can be expanded to show what it does more clearly. You could replace line 5 with the following and get the same result:
String suffix = "";
if (getRotation() == 180) suffix = "left";
The 'suffix' variable is used on line 6 to create the String of the image filename to be set to the actor.
Great. Now I need to adapt this coding to Auto class. The Auto Class Movement is way different. In this case the Actor goes up and goes down. So, when it is going up, this works. But when it is going down, the things change a bit and now the reverse effect we forced when it was going up, makes the auto actor looks upside down again.
joandvgv joandvgv

2015/2/21

#
Still need help with this.
danpost danpost

2015/2/21

#
joandvgv wrote...
Still need help with this.
What do you have?
joandvgv joandvgv

2015/2/21

#
It is kind a bit messed up and really bad programming. But I just was trying to see what happens.
 public void moverse(){
      directions=Ruta.direcciones[5][progresoauto];
      //  System.out.print(aux.get(1));
        if(pasos == valor[j])
        {
            if ((directions=="Left") && (progresoauto<15))
            {
                turn(-90);
                x++;
                if (x>1){
                    myImage=new GreenfootImage("camionleft.png");
                    setImage(myImage);
                }
            }
            else if ((directions=="Right") && (progresoauto<15))
            {
                turn(90);
                x=0;
                myImage= new GreenfootImage("camion.png");
                setImage(myImage);
            }
            pasos = 0;
            progresoauto++;
            j++;
            
            if (directions=="Left" && (progresoauto>15) && (progresoauto<27) ) 
            {
                turn(-90);
                x++;
                if (x>1){
                    myImage=new GreenfootImage("camion.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && (progresoauto>15) && (progresoauto<27) )
            {
                turn(90);
                x=0;
                myImage= new GreenfootImage("camionleft.png");
                setImage(myImage);
            }
           if (directions=="Left" && progresoauto>=27 && progresoauto<31){
               turn(-90);
               x++;
               if (x>1){
                    myImage=new GreenfootImage("camion.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && progresoauto>=27 && progresoauto<31){
                turn (90);
                x=0;
                myImage=new GreenfootImage("camionleft.png");
                setImage(myImage);
                }
          
              if (directions=="Left" && progresoauto>=31 && progresoauto<36){
               turn(-90);
               x++;
               if (x>1){
                    myImage=new GreenfootImage("camion.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && progresoauto>=31 && progresoauto<36){
                turn (90);
                x=0;
                myImage=new GreenfootImage("camionleft.png");
                setImage(myImage);
                }
                
              if (directions=="Left" && progresoauto>=36 && progresoauto<39){
               turn(-90);
               x++;
               if (x>1){
                    myImage=new GreenfootImage("camionleft.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && progresoauto>=36 && progresoauto<39){
                turn (90);
                x=0;
                myImage=new GreenfootImage("camion.png");
                setImage(myImage);
                }
            
            if (directions =="Right"&& (progresoauto==39 || progresoauto==40)){
                 turn(90);
                x=0;
                myImage= new GreenfootImage("camionleft.png");
                setImage(myImage);
           }
        }
    }
This works, but not completely well. It does what it has to do. The object looks upside down just twice of the 40 times it has to turn. As you suggested the previous code to make it better, I think there's a better way to do this. What i want to do: Same as the your suggested code but in addition, reverse the condition if the object is going down in the scenario.
danpost danpost

2015/2/21

#
I do not know what the big deal is. If the actor is going down, its rotation will be 90. Just change the condition to use "left" for the suffix if the rotation is 90 (using 'x' to try and track which image you should use obviously is not working -- at least, not completely)
joandvgv joandvgv

2015/2/21

#
I'll try this and let you know if i figured it out. ============================ In my scenario there's one counter. Which actually every truck uses. I need it to show at any time the truck capacity. But I don't really know how to add a counter for each truck. I tried to add 5 objects, they were added to the scenario. Cool In my counter class, I created 5 method for changing what the counter shows. This method will be called depending on the route number of the truck. The problem is getting the object in the truck class in order to make it work.
  private Informacion getContadores(){
     if (numruta!=0){
       switch (numruta){
                case 1: return (Informacion)getOneObjectAtOffset(960, 658, Informacion.class);
                case 2: return (Informacion)getOneObjectAtOffset(960, 676, Informacion.class);
                case 3: return (Informacion)getOneObjectAtOffset(960, 694, Informacion.class);
                case 4: return (Informacion)getOneObjectAtOffset(960, 712, Informacion.class);
                case 5: return (Informacion)getOneObjectAtOffset(960, 730, Informacion.class);
              }
     }
    return (Informacion)getOneObjectAtOffset(0, 0, Informacion.class); 
 }
Obviously, adding the field
Informacion information
and getting it initialized like this:
information = getContadores();
But any time I try to use informacion.anymethod(cantidad); Got Null Pointer Exception.
danpost danpost

2015/2/21

#
The trucks move around and the coordinates at the offset from where the truck is, ( getX()+960, getY()+??? ), is going to change. If you want to get an object that is at specific coordinates, (960, ???), then use 'getWorld().getObjectsAt(960, ???, Informacion.class).get(0)'. Again the method above can be simplified to one line.
private Informacion getContadores(){
    return (Informacion)getWorld().getObjectsAt(960, 658+18*(numruta-1), Informacion.class).get(0);
}
BTW, I would like to see the code for the class of your counters. It seems strange that you would have 5 different methods in the class, one for each counter.
joandvgv joandvgv

2015/2/21

#
danpost wrote...
BTW, I would like to see the code for the class of your counters. It seems strange that you would have 5 different methods in the class, one for each counter.
It is the same method. So, now that I think, if i'm adding different objects, it should just be one method. Thanks for the observation. And thanks for the simplification, i do need to realize this things. By the way. Which one is the best way to dispatch the auto truck after all the trucks have been already dispatched? I actually have this. But it's kinda giving me some troubles. Don't know what. Sometimes it works, sometimes it does not. I'm kinda starting to thing that if the simulation speed is a bit acelerated it would never work.
public void act() 
   {
      if (numero!=(100)){
        if ((despachoTimer % frecuencia == 0) && i<5 ){
           despacharRecolector( despachoTimer/frecuencia+1 ); // add truck every 500 acts or whatever the frequency is
           i++;
        }
       if (despachoTimer == (frecuencia*4)) despachoTimer = -1; // indicate timer has completed its course
       if (despachoTimer >= 0) despachoTimer++; // increment timer if has not complete its course
       if (i>=5 && i<=200){ 
           i++;
        } 
       if (i==201){ despacharAuto();
           i++;
        }
           
      }
    }
    public void despacharRecolector(int rteNum){
      if ( ! getObjectsAt(38, 702, Recolector.class).isEmpty())
       {
        ((Recolector)getObjectsAt(38, 702, Recolector.class).get(0)).setnumRuta(rteNum);
      }
    }
    public void despacharAuto(){
        if ( ! getObjectsAt(38, 702, Auto.class).isEmpty())
      {
        ((Auto)getObjectsAt(38, 702, Auto.class).get(0)).setnumRuta(6);
      }
      
    }
danpost danpost

2015/2/21

#
joandvgv wrote...
I'm kinda starting to thing that if the simulation speed is a bit acelerated it would never work.
The simulation speed will not alter anything unless real time is involved (act cycles are act cycles regardless of scenario speed). I do not have a full picture of what is happening. For one thing, the 'numero' field is quite 'fuzzy' to me. You use it to regulate the actions, but I do not know what controls you have on its value (or how the field is set up and what it is used for). I am not sure why you decided to use a value of 6 for the Auto object's route number. I would think zero would work as a suitable value. I am still unclear as to how your Recolector and Auto classes are designed to work together (I have seen very little of what you have in the Auto class up to this point). If you posted both of those classes in their entirety as they are now, I might be able to get a better idea of what is going on there (I am not even sure if creating an Auto object is the best way to add the 'route inspector auto'; it may be better to subclass Auto with another class for that).
joandvgv joandvgv

2015/2/21

#
danpost wrote...
I am not sure why you decided to use a value of 6 for the Auto object's route number. I would think zero would work as a suitable value.
I think of this. But the route number 0 is made to make the auto unable to move as you suggested I'll show you what I actually have in both classes:
import greenfoot.*;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;

public class Recolector extends Auto
{
    public int cantidad;
    public int capacidad=1000;
    public int randomNum=0;
    private int pauseTimer=0;
    private int timer=0;
    private int n=0;
    private boolean empezar=false;
    private boolean necesitacarro=false;
    private boolean enviarcarro=false;
    private int t=0;
    private int progreso=0;
    private String direction;
    private int rotation;
    private int norepetir=0;
    //private int valor=0;
    public void act() 
    {
            startUp();
            darvalores(numruta);
       if (!empezarRecoleccion()){
         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;
            }
          }
        }
        move(0);           
         
          if (pauseTimer<=0 && (numruta!=0) ){
            moverse();
            move(1);
            pasos++;
            pasosaux++;
          }
          if ( haybasura() && empezarRecoleccion()) {
             if (capacidad<randomNum) {
                  necesitacarro = true;
             }
            else {  
            pauseTimer=361;
            recoger(); //pick up
            norepetir=0;
            }
          }
          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);
                estadistica.cEu();
             }
             descarga();
          }
          if (necesitacarro==true && norepetir==0){
              estadistica.nVbNr();
              norepetir=1;
            }
           
      }
  
   public void alert(int num){
       return;
    }
    
   private boolean haybasura()
    {
        Actor punto = getOneObjectAtOffset(0, 0, Punto.class);
        if(punto != null) {
            randomNum = getPunto().getCantidad();
            return true;
        }
        else {
            return false;
        }
    }
    private void recoger()
    {
        Actor punto = getOneObjectAtOffset(0, 0, Punto.class);
        if(punto != null) {
            //Greenfoot.delay(retraso);
            getWorld().removeObject(punto);
            capacidad =  capacidad - randomNum; 
            switch (numruta){
                case 1: {informacion.contador1(capacidad);
                    break;}
                case 2: {informacion.contador2(capacidad);
                    break;}
                case 3: {informacion.contador3(capacidad);
                    break;}
                case 4: {informacion.contador4(capacidad);
                    break;}
                case 5: {informacion.contador5(capacidad);
                    break;}
                default: break;
              }
            infoparada.contador(randomNum);
            estadistica.cPrC();
        }
    }   
         
   private boolean foundrelleno()
   {
      Actor relleno = getOneObjectAtOffset(0, 0, Relleno.class);
       if(relleno != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    
   private void descarga()
    {
     //   Actor recolector = getOneObjectAtOffset(0, 0, Recolector.class);
        if (relleno!=null){
       relleno.descargar(capacidad);
       eliminar();

    }
   }
       
    private void eliminar()
   {
      if (isTouching(Relleno.class)) {
       // Actor actor = getOneIntersectingObject(Recolector.class); 
       getWorld().removeObject(this);
      }
   }
   
   private boolean empezarRecoleccion(){
       switch (numruta){
            case 1: {
                if (pasosaux>=481) return true;
                break;
            }
            case 2: {
                if (pasosaux>=36) return true;
                break;
            }
            case 3: {
                if (pasosaux>=200) return true;
                break;
            }
            case 4: {
                if (pasosaux>=540 && (progreso!=8)) return true;
                break;
            }
            case 5: {
               if (pasosaux>=400) return true;
                break;
            }
            default: {
                break;
        }
    }
     return false;
   }  

  public int getRuta(){
      return numruta;
    }
  public boolean getHola(){
      return empezarRecoleccion();
    }
  public void setNumRuta(int numRuta){
      numruta=numRuta;
    }
    
   public boolean getNecesitaCarro(){
       return enviarcarro;
    }
    
     public void moverse(){
     direction = (Ruta.direcciones[numruta-1][progreso]);
       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++;
      }
    }
      protected Punto getPunto()
    {
    return (Punto)getOneObjectAtOffset(0, 0, Punto.class);
   }
   
    public void darvalores(int numruta){
        switch (numruta){
        case 1: {   
                valor[1]=(820);
                valor[2]=(359);
                valor[3]=(652);
                valor[4]=(280);
                break;
            }
        case 2: {
                valor[1]=(36);
                valor[2]=(75);
                valor[3]=(230);
                valor[4]=(66);
                valor[5]=(230);
                valor[6]=(66);
                valor[7]=(230);
                valor[8]=(71);
                valor[9]=(258);
                valor[10]=(225);
              
               break;
            }
         case 3: {
                valor[1]=(340);
                valor[2]=(220);
                valor[3]=(398);
                valor[4]=(66);
                valor[5]=(292);
                valor[6]=(280);
              
               break;
            }
          case 4: {
                valor[1]=(585);
                valor[2]=(66);
                valor[3]=(335);
                valor[4]=(360);
                valor[5]=(830);
                valor[6]=(144);
                valor[7]=(100);
                valor[8]=(66);
               
              
               break;
            }
           case 5: {
                valor[1]=(422);
                valor[2]=(140);
                valor[3]=(503);
               
              
               break;
            }
    }
}     
  protected Ruta getRutas()
    {
    return (Ruta)getOneObjectAtOffset(0, 0, Ruta.class);
   }   
 }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import java.util.List;
/**
 * Escribe una descrición de la clase Automovil aquí.
 * 
 * @autor (tu nombre) 
 * @versión (Un número de versión o una fecha)
 */
public class Auto extends Actor
{          
    private int ID = 0;
    GreenfootImage myImage;
    public int retraso;
    private int i = 1;
    protected int j=1;
    public double VEL = 1.0;
    public int pasos= 0;
    private int progresoauto=0;
  //  protected int numruta = (1 + Greenfoot.getRandomNumber(5 - 1 + 1));
    protected int numruta; 
    protected int[ ] valor = new  int[100];
    public int cantidadbasura;
    public Punto punto;
    public Informacion informacion;
    public Infoparada infoparada;
    public Recolector recolector;
    public Relleno relleno;
    public Ruta ruta;
    public Estadistica estadistica;
    public int capacidad;
    public int numcarros;
    public int x=0;
   // public Reloj reloj;
    protected int pasosaux=0;
    private String directions;
    /**
     * Act - hace lo que Automovil quiere hacer. Este método se llama "cuando quiera" o whenever
     * los botones 'Actuar or 'Ejecutar' son presionados en el entorno.
     */

   public void startUp(){
       Ambiente ambiente = (Ambiente)getWorld();
       Ruta ruta = ambiente.getRuta(); 
       if (numruta!=0) informacion = ambiente.returnCounter(numruta);
       infoparada=ambiente.getInfoparada();
       recolector = ambiente.getRecolector();
       relleno = ambiente.getRelleno();
       estadistica = ambiente.getEstadistica();
       //path = ambiente.getPath();
       ruta = ambiente.getRuta();
       numcarros=ambiente.getCantidad();
      if (numruta==6) darvalores();
       

    }
    
    public void act() 
    {
        startUp();
        if (numruta!=0){
          moverse();
          move(1);
          pasos++;
       }
       }
    
    public void darvalores(){
         valor[1]=(36);
         valor[2]=(75);
         valor[3]=(230);
         valor[4]=(66);
         valor[5]=(230);
         valor[6]=(66);
         valor[7]=(230);
         valor[8]=(71);
         valor[9]=(258);
         valor[10]=(225);
         valor[11]=(89);
         valor[12]=(66);
         valor[13]=(109);
         valor[14]=(66);
         valor[15]=(841);
         valor[16]=(66);
         valor[17]=(659);
         valor[18]=(66);
         valor[19]=(659);
         valor[20]=(66);
         valor[21]=(659);
         valor[22]=(77);
         valor[23]=(272);
         valor[24]=(66);
         valor[25]=(300);
         valor[26]=(78);
         valor[27]=(400);
         valor[28]=(220);
         valor[29]=(73);
         valor[30]=(150);
         valor[31]=(397);
         valor[32]=(72);
         valor[33]=(233);
         valor[34]=(72);
         valor[35]=(350);
         valor[36]=(425);
         valor[37]=(480);
         valor[38]=(72);
         valor[39]=(480);
         valor[40]=(491);         
         
     }
   

 public void setRuta (int valorRuta) {

        numruta = valorRuta;

    } //Cierre del método
    
  public int getnumRuta(){
      return numruta;
    }    
    
  public void moverse(){
      directions=Ruta.direcciones[5][progresoauto];
      //  System.out.print(aux.get(1));
        if(pasos == valor[j])
        {
            if ((directions=="Left") && (progresoauto<15))
            {
                turn(-90);
                x++;
                if (x>1){
                    myImage=new GreenfootImage("camionleft.png");
                    setImage(myImage);
                }
            }
            else if ((directions=="Right") && (progresoauto<15))
            {
                turn(90);
                x=0;
                myImage= new GreenfootImage("camion.png");
                setImage(myImage);
            }
            pasos = 0;
            progresoauto++;
            j++;
            
            if (directions=="Left" && (progresoauto>15) && (progresoauto<27) ) 
            {
                turn(-90);
                x++;
                if (x>1){
                    myImage=new GreenfootImage("camion.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && (progresoauto>15) && (progresoauto<27) )
            {
                turn(90);
                x=0;
                myImage= new GreenfootImage("camionleft.png");
                setImage(myImage);
            }
           if (directions=="Left" && progresoauto>=27 && progresoauto<31){
               turn(-90);
               x++;
               if (x>1){
                    myImage=new GreenfootImage("camion.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && progresoauto>=27 && progresoauto<31){
                turn (90);
                x=0;
                myImage=new GreenfootImage("camionleft.png");
                setImage(myImage);
                }
          
              if (directions=="Left" && progresoauto>=31 && progresoauto<36){
               turn(-90);
               x++;
               if (x>1){
                    myImage=new GreenfootImage("camion.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && progresoauto>=31 && progresoauto<36){
                turn (90);
                x=0;
                myImage=new GreenfootImage("camionleft.png");
                setImage(myImage);
                }
                
              if (directions=="Left" && progresoauto>=36 && progresoauto<39){
               turn(-90);
               x++;
               if (x>1){
                    myImage=new GreenfootImage("camionleft.png");
                    setImage(myImage);
                }
            }
            else if (directions=="Right" && progresoauto>=36 && progresoauto<39){
                turn (90);
                x=0;
                myImage=new GreenfootImage("camion.png");
                setImage(myImage);
                }
            
            if (directions =="Right"&& (progresoauto==39 || progresoauto==40)){
                 turn(90);
                x=0;
                myImage= new GreenfootImage("camionleft.png");
                setImage(myImage);
           }
        }
    }
    public void setnumRuta(int num){
      numruta=num;
    }
    
    public void setPasos(int num){
        pasos=num;
    }
    
 }       
        
danpost danpost

2015/2/21

#
Also, try this:
public void moverse()
{
    directions = Ruta.direcciones[5][progresoauto];
    if (pasos == valor[j])
    {
        if ("Left".equals(directions)) turn(-90); else turn(90);
        String suffix = getRotation() == 180 ? "left" : "";
        setImage("camion"+suffix+".png");
        pasos = 0;
        progresoauto++;
        j++;
    }
}
joandvgv joandvgv

2015/2/21

#
Let me guess what it's happening here. All the mess is because when I'm dispatching an Auto i'm also dispatching a Recolector. That makes it a whole problem. So, the best way to go would be adding another subclass inspector that would be just the same as Actor. And instead of dispatching an Auto (an also a recolector) I would be dispatching an inspector and there would be no mess. Right?
There are more replies on the next page.
3
4
5
6
7
8