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

2013/8/28

Help Please

1
2
3
4
mattjames mattjames

2013/8/28

#
Hi guys I know I have asked this before but I still need help with it. This is my program structure: World world Actor car //retreives variable choice from buttons buttons //initialises a variable called choice one //sets choice to one two //sets choice to two three //sets choice to three I want to create a variable called choice in buttons. When you click on either one, two or three, the variable changes accordingly (as shown above). Then when you start the game the car class retrieves the variable choice to help with movement (not needed for explanation). They are all in the same world Please can someone explain the various code bits that will fulfil this purpose. Many thanks
davmac davmac

2013/8/28

#
A couple of questions: - does 'buttons' represent an actor is it purely abstract? - is there some reason why the variable 'choice' must be in the 'buttons' class? I'm thinking it might be easier to declare 'choice' in the world class and have your buttons manipulate it there.
mattjames mattjames

2013/8/28

#
Davmac - buttons is in the actor subclass but it is just a heading for the actors in it, it will have no presence in the world. The choice variable can be in the world class as well.
davmac davmac

2013/8/28

#
Right, then that's what I'd do - put the variable in the world class. You can then access it as per tutorial #6.
mattjames mattjames

2013/8/30

#
I had a look a that but don't really get it. I mean what do you have to put in the world class, how do you change it in the various buttons class and then how do you read it in the car class?
davmac davmac

2013/8/30

#
In the World class, you have to put the variable declaration and a method to access or manipulate the variable (set its value). This is basically the same as the tutorial which adds a Counter, but in this case your variable will probably be an 'int'. Instead of the 'getCounter' method (as per the tutorial) you'd have a 'setChoice' method which has an int parameter and sets the variable to the value of the parameter, as well as a 'getChoice' method which returns the value of the variable. In the buttons classes, you call the 'setChoice' method from the world, the same way as the tutorial has Rocket call the 'getCounter' method from the world. In the car class, you'd call the 'getChoice' method from the world in the same way.
mattjames mattjames

2013/9/1

#
Thanks that explains a lot but I still don't get what I would write in the setChoice method, and then how I would call and change the variable in each button. The organisation I now get though which is a big help.
danpost danpost

2013/9/1

#
mattjames wrote...
When you click on either one, two or three, the variable changes accordingly (as shown above). Then when you start the game the car class retrieves the variable choice to help with movement...
How can this be (unless you really do not mean 'when you start the game')? You cannot have the button objects detect clicks before the user starts the game. When are the buttons really able to be clicked on? Is it that when the scenario is started, the buttons appear (or are activated) and clicking on a button starts the actions of the car? and the behavior (and possibly some characteristics) of the car is determined by which button was clicked? Once a button object is clicked, can another one be clicked (or again the same one) or are the button objects removed?
mattjames wrote...
Thanks that explains a lot but I still don't get what I would write in the setChoice method, and then how I would call and change the variable in each button. The organisation I now get though which is a big help.
You should not have to change any variables in any of the button sub-classes. Each one should, upon instantiation (or construction), save its button number or text in an instance field. This value would then be used to set your 'choice' field (wherever you put it) to when the button object is clicked on. If this set of buttons are used to control just one car object, then the 'choice' field can be placed in the 'buttons' class using a 'static' modifier ('public static int choice = 1;' or 'public static String choice = "one";'). You can then refer to this field from any world or actor class with 'buttons.choice'. Making the field static tells the compiler that the field belongs to the class and not to individual objects of the class as instance fields. Please note that sometimes (but I do not think in your case) using static fields may cause some unwanted behavior or errors when stopping/resetting/starting your scenario (because the static fields hold their values and are not reset until re-compilation).
mattjames mattjames

2013/9/1

#
Dan sorry I didnt make myself clear, the game is started and this is in a menu system, the bit where I wrote when the game starts is when you start playing. Sorry about that. When you click a button nothing happens to the actor except the variable changes, and you can click any of the other buttons to keep changing the variable. You then click a back button, then a play button which takes you onto the game screen (all in one world). And what I wanted was a variable to be made, as you suggested in the world class, and then when you click on a button this variable changes. I don't understand what you mean when you talk about fields (really new to all this).
mattjames mattjames

2013/9/1

#
Dan sorry I didnt make myself clear, the game is started and this is in a menu system, the bit where I wrote when the game starts is when you start playing. Sorry about that. When you click a button nothing happens to the actor except the variable changes, and you can click any of the other buttons to keep changing the variable. You then click a back button, then a play button which takes you onto the game screen (all in one world). And what I wanted was a variable to be made, as you suggested in the world class, and then when you click on a button this variable changes. I don't understand what you mean when you talk about fields (really new to all this).
mattjames mattjames

2013/9/1

#
for example, here is what I want it to do: (not proper code) world class: int choice=0; buttonone class: if(clicked){ //get variable choice choice=1; } buttontwo class: if(clicked){ //get variable choice choice=2; } car class: //get variable choice if(choice==1){ ....} if(choice==2){ .... }
davmac davmac

2013/9/1

#
I still don't get what I would write in the setChoice method, and then how I would call and change the variable in each button.
You just set the choice variable to the value of the parameter. Something like:
void setChoce(int newchoice)
{
    choice = newchoice;
}
In the buttons you call getWorld().setChoice(3) for example, but you need to cast "getWorld()" to the appropriate type as per the tutorial. The tutorial has plenty of examples for calling methods in the world from an actor, just look at it again.
mattjames mattjames

2013/9/1

#
Basically I need someone to give me the code that will make an int variable to be shared through a world, then the code to access and change it through an actor in that world, and then finally the code to read the variable through an actor in the world (for example for use in an if 1....statement).
davmac davmac

2013/9/1

#
You already have all that, in the tutorial and with what I posted above.
danpost danpost

2013/9/1

#
'field' is another name for a variable. The buttons class should have all the fields and methods needed for its sub-classes. The only thing the sub-classes need to do is save its button number and call a method to create its image. A sample button sub-class would be simply:
import greenfoot.*;

public class one extends buttons
{
    public one()
    {
        value = 1;
        updateImage();
    }
}
with a buttons class like this:
import greenfoot.*;
import java.awt.Color;

public class buttons extends Actor
{
    public static int choice = 1;
    private int value;

    public void updateImage()
    {
        setImage(new GreenfootImage(""+value, 24, Color.black, Color.cyan));
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this)) choice = value;
    }
}
The code given is more an outline of what you probably want. First, you may want to change the updateImage method so that the image is not such a plain one.
There are more replies on the next page.
1
2
3
4