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

2012/8/28

Get Variables from an Actor to a World

1
2
Barador Barador

2012/8/28

#
Hi it's me again, In one world of my program i defined some variables i need for Text Boxes there. In this world I want to create an if-Statement with a variable from an Actor and the variables in this world. I already checked This HowTo but I didn't really get get it. Can you help me?
DonaldDuck DonaldDuck

2012/8/28

#
This:
theCounter = new Counter();
addObject(theCounter, 5, 5);
is the important part in the tutorial you've linked to. In your World subclass, when you add the Text Boxes you probably just use addObject(new textbox(), x, y)... By creating a reference to the text box you are adding, you can use that reference to read variables and such from the textbox. For example, here is some code you can put in your World subclass to do what you need.
Password-Box = new textbox();
addObject(Password-Box, x, y);
this.password = Password-Box.password;
Alternatively, you can put this code into the textbox class...
if(((WORLD-NAME) getWorld()).password == this.password)
{
    throw_party();
}
By giving the actor a direct reference to the World it's in, you can interact with it. Alternatively still, you can just create a variable in the World class and set it from the textbox class using ((WORLD-NAME) getWorld()).VARIABLE-NAME = this.VARIABLE-NAME;
Barador Barador

2012/8/28

#
Thanks! Now this part works I think... I will put the next problem in this Discussion OK? My check of the Input and the two number I have to add to each other doesn't really work. Here's the code:
public void act()  
    {  
        if(number1 + number2 == value)
        {
          GreenfootImage gi7 = new GreenfootImage("button-red.png");
          Button bt7 = new Button(50, 65, "Right!", 2, gi7);
          addObject (bt7, getWidth()/2, (getHeight()/4)*2);
        }
    }  
danpost danpost

2012/8/28

#
One thing, is you are (in line 5) creating an image, but not doing anything with it! As soon as the act method completes its cycle, the image is gone (the "button-red.png" image).
DonaldDuck DonaldDuck

2012/8/29

#
As danpost says, the image gi7 is fairly useless and I'd just assume make it a final GreenfootImage when you initialize the other variables. As for your problem, clarity is key. I'm not exactly sure where the code is failing for you. I can only assume the one of the variables (number1, number2 or value) isn't being updated every time it actually changes. If one of the numbers changes in the textbox class, it should update for whatever class your above code is in. If it doesn't, it will always be adding the same two numbers and it will never add to the needed value. Try putting something like number1 = tb1.number; in the act cycle to ensure that the local variable is constantly updating.
Barador Barador

2012/8/29

#
@danpost But when I input something like if(1 == 1) etc. it works and the image with text is shown @DonaldDuck The Variables number1 and number2 are in the same world class than this if-Statement so they should update or not? There is only the value with which I'm not sure I will try to let it update...
danpost danpost

2012/8/29

#
Barador wrote...
@danpost But when I input something like if(1 == 1) etc. it works and the image with text is shown
You may be getting an image, but it is not coming from line 5. As far as the 'if' part not working, all three variables ('number1', 'number2', and 'value') must be declared in the class and have been initialized. As you only posted the short snippet above, one cannot tell where the values of those variables are being set or changed; and, therefore, can be of little help. The image with text would be the Button object you created with the word "Right". I can see how that would show, but the 'gi7' image is in no way being used for that object (or anything else, for that matter).
sellerofperfume sellerofperfume

2012/8/29

#
Hi ... i beginner in greenfoot , i want teaching can help me
sellerofperfume sellerofperfume

2012/8/29

#
any book explain greenfoot and java in pdf
danpost danpost

2012/8/29

#
The 'Documentation' link above will get you to the Greenfoot tutorials and APIs. The Java tutorials abounds with information on the language, including a link to the Java APIs. Start with the second section on the left.
Barador Barador

2012/8/30

#
Here's the whole code of the world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Rechnen extends World
{
    boolean gettingNumber = true;
    
    int number1 = Greenfoot.getRandomNumber(20);
    int number2 = Greenfoot.getRandomNumber(20);
    String char1 = Integer.toString(number1);
    String char2 = Integer.toString(number2);
    int value;
    
    public Rechnen()
    {    
        super(600, 400, 1);
        
        NumberInputBox nib = new NumberInputBox();
        addObject(new NumberInputBox(), (getWidth()/8)*6, getHeight()/2);
        this.value = nib.value;
        gettingNumber = true;
        
        GreenfootImage gi3 = new GreenfootImage("button-yellow.png");
        Button bt3 = new Button(55, 65, char1, 0, gi3);
        addObject (bt3, getWidth()/8, getHeight()/2);

        GreenfootImage gi4 = new GreenfootImage("button-yellow.png");
        Button bt4 = new Button(50, 65, char2, 0, gi4);
        addObject (bt4, (getWidth()/8)*3, getHeight()/2);
        
        GreenfootImage gi5 = new GreenfootImage("button-yellow.png");
        Button bt5 = new Button(50, 65, "+", 0, gi5);
        addObject (bt5, (getWidth()/8)*2, getHeight()/2);
        
        GreenfootImage gi6 = new GreenfootImage("button-yellow.png");
        Button bt6 = new Button(50, 65, "=", 0, gi6);
        addObject (bt6, (getWidth()/8)*4, getHeight()/2);
    }
    
    public void act()  
    {  
        if(number1 + number2 ==  value)
        {
          GreenfootImage gi7 = new GreenfootImage("button-red.png");
          Button bt7 = new Button(50, 65, "Right!", 2, gi7);
          addObject (bt7, getWidth()/2, (getHeight()/4)*2);
        }
    }  
}
danpost danpost

2012/8/30

#
Need a little help, I see. Remove lines 8 and 9 (those variables not used anywhere). Remove line 10 and 18 (18: not used that way; 10: unneccessary) Change your act method (lines 38 through 46) to:
public void act()
{
    if (gettingNumber && getObjects(NumberInputBox.class).isEmpty)
    {
        gettingNumber = false;
        if (number1 + number2 == NumberInputBox.value)
        {
            GreenfootImage gi7 = new GreenfootImage("button-red.png");
            Button bt7 = new Button(50, 65, "Right!", 2, gi7);
            addObject(b7, getWidth() / 2, (getHeight() / 4) / 2);
        }
        // else if number wrong (maybe ask for retry?)
        Greenfoot.stop(); // remove after adding 'else' block
    }
}
That should start you on your way.
DonaldDuck DonaldDuck

2012/8/31

#
As danpost says. The reason your current code probably isn't working is because you initialize the integer "value" when the world is created and it doesn't update itself. Value will always equal whatever number it was assigned at the moment the world was made. If your current if statement was ever to complete (number1+number2==value), it would begin creating Button 7 and keep doing that until you ran out of Java heap space (because you don't have anything to stop it)
danpost danpost

2012/8/31

#
I am so sorry. I did not see the Button parameters with char1 and char2, so do not remove lines 8 and 9. Also, I found two typos in my last code post (1) 'isEmpty' should be 'isEmpty()' (with parenthesis) in line 3, and (2) 'b7' should be 'bt7' in the addObject statement in line 10
sellerofperfume sellerofperfume

2012/8/31

#
danpost wrote...
The 'Documentation' link above will get you to the Greenfoot tutorials and APIs. The Java tutorials abounds with information on the language, including a link to the Java APIs. Start with the second section on the left.
thank you very much danpost
There are more replies on the next page.
1
2