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/4/1

#
Sorry what do you mean by fields in my world class and by area I am trying to access the score, do you mean the method I want to use it in? Sorry about this and thanks for persevering.
danpost danpost

2014/4/1

#
qwertyuiop wrote...
what do you mean by fields in my world class
Anything not inside a method or constructor would do.
and by area I am trying to access the score, do you mean the method I want to use it in?
Yes.
qwertyuiop qwertyuiop

2014/4/1

#
Well I am still slightly confused by the fields and I don't want to keep trying to guess what you mean so here is the entire world code and I want implement:
1
2
3
4
if(score == 10)
{
        addObject(new Square2, 0, 0);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class mainWorld extends World
{
    Score score = new Score();
 
    public mainWorld()
    {   
        super(256, 512, 1);
        setBackground("Background.png");
         
        Greenfoot.setWorld(new mainMenu());
         
        if(score == 10)
        {
            addObject(new Square2(), 0, 0);
        }
         
        displayUser();
        displayDefault();
        displayScore();
        getScore();
    }
     
    public void displayUser()
    {
        User userMain = new User();
        addObject(userMain, 128, 48);
    }
     
    public void displayDefault()
    {
        int randomXe1 = Greenfoot.getRandomNumber(256);
        int randomXe2 = Greenfoot.getRandomNumber(256);
        int randomXe3 = Greenfoot.getRandomNumber(256);
         
        addObject(new Rectangle(), randomXe1, 512);
         
        addObject(new Rectangle2(), randomXe2, 512);
         
        addObject(new Square(), randomXe3, 512);
}
     
    public void displayScore()
    {
        score = new Score();
        addObject(score, 128, 505);
    }
     
    public Score getScore()
    {
        return score;
    }
}
Thanks again.
danpost danpost

2014/4/1

#
Remove line 44 from the mainWorld code above. Ok, the main problem is that 'score' in the world class is a Score object (not an 'int'). You cannot compare 'score' with an 'int' You might try 'score.score == 10'. However, lines 12 through 15 of the mainWorld class above should not be in the world constructor which is only executed when the world itself is instantiated. I was going to say to add a 'public void act()' method to the class and put that code in there, but there is one more problem that will need to be dealt with. The score may stay at '10' for many act cycles and we only want to create one Square2 object. It might be better to place this code at the place where you increment the score
qwertyuiop qwertyuiop

2014/4/1

#
Well I increase the score when 1 of 3 objects reaches the top of the screen so I'm not sure how I would do that then I will try it out and see what happens thanks.
danpost danpost

2014/4/1

#
qwertyuiop wrote...
Well I increase the score when 1 of 3 objects reaches the top of the screen so I'm not sure how I would do that then I will try it out and see what happens thanks.
But all three would call 'addScore' in the Score class, right? and that is where the value of the int that holds the score gets incremented -- in the Score class.
qwertyuiop qwertyuiop

2014/4/1

#
Yeah you were right. I added 'public void act()' and it now works however, it adds 3 objects as you thought may happen so I am almost there. Is there a way to put a cap on the number of squares that can be added for that if statement?
danpost danpost

2014/4/1

#
How many of them do you want to create when the score reaches 10?
qwertyuiop qwertyuiop

2014/4/1

#
Just the 1 if possible.
danpost danpost

2014/4/1

#
Will it remain at least until the score increases again. or is there any chance of it being removed before the score increases again?
qwertyuiop qwertyuiop

2014/4/1

#
It will remain until the game is over.
danpost danpost

2014/4/1

#
Then you can use:
1
if (score.score == 10 && getObjects(Square2.class).isEmpty())
qwertyuiop qwertyuiop

2014/4/1

#
Ahhh thank you, I will try it now.
qwertyuiop qwertyuiop

2014/4/1

#
It works thanks, but if I wanted more than one what would I do then?
qwertyuiop qwertyuiop

2014/4/1

#
Never mind I just changed .isEmpty() to .size() < 2
You need to login to post a reply.
1
2