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

2014/3/30

Help with accessing variables from another class

1
2
qwertyuiop qwertyuiop

2014/3/30

#
How would I access the value of my score variable from another class?
sopatron sopatron

2014/3/30

#
1st write this on the other class
1
2
3
4
public static int getScore()
{
   return score;
}
then you can access this whenever you want by using the (name of the class).score remember that you need to have the variable inserted like this
1
public static int score =  (the number you want)
hope i helped
danpost danpost

2014/3/30

#
Please see the Greenfoot tutorial page on this subject here.
qwertyuiop qwertyuiop

2014/4/1

#
Thanks but how would be able to use the value of the score variable in an if statement. For instance:
1
2
3
4
if(score = 10)
{
    addObject(new Square(), 0, 0);
}
danpost danpost

2014/4/1

#
qwertyuiop wrote...
Thanks but how would be able to use the value of the score variable in an if statement. For instance:
1
2
3
4
if(score = 10)
{
    addObject(new Square(), 0, 0);
}
Equality comparison are done by using a double equal sign '=='. A single one just set the evaluated expression on the right to the variable on the left of the single equal sign. This would produce an 'int' in the 'if' condition which the compiler will complain about.
qwertyuiop qwertyuiop

2014/4/1

#
Ahhh, yeah, so i should use...
1
2
3
4
if(score == 10)
        {
            addObject(new Square2(), 0, 0);
        }
... but then how do i declare the score variable again as a number? Sorry for these stupid questions, I'm new to this. Thanks.
qwertyuiop qwertyuiop

2014/4/1

#
Now i get the error of incomparable types.
danpost danpost

2014/4/1

#
qwertyuiop wrote...
Now i get the error of incomparable types.
What statement is being highlighted? and what are the types of variables being used in that statement?
qwertyuiop qwertyuiop

2014/4/1

#
The if(score == 10) bit. What I need is to find out how do:
1
int score = <the value score currently is>
danpost danpost

2014/4/1

#
qwertyuiop wrote...
The if(score == 10) bit. What I need is to find out how do:
1
int score = <the value score currently is>
I have no idea what you are asking here. Explain in detail what you are trying to do.
qwertyuiop qwertyuiop

2014/4/1

#
Okay sorry. Basically in my world class I want to add an object when the score (a separate class) reaches 10 for example. This is what I am trying to implement but I haven't been able to do it. Would you like the code for both the world and the score?
danpost danpost

2014/4/1

#
qwertyuiop wrote...
Would you like the code for both the world and the score?
No. Just the score class; but, it would be good to know what fields you have in your world class and the area around which you are trying to access the value of the score.
qwertyuiop qwertyuiop

2014/4/1

#
Here is the score class...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
import java.awt.*;
 
public class Score extends Actor
{
    public int score;
    
    public void act()
    {
        setImage(new GreenfootImage("Score: " + score, 16, Color.BLACK, Color.WHITE));
    }
     
    public void addScore()
    {
        score++;
    }
}
danpost danpost

2014/4/1

#
You do not need to set the image of the score every act cycle. After the initial setting of the image, you only need to reset the image when the score changes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;
import java.awt.Color;
 
public class Score extends Actor
{
    public int score;
 
    public Score()
    {
        updateImage(); // initial image
    }
 
    public void updateImage()
    {
        setImage(new GreenfootImage("Score: +score, 16, Color.BLACK, Color.WHITE));
    }
 
    public void addScore()
    {
        score++;
        updateImage(); // reset image
    }
}
danpost danpost

2014/4/1

#
Still waiting on the rest of what was asked for.
There are more replies on the next page.
1
2