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

2016/6/15

Access to a boolean

carti26 carti26

2016/6/15

#
Hey! I want to have access to a boolean, but I don't know how that could work... My actor class "car" needs this boolean, but it is initialized about the world class "racetrack" (where i make a new object timer timer = new timer()) And from this timer i need the boolean. Please don't ask, why i made it this difficult :-)
danpost danpost

2016/6/15

#
carti26 wrote...
My actor class "car" needs this boolean, but it is initialized about the world class "racetrack" (where i make a new object timer timer = new timer()) And from this timer i need the boolean
It would help if you posted the code to the "racetrack" class.
carti26 carti26

2016/6/15

#
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
import greenfoot.*; 
 
public class Rennstrecke extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public Rennstrecke()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 300, 1);
        prepare();
        //Greenfoot.playSound("sound.m4a");
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        r8 r8 = new r8();
        addObject(r8,50,30);
        smart smart = new smart();
        addObject(smart,50,230);
        Auto countdown = new Auto();
        addObject(countdown,500,150);
        countdown.starte();
    }
}
danpost danpost

2016/6/15

#
carti26 wrote...
< Code Omitted >
Is 'Auto' the class of the timer? If so, you can use:
1
Auto timer = (Auto)getWorld().getObjects(Auto.class).get(0);
to get a reference to the object in the 'car' class. I will need to see the code for the Auto class to help get the boolean value.
carti26 carti26

2016/6/15

#
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Auto here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Auto extends Actor
{
    private int count; // the counter field
    private int initialCount; // the initial time given before event occurs
    private boolean running;
    private boolean getsStarted = false;
    public boolean couldStart = false;
    private int timeBeforeEvent = 6;
     
    /**
     * Konstruktor der Klasse Tier - hier wird nichts gemacht.
     */
    public Auto()
    {
         
    }
 
    public void starte()
    {
        setTimer(timeBeforeEvent);
        updateImage();
        running = getsStarted;
    }
     
    /**
     * Drehe den uebergebenen Wert in Grad nach rechts.
     * Das p vor pGrad zeigt an, dass es ein Parameter (Uebergabewert) ist.
     */
    public void drehe(int pGrad)
    {
        setRotation(getRotation() + pGrad);
    }
 
    public void setTimer(int timeBeforeEvent)
    {
        initialCount = 60 * timeBeforeEvent;
        count = -initialCount;
    }
 
    private void updateImage()
    {
        if (timeBeforeEvent >=2)
        {
            count++;
            timeBeforeEvent --;
            if ((count + initialCount) % 60 == 0) updateImage();
            int time = count * (int)Math.signum(count);
            time = time / 60;
            int secs = time % 60;
            time = (time - secs) / 60;
            String s = "" + secs;
            String text = s;
            GreenfootImage textImage = new GreenfootImage(text, 40, Color.red, new Color(0, 0, 0, 0));
            GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
            image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
            setImage(image);
        }
        else if (timeBeforeEvent == 1)
        {
            timeBeforeEvent --;
            couldStart = true;
            String text = "GO";
            GreenfootImage textImage = new GreenfootImage(text, 50, Color.red, new Color(0, 0, 0, 0));
            GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
            image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
            setImage(image);
        }
        else
        {
            String text = "";
            GreenfootImage textImage = new GreenfootImage(text, 50, Color.red, new Color(0, 0, 0, 0));
            GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
            image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
            setImage(image);
        }
    }
 
    public void act()
    {
        setLocation(500,150);
         
        if (Greenfoot.isKeyDown("p"))
        {
            start();
        }
         
        if (running == true)
        {
            count++;
            if ((count + initialCount) % 60 == 0) updateImage();
        }
    }
 
    public int getTime()
    {
        return count / 60;
    }
 
    public void start()
    {
        running = true;
    }
}
thank you for your effort. :D
danpost danpost

2016/6/15

#
Since there are no 'public' members that the car can call to acquire the boolean value, it is impossible at this time. Either change the access modifier of the boolean field to 'public' (from 'private') or add a 'public boolean getRunning()' method that returns the value of the field so the car can call the method to get the boolean value. Once that is done, the value will be accessible; however, accessing it is yet another step in the process. The line I gave above can be followed by either:
1
2
3
4
// if field is made 'public'
boolean timerRuns = timer.running;
// if public method is added
boolean timerRuns = timer.getRunning();
You need to login to post a reply.