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.

5
6
7
8
joandvgv6 joandvgv6

2015/2/21

#
Ok, well. This did work. But I found no way to set the same image. I mean, the logic to set the image was numruta-1. But now numruta is 7 because it will follow a different path. So, How do I set the same image that the deleted car had?
danpost danpost

2015/2/21

#
I was trying to think up a solution to the problem, and each attempt seemed to have one problem or another (that is, without totally restructuring your code). I think the main problems were caused by your fields -- insufficient in some areas, excessive in other, and placements thereof. I will see what I can conjure up to resolve this.
joandvgv6 joandvgv6

2015/2/22

#
Ok, well. I think I solved it, at least it works... Since the object is not removed after the other is created, i still can get information from the one whos going to be removed. Said this, the 'crear' method is right know like this:
 private void crear()
   {
         int mantener=numruta; //don't really know if I can use numruta here, so I use this. 
         String[] colors = { "azul", "cyan", "amarillo", "verde", "rojo" };  
         Recolector recolectorContinuado = new Recolector();
         getWorld().addObject(recolectorContinuado, 963, 265);
         recolectorContinuado.setnumRuta(7);
         recolectorContinuado.setRotation(90);
         recolectorContinuado.setImage("camion"+colors[mantener-1]+".png");
        }
The 'moverse' method is currently like this:
   
     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" : "";
           if (numruta!=7) setImage("camion"+colors[numruta-1]+suffix+".png"); //because if numruta==7, it will throw an Index Borders exception
           if (numruta==7){
               GreenfootImage img=getImage(); // there is just one turn, so I don't do this upon a condition.
               img.mirrorVertically();
            }
            pasos = 0;
            progreso++;
            j++;
      }
    }
danpost danpost

2015/2/22

#
joandvgv wrote...
Since the object is not removed after the other is created, i still can get information from the one whos going to be removed.
Actually, you can get any information from the removed object up to the point the act method completes its execution. The problem with 'getWorld' is that from the statement that removes the actor from the world and on it will return a null value (you lose the reference to the world object unless you previously assigned it to a reference variable or field). Also, once the object is removed (unless placed back into a world), you cannot, without exception, execute any method that requires the actor be in the world (collision checking and location commands). Field values however will still be accessible. As a sidenote, I should mention that if you had a reference to the actor, let us say in your world subclass -- held in a field, you would have access to the 'public' fields and methods of that actor, whether in the world or not.
joandvgv joandvgv

2015/2/22

#
Take a look at my scenario I don't want that to happen. What do you suggest? The thing about 180 rotation when its sense is up all over the scenario works. But when its sense is down, it does not. What do I need to change?
danpost danpost

2015/2/22

#
Alright. I hope I can explain this properly and fully. The first thing that I found was that the 'act' method of the Auto class was obsolete (you have act methods in both subclasses and neither of them use 'super.act' within them; and, also, no Auto object is being directly created -- only instances of its subclasses are created). Next I realized that the 'moverse' method in the Auto class was intended just for Inspector objects. This was a clue that it was currently misplaced and should be moved to the Inspector class. This method, however, is flawed in its logic as far as what image to use at certain times. Anyways, to start remove the 'act' and the 'moverse' methods from the Auto class. In the Inspector class, add the following 'act' and 'moverse' methods:
public void act() 
{
    startUp();
    if (numruta != 0)
    {
        moverse();
        move(1);
        pasos++;
    }
}

public void moverse()
{
    if (pasos == valor[j])
    {
        String directions = Ruta.direcciones[5][progreso];
        if ("Left".equals(directions)) turn(-90);
        else if ("Right".equals(directions)) turn(90);
        if (getRotation() == 180) setImage("camionleft.png"); else setImage("camion.png");
        pasos = 0;
        progreso++;
        j++;
    }
}
You had a 'progresoauto' field in the Auto class and a 'progreso' field in the Recolector class. I changed the name (in the code itself) of the field in the Inspector class from 'progresoauto' to 'progreso'. So now you can remove 'private int progreso;' from the Recolector class and change 'private int progresoauto;' in the Auto class to 'protected int progreso;'. It should now compile and your Inspector should not be upside down at any time.
joandvgv joandvgv

2015/2/22

#
Wow. Thank you so much for those explanations. Really really helped, since this is my first time programming in Java and I decided to use greenfoot, there are a lot of things that are a bit difficult to understand like reference fields and so, now I understand more about this stuff.
joandvgv wrote...
Take a look at my scenario I don't want that to happen. What do you suggest? The thing about 180 rotation when its sense is up all over the scenario works. But when its sense is down, it does not. What do I need to change?
This is what I previously published yesterday and it got posted today while I was sleeping. Will try that the code you said and I think it will be the last thing im gonna need before I submit my project to the teacher.
joandvgv6 joandvgv6

2015/2/22

#
Wow. Thank you so much for those explanations. Really really helped, since this is my first time programming in Java and I decided to use greenfoot, there are a lot of things that are a bit difficult to understand like reference fields and so, now I understand more about this stuff.
joandvgv wrote...
Take a look at my scenario I don't want that to happen. What do you suggest? The thing about 180 rotation when its sense is up all over the scenario works. But when its sense is down, it does not. What do I need to change?
This is what I previously published yesterday and it got posted today while I was sleeping. Will try that the code you said and I think it will be the last thing im gonna need before I submit my project to the teacher.
You need to login to post a reply.
5
6
7
8