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

2016/3/25

Controling one actor at a time

1
2
kidlivin kidlivin

2016/3/27

#
There's the code of the car class:
import greenfoot.*;
import java.util.List;


/**
 * Write a description of class Carro here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Carro extends Actor
{
    //Declaração de variáveis
    private int dirX, dirY; // sentido do carro para X e Y
    public char Key;//
    private int direction;
    public boolean carRemoved = false;
    private char Car;
    GreenfootImage img = new GreenfootImage("explosao.png");
    public static Actor controlledCar;
    public int nextDirection = 0;
    public int pontuação = 30;
    
   /**
     * Act - do whatever the Carro wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        if(Greenfoot.mouseClicked(this))
        {
            controlledCar = this;
        }
        if(controlledCar == this)
        {
            int dir = 0;
            if (Greenfoot.isKeyDown("a")) 
            {
                dir++;
            }
            
            if (Greenfoot.isKeyDown("d")) 
            {
                dir--;
            }
            
            if (dir != 0) 
            {
                nextDirection = dir;
            }
        }
        mostraScore();
        colisaocurvas();
        //selectCar();
        //checkKey();
        turnToHouse();
        foraEstrada();
        colisaoCasa();
        mostraScore();
        //listaCarrosPresentes = World.getObjects(Carro.class);
        colisaoentroncamentos();
        
            }  
   /**
       Método para velociade automática no carro
       */
    public void move()
    {
        move(-1);
    }
    
   /**
       Determina intersecção entre os carros e as curvas
       */
    public void colisaocurvas()
    {
        if(this.isTouching(Curva.class) && this.getX() == 185 && this.getY() == 399)
            {
                turn(-90);
            }
        else if(this.isTouching(Curva.class) && this.getX() == 41 && this.getY() == 542)
            {
                turn(90);
            }
        else 
            {
                carRemoved = true;
            }
    }
   /**
      Verifica a tecla pressionada e guarda numa variávél;
      */
   
       /**
       Determina para onde o carro deve virar consoante a última tecla pressionada
       */
   public void colisaoentroncamentos()
    {            
        if(this.isTouching(Entroncamento.class) && this.getX() == 185 && this.getY() == 542) 
            {
                turn(90*nextDirection);
            }
        else 
            {
                carRemoved = true;
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 41 && this.getY() == 255) 
            {
                turn(90*nextDirection);
            }
        else 
            {
                carRemoved = true;
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 256 && this.getY() == 255) 
            {
                turn(90*nextDirection);
            }
        else 
            {
                carRemoved = true;
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 542) 
            {
                turn(90*nextDirection);
            }
        else 
            {
                carRemoved = true;
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 326) 
            {
                turn(90*nextDirection);
            }
        else 
            {
                carRemoved = true;
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 183) 
            {
                turn(90*nextDirection);
            }
        else 
            {
                carRemoved = true;
            }
    }
    /**
       Método que faz os carros virarem para a sua casa
       */
   public void turnToHouse()
    {
        if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 820 && this.getY() == 326) 
            {
                turn(-90);
            }
        else if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 741 && this.getY() == 542) 
            {
                turn(-90);
            }
        else if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 675 && this.getY() == 183) 
            {
                turn(-90);
            }
        else if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 473 && this.getY() == 183) 
            {
                turn(90);
            }
        else 
            {
                carRemoved = true;
            }
   }
   /**
      Remove o veículo que chegou à casa
      */
   public void colisaoCasa()
    {
        if(this.isTouching(Casa.class) && this.getX() == 820 && this.getY() == 241)
        {
            getWorld().removeObject(this);
            
            }
        else if(this.isTouching(Casa.class) && this.getX() == 741 && this.getY() == 455)
        {
            getWorld().removeObject(this);
            
            }
        else if(this.isTouching(Casa.class) && this.getX() == 41 && this.getY() == 87)
        {
            getWorld().removeObject(this);
            
            }
        else if(this.isTouching(Casa.class) && this.getX() == 256 && this.getY() == 93)
        {
            getWorld().removeObject(this);
            
            }
        else if(this.isTouching(Casa.class) && this.getX() == 473 && this.getY() == 90)
        {
            getWorld().removeObject(this);
            
            }
        else if(this.isTouching(Casa.class) && this.getX() == 675 && this.getY() == 99)
        {
            getWorld().removeObject(this);
            
            }
        
        else
        {
            carRemoved = true;
        }
    } 
   /**
      Mostra a pontuação do utilizador quando este coloca o veículo na casa correta
      */
   public void mostraScore()
   {
       List<Pontuação> listaTexto = getWorld().getObjects(Pontuação.class);
       Pontuação textoScore = listaTexto.get(0);
       textoScore.escreve("" + pontuação);
   }
}
Image As you can see, in some road junctions we don't turn to left or right but to ahead.
danpost danpost

2016/3/27

#
kidlivin wrote...
And there's another problem: if that solution work( doing turn(90*nextdirection), in some road junctions the car just must turn right or ahead, and in case the player click the wrong key, we have a bug...
You currently are not restricting movement to just the controlled car. Are you wanting the other cars to move around un-controlled? or do you want the un-controlled cars to move forward until it cannot proceed further in the direction it was currently going -- and then just stop until clicked on before proceeding when directed?
kidlivin kidlivin

2016/3/27

#
All cars have forward movement. We have a car. We select the car with the mouse. Then, we click "a" or "d" to set the direction. Then, when the car arrivers at the road juction, it turns according with the key pressed. Then, the car just move forward because there's no more key pressed (just used once). If we wanna set again the direction, we have to click the key again, otherwise if we want to move the car forward we don't click any key. And we do this for all cars. Control multiple cars to its houses (same colour of the car).
danpost danpost

2016/3/27

#
Okay -- are you saying that you cannot click on another car unless the previously clicked car has made it to its house? because, if you can click on a different car before the currently controlled car makes it to it house, then there are things that need to be considered. Such as, does the previous car proceed to the next junction or just automatically stop moving? if to the next junction, and it can continue forward (without turning), does it continue? The reason I ask these things is because the first line in your act method is 'move(-1)' -- and there is no condition on it, meaning it does not matter whether the car is the controlled car or not, it will move.
kidlivin kidlivin

2016/3/27

#
Hmm it's complicated to understand, sorry about that.. :/ All cars have automatic velocity (move(-1)). Problem: when I click "a" all cars turn left and "d" to right. So, I wanna control multiple cars and each car save the key when I click on it, once. If we could do that, some cars go to left (when I click on it and press "a"), others go to right (when I click on it and press "d") and others just go forward(when I don't click in "a" or "d"), in direction to its houses. That problem happens because the key function and direction function (initially presented) are applied for all cars. So, what I've made? A list of all cars present in the world (updated at every 5 seconds). Can't I apply that functions for each object of that list? There it is the list code:
private List<Carro> listPresentCars;
public void listCars()
    {
    listPresentCars = getObjects(Car.class);
    }
danpost danpost

2016/3/27

#
kidlivin wrote...
All cars have automatic velocity (move(-1)).
The velocity would be '-1'. The 'move' method actually makes the actor move (unless the velocity was zero -- move(0); ).
Problem: when I click "a" all cars turn left and "d" to right.
Then you are not placing the condition that that car be the controlled car.
So, what I've made? A list of all cars present in the world (updated at every 5 seconds). Can't I apply that functions for each object of that list?
There is no need for the list. Anything you do not want uncontrolled cars to do should be placed under the condition that it be the controlled car:
if (controlledCar == this)
So, if you place the key detection inside the 'if' block, only the controlled car will accept the keys. If you place the 'move(-1);' line inside the 'if' block, only the controlled car will move.
kidlivin kidlivin

2016/3/27

#
And I do that, I put the key detection in act(), in that condition:
if(Greenfoot.mouseClicked(this))
        {
            controlledCar = this;
        }
        if(controlledCar == this)
        {
            int dir = 0;
            if (Greenfoot.isKeyDown("a")) 
            {
                dir++;
            }
             
            if (Greenfoot.isKeyDown("d")) 
            {
                dir--;
            }
             
            if (dir != 0) 
            {
                nextDirection = dir;
            }
        }
But doesn't work, maybe I need to call in all colour cars, but how? And another thing, after the key pressed has been used once, it clear the key pressed or will follow the key direction forever?
Super_Hippo Super_Hippo

2016/3/27

#
public static Actor controlledCar;
Move that line into the 'Carro' class, so there is not one active car of each color, but one active car at all.
kidlivin kidlivin

2016/3/27

#
There's actually in the Carro class, but don't I need to create a metod for that and call in all colour cars? And the problem I refer:
kidlivin wrote...
And another thing, after the key pressed has been used once, it clear the key pressed or will follow the key direction forever?
Super_Hippo Super_Hippo

2016/3/27

#
Reset it after it turns at a junction:
nextDirection = 0;
Oh right, sorry... What is the code in the colored cars?
kidlivin kidlivin

2016/3/28

#
Finnaly I resolved the problem! Thanks for all!! :D :D :D
You need to login to post a reply.
1
2