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

2013/7/22

Controlling Multiple Instances Individually

tommyd456 tommyd456

2013/7/22

#
Hi I'm a beginner when it comes to Greenfoot and Object Oriented programming. After playing around with the software for a while I can't seem to find out a way to add code for specific instances. For example, lets say I have an Actor called Car. I can get my two instances on the stage but when I press the Right Arrow key I want just one of the cars to move, not both. How can I do this? Thanks
Gevater_Tod4711 Gevater_Tod4711

2013/7/22

#
You have to differentiate the two cars using an int or a boolean. It's probably easyer to use an int because with a boolean you can only have two car types. So you have to set the value of the int to 1 in the first car and to 2 in the second car (or any other numbers. You just have to care about the different numbers). Then you can check whether the car should be moved when the key is pressed using an if statement to check which of the cars this instance is:
int carType;

public void act() {
    if (carType == 1) {
        //move when key pressed;
    }
    else {
        //do something else;
    }
}
Now you just need to set the values in this car instances to the right values to not change up the cars. You either can do this in the constructor of the cars or use the addedToWorld(World world) method of greenfoot. If you want to use the constructor you can do it like this:
//add this constructor to your car class;
public Car(int carType) {
    this.carType = carType;
    //may some other stuff;
}
Then you need to call the right constructor while creating the car:
//if you now got some code like this:
addObject(new Car(), 100, 100);
addObject(new Car(), 100, 200);

//you need to change it to:
addObject(new Car(1), 100, 100);
addObject(new Car(2), 100, 200);
If you want to use the addedToWorld method you can do it this way:
//add this method to your car class;
public void addedToWorld(World world) {
    carType = getWorld().getObjects(Car.class).size() +1;
    //this will make the first car carType 1, the second carType 2 the third carType 3, ...
}
danpost danpost

2013/7/22

#
Gevater_Tod4711 wrote...
It's probably easyer to use an int because with a boolean you can only have two car types.
Actually, either way is fine. There would only be two types of car (one that accepts keystroke control and the other that does not). As an alternative, you can use subclasses of Car to distinguish between the two types (one subclass for auto-control and the other for manual-control).
Gevater_Tod4711 Gevater_Tod4711

2013/7/22

#
But I think if you want to add more cars to your game that will not work very long. Using more than two a boolean would not work and using subclasses would also not realy be the best idea because the more classes you got in your programm the more code you need and the programm will be more confusing... So I think using an int to check the type of the instance is the best way for doing this.
danpost danpost

2013/7/22

#
It would not matter how many cars of either type are in the game. Either way I suggested would work fine. When subclassing, you really have little extra code, as they share the code in the superclass. Only the code that makes it unique should go in the subclass. If I find time to, I will upload an example scenario or two to illustrate.
danpost danpost

2013/7/23

#
@Gevater_Tod4711, I uploaded an illustrative example using both methods. You will have to download it to view the code and try out the other method (the one running on the site is the subclassing method). You can find it here.
Gevater_Tod4711 Gevater_Tod4711

2013/7/23

#
I know how subclasses and polymorphism works. But I still think in this case it's better to use variables to differentiate between the objects.
eurekatuesday eurekatuesday

2013/10/29

#
What about Actor with a String "name"? "LadyGaga"...
danpost danpost

2013/10/29

#
If only controlling one Car object at a time, you could add a class (static) field to hold the specific car object that is to be controlled.
// class field
static Car controlledCar = null; // to be set by code elsewhere
// act method
public void act()
{
    if (controlledCar == this) checkControls();
}

private void checkControls()
{
    // check for keystrokes and act upon them
}

public void loseControl()
{
    controlledCar = null;
}

public void gainControl()
{
    controlledCar = this;
}
You need to login to post a reply.