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
123Mike123 123Mike123

2016/3/25

#
I'm developing a game were you have multiple cars in the World, but you control only one at a time using the mouse and the arrows. However my code allows me to click on the car to select it, but all the other cars follow the same commands. Can someone help me?
Jalapena42 Jalapena42

2016/3/25

#
Please provide the code for the cars and movement.
danpost danpost

2016/3/25

#
Add a class field to the class of the car to hold the controlled car:
private static Actor controlledCar;
Then select a car with:
if (Greenfoot.mouseClicked(this)) controlledCar = this;
and place a condition on controlling code with:
if (controlledCar == this)
kidlivin kidlivin

2016/3/26

#
I think that isn't what 123Mike123... Each car have automatic velocity, and the cars just turn to right or left, according to the user. But when the user select a car and say to it to turn left, all the cars turn left when they can. The objetive it's to control each one, but all share the same directions's function.
Super_Hippo Super_Hippo

2016/3/26

#
I think he only wants to control one at a time. The other ones will stay where they are, drive straight or do something random, I don't know. What confuses me is that he said that his code allows him to select a car. I wonder how he did that because it doesn't work apparently.
kidlivin kidlivin

2016/3/26

#
This is what we have on the Cars. We have 6 cars with different colors.
/**
      Verify  the pressionated key and saves it in the variable;
      */
   public void checkKey()
    {
        if(Greenfoot.isKeyDown("a"))
            {
                Key = 'a';
            }
        else if(Greenfoot.isKeyDown("d"))
            {
                Key = 'd';
            }
    }
    /**
      Determine for where the car must turn according with the last key pressed
       */
   public void colisaoentroncamentos()
    {            
        if(this.isTouching(Entroncamento.class) && this.getX() == 185 && this.getY() == 542) 
            {
                if(Key == 'a')
                {
                    turn(-90);
                    Key = 'Z';
                }
                else if(Key == 'd')
                {
                    turn(90);
                    Key = 'Z';
                }
                else 
                {
                    carRemoved = true;
                }
            }
            
        if(this.isTouching(Entroncamento.class) && this.getX() == 41 && this.getY() == 255) 
            {
                if(Key == 'd')
                {
                    turn(90);
                    Key = 'Z';
                }
                else 
                {
                    carRemoved = true;
                }
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 256 && this.getY() == 255) 
            {
                if(Key == 'a')
                {
                    turn(-90);
                    Key = 'Z';
                }
                else 
                {
                    carRemoved = true;
                }
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 542) 
            {
                if(Key == 'a')
                {
                    turn(-90);
                    Key = 'Z';
                }
                else 
                {
                    carRemoved = true;
                }
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 326) 
            {
                if(Key == 'd')
                {
                    turn(90);
                    Key = 'Z';
                }
                else 
                {
                    carRemoved = true;
                }
            }
        if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 183) 
            {
                if(Key == 'a')
                {
                    turn(-90);
                    Key = 'Z';
                }
                else if(Key == 'd')
                {
                    turn(90);
                    Key = 'Z';
                }
                else
                {
                    carRemoved = true;
                }
            }
    }
And these 2 functions must work for a list (listaCarrosPresentes). I have these 3 functions to create random cars of each 5 seconds and saves it in list.
public void colocaCarros()
    {
       CorCar = Greenfoot.getRandomNumber(1);
           if(CorCar == 0)
            {
            addObject(new Carro_Amarelo(), 300, 399);
            car++;
            };
           if(CorCar == 1)
            {
            addObject(new Carro_Azul(), 300, 399);
            car++;
            };
           if(CorCar == 2)
            {
            addObject(new Carro_Branco(), 300, 399);
            car++;
            };
           if(CorCar == 3)
            {
            addObject(new Carro_Cinza(), 300, 399);
            car++;
            };
           if(CorCar == 4)
            {
            addObject(new Carro_Verde(), 300, 399);
            car++;
            };
           if(CorCar == 5)
            {
            addObject(new Carro_Vermelho(), 300, 399);
            car++;
            };
       CorCar = 0;
       listaCarros();
    }
    public void listaCarros()
    {
    listaCarrosPresentes = getObjects(Carro.class);
    }
    public void act()
    {
        if ((System.currentTimeMillis() - beginTime) / 1000 >= Interval)
        {
            colocaCarros();
            beginTime = System.currentTimeMillis();
            
        }
    }
Can I apply the first two functions for each car in list? The car have automatic velocity, so we must to click on the car, click the key direction to save, and the car must turn to left or right when he arrives at the road junction and when we click in the anothers cars, they have indepent direction of the previous cars. Can someone help me?
Super_Hippo Super_Hippo

2016/3/26

#
I would do it like this:
private boolean selected = false;
private int nextDirection = 0;

public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        selectThis();
    }
    if (selected)
    {
        Greenfoot.isKeyDown("a")
        {
            nextDirection = -1;
        }
        Greenfoot.isKeyDown("d")
        {
            nextDirection = 1;
        }
    }
    //...
}

//select this car and unselect all others
public void selectThis()
{
    for (Object obj : getWorld().getObjects(Car.class))
    {
        Car c = (Car) obj;
        c.selected = false;
    }
    selected = true;
}
danpost danpost

2016/3/26

#
My way would look like this (somewhat similar -- but without needing to iterate through all the cars to clear the 'selected' fields):
private static Actor controlledCar;
private int nextDirection = 0;

public void act()
{
    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;
        // ...
    }
    // ...
}
kidlivin kidlivin

2016/3/26

#
But how that dir or nextDirection works? If you see in the code I posted, he just turn in certain coordenates; the car must save the direction (turn 90 or -90), There it is to understand the problem... LinkGame
Super_Hippo Super_Hippo

2016/3/26

#
When a car reaches these coordinates, it will turn 90 when dir=1 or -90 if dir=-1. So it turns 90*dir. That was an idea, you don't have to use the variable with 1/-1.
danpost danpost

2016/3/26

#
kidlivin wrote...
But how that dir or nextDirection works? If you see in the code I posted, he just turn in certain coordenates; the car must save the direction (turn 90 or -90), There it is to understand the problem... LinkGame
When the car reaches any of those coordinates, you would use:
turn(90*nextDirection);
kidlivin kidlivin

2016/3/26

#
This is the method to turn the cars at the road junctions:
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;
            }
    }
And I put this at act:
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;
            }
        }
And I called in act, the colisaoentroncamentos(). It doesn't work and don't know why! :(
danpost danpost

2016/3/27

#
So far, all I have seen is code to turn the car. What code do you currently have to actually have the car(s) move?
kidlivin kidlivin

2016/3/27

#
There it is:
 /**
       Metod for constant velocity of the car
       */
    public void move()
    {
        move(-1);
    }
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...
danpost danpost

2016/3/27

#
kidlivin wrote...
There it is: < Code Omitted > 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...
Maybe you should show the current entire class as you have made some changes and a full picture of what you have is easier to deal with. As far as the bug, you need to show what error message you are getting (copy/paste the entire terminal output here after clearing the terminal and re-creating the error.
There are more replies on the next page.
1
2