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

2015/11/7

How to use a variable from the World in and Actor.

mickyg mickyg

2015/11/7

#
I'm having an issue where I can't seem to use a variable from my world class in the actor class. Basically the variable is a field that is assigned a value in a method in the World (either a 1 or 2). I am trying to access that variable in one of my actors. I am able to access it (whether by making it public or using a method to return it), however when I try to use it or inspect it by assigning it to a new variable in the Actor class the new variable always remains zero or is assigned zero. What am I not doing correctly?
Super_Hippo Super_Hippo

2015/11/7

#
It's always easier to help if you show the code you are using. Please use the 'code' tags when posting code.
mickyg mickyg

2015/11/8

#
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
import greenfoot.*;
import java.util.*;
 
/**
 * Write a description of class Table here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Table extends World
{
    Random generator = new Random();
    public int server;
    /**
     * Constructor for objects of class Table.
     *
     */
    public Table()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        Bat bat = new Bat();
        addObject (bat,300,200);
    }
 
    public void coinToss()
    {
        int toss = 0 + generator.nextInt(2);
        //System.out.println(toss);
        if (toss == 0)
        {
            server = 0;
        }
 
        if (toss == 1)
        {
            server = 1;
        }
    }
}
mickyg mickyg

2015/11/8

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import greenfoot.*;
import java.util.*;
 
/**
 * Write a description of class Bat here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bat extends Actor
{
    /**
     * Act - do whatever the Bat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Table table = new Table();
        int store = table.server;    
        System.out.println(store);// checking to see if it works
    }   
}
Super_Hippo Super_Hippo

2015/11/8

#
Call the 'coinToss' method from the constructor of your world subclass.
danpost danpost

2015/11/8

#
You are creating a different world from the one that the Bat object is in and getting the value of its 'server' field. Replace lines 18 and 19 of your Bat class with the following line:
1
int store = ((Table)getWorld()).server;
to get value of 'server' from the world the Bat object is in. Or, you could just change line 18 to:
1
Table table = (Table)getWorld();
mickyg mickyg

2015/11/8

#
Ok thank you very much, I didn't realize I needed to cast it to a Table object. I thought it was still the same World. Any chance you can explain that a little further? I'm still a little unsure why that is. Is that because new Table() is creating a "new" Table object as opposed to changing its type?
danpost danpost

2015/11/8

#
A class is not restricted to creating only one object of its type. Every time you use 'new Table()', a new distinct Table type world is created. You cannot change the type of an object; however, sometimes it is necessary to inform the JVM that an object is a specific type of object. All objects are of type Object, but only certain ones are World objects -- and of those World objects only certain ones are Table object (in your case, all World objects are Table objects, but the 'getWorld' method returns the Table object as a World object and to access fields or methods in the Table class, the JVM must be informed that the world is indeed a Table object -- otherwise, it will not look in the Table class for those members (fields or methods).
mickyg mickyg

2015/11/8

#
Thank you very much for the excellent explanation and for correcting my understanding of how it works. It makes much more sense now!
You need to login to post a reply.