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

2013/10/28

Get the value of a variable from another class

sihaco sihaco

2013/10/28

#
I have a game and there's a car that you drive, and when you hit a coin, the car 'eats' the coin and every coin is a point. I have a field 'public int points'. With this piece of code, I want to display the points:
GreenfootImage img = new GreenfootImage(150, 30);
        img.drawString(points, 3, 20);
        setImage(img);
Problem is, the field 'public int points' is an integer, and the img.drawString method expects a string as a parameter. This is the code for the points system:
public void getCoin() 
    {
        while (points < 30) {  
            if (canSee(Coin.class)) {  
                eat(Coin.class);  
            } else {  
                break; // don't touch a coin, so stop the loop  
            }  
            points += 1; // add 1
            if(points == 30){
                Greenfoot.stop();
            }
        }
    }
Because points is an integer, I can do points + 1 for example. Can I do this with string too, if I change the field 'public int points' to 'public String points'? Can I also do this when points is a string: points = points + "1"? After this works, how can I display the value of the field points from another class. I want to make another class to draw the text (img.drawString()). Anyone knows how to solve this?
davmac davmac

2013/10/28

#
Problem is, the field 'public int points' is an integer, and the img.drawString method expects a string as a parameter.
The easiest way to fix this is to convert the integer to a String when you use it. Line 2 becomes:
        img.drawString("" + points, 3, 20);  
Note that changing the variable itself to a String will not do what you want. It would then be much more complicated to add to the score.
davmac davmac

2013/10/28

#
By the way, your getCoin method is somewhat overcomplicated. How about:
    public void getCoin()   
        {  
            while (canSee(Coin.class)) {    
                eat(Coin.class);    
                points += 1; // add 1  
                if(points == 30){  
                    Greenfoot.stop();  
                    break;
                }  
            }  
        }  
danpost danpost

2013/10/28

#
First, I think you need to understand that by calling 'getCoin' from the act method, you are creating an inherent loop supplied by the system. While the scenario is running every act method for every active object will be executed again and again (this will include the active world and any actor object within that world). Therefore, the 'while' loop you included in that method is not needed. In fact, with it there, your scenario will instantly stop once a coin is encountered because the while loop will execute and increase the points value until it is 30 all at once. Second, in response to your question, no. You cannot add a string to an int value without first extracting the value from the string, adding the values and then re-converting it back to a string. It would be easier to leave your points field as an int value. You can, however take an int value and include it in string building. The int value will be turned to a string and concatenated onto the string. In other words, as a String, points = points + "1" will tag the end of the points String with a "1", making its apparent value to be one more than 10 times its original value; as an int, ""+points will create a string for the points value. The easiest way to pass the points value to the class of the object that displays the points value is to add a method to that class like the following:
public void updateDisplay(int points)
{
    GreenfootImage img = new GreenfootImage(150, 30);
    img.drawString(""+points, 3, 20);
    setImage(img);
}
Keep a reference to the object displaying the points value, which I call 'pointsText', in the class where your points field is located and call this method on it anytime the value of points changes:
points++;
pointsText.updateDisplay(points);
Your logic for eating a coin should be:
if (canSee(Coin.class))
{
    eat(Coin.class);
    points++;
    pointsText.updateDisplay(points);
    if (points == 30) Greenfoot.stop();
}
davmac davmac

2013/10/28

#
Hi danpost, I don't think this is quite correct:
Therefore, the 'while' loop you included in that method is not needed. In fact, with it there, your scenario will instantly stop once a coin is encountered because the while loop will execute and increase the points value until it is 30 all at once.
The coin is "eaten" if seen, so this while loop just consumes all the coins that can be seen right now (for 1 point each).
danpost danpost

2013/10/28

#
davmac wrote...
Hi danpost, I don't think this is quite correct: < quote omitted > The coin is "eaten" if seen, so this while loop just consumes all the coins that can be seen right now (for 1 point each).
You are quite correct. I, for some reason, was thinking that the coin(s) was/were not being removed. Thanks for the correction.
You need to login to post a reply.