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

2013/8/15

Sharing a variable

mattjames mattjames

2013/8/15

#
Hi again This problem has probably been solved many times but i would like specific help if possible. I would like to modify my car, for example, give it three different types of tyres (racing slicks, standard, rain) and this changes how much fuel it uses each time it moves. I have my counter sorted but it is the modification that is slowing me down. Could someone please explain how to fix this problem. I have the three buttons that will be in the modification menu as actors in a buttons sub class as so: Actor buttons racing standard wet Also I have the car as a separate actor. They are all in the same world. Please can someone explain how I can set a variable that will be affected by which modification button you press and will then change the fuel consumption of the car. Thank you very much
Gevater_Tod4711 Gevater_Tod4711

2013/8/15

#
The fuel consumption probably is a variable in the car class, right? So if you problem is that you can't access the variable in the car you should have a look at this tutorial. If that isn't the problem you'll have to specify it.
mattjames mattjames

2013/8/15

#
Sorry the fuel consumption is a counter class that is working fine, and when you move the car it decreases the counter fine. I want to have a variable (simple as 1,2 and 3) that is changed when you pick a different wheel, and then is monitored in the car class that will dictate how much to decrease the fuel by each time. I tried the tutorial but I couldn't understand it properly.
Gevater_Tod4711 Gevater_Tod4711

2013/8/15

#
You need a variable for the fule consumptio. This variable is in the counter class. Now to change the value in the counter class you can either get the reference to this object like in the tutorial or (if you have only one instance of the counter class) you can access the variable by declaring it static. But this only works if you have only got one instance of the counter in your world at every time. If you got two or more you need to get the reference like discribed in the tutorial. If you want to use static variables you can try it like this:
//in your counter class declare this variable;
public static int fuelConsumption = 0;
Now you can access this variable static:
//in your button classes you can change the value like this:
Counter.fuleConsumption = 1;

//You access a static variable always with Classname.variablename ...
mattjames mattjames

2013/8/15

#
Sorry thats not what I meant. I will try and describe it better. I have one screen in the game where you can modify the car you use. It will have three buttons on the screen, and i will create an int variable called choice. I want it to, when you click on the first button or example, change the choice variable to 1, the second button will change it to two, and the third will change it to 3. Then when you play the actual game, in the code for the car, I will have an if statement that goes if (choice==1){ counter.bumpCount(-5);} //simplified without full code if (choice==2){ counter.bumpCount(-6);} Does this explain it better? I need help getting the variable between actors because I do not understand the tutorial. BTW the world is called world
Gevater_Tod4711 Gevater_Tod4711

2013/8/15

#
I think the code I gave you abouve would work for this problem. If you declare the variable choise 'public static' you can change it's value like I tould you. If now the first button is pressed you set it's value to 1 if the second button is pressed you set the value to two ... Where the variable is or how it is named doesn't realy matter. If your variable is called choice and it's in your car class you have to use 'Car.choice = 1;' instead of 'Counter.fuelCousumption = 1' but it'll work the same way.
danpost danpost

2013/8/15

#
When you say 'one screen' to modify the car, if you meant 'one world', then the easiest thing to do is to pass the car to be modified to the world. In the 'choice world', save the Car object in an instance field declared with 'private Car car;'. In the 'choice world' constructor, save the Car object that was passed to it. Then, when a button is pressed, modify the car right then. To return to the main world, you can use 'Greenfoot.setWorld(car.getWorld());'.
You need to login to post a reply.