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

2015/12/28

Access a variable to another class

Ramin Ramin

2015/12/28

#
I want ClassA to see whether ClassB has image1 or image2. I thought I can do this with variable but how can ClassA use variable of ClassB ??
davemib123 davemib123

2015/12/28

#
you probably want to check this link: http://www.greenfoot.org/doc/howto-1
fejfo fejfo

2015/12/28

#
in class a put :
if(getWorld().getObjects(ClassB.class).isEmpty() = false) <image> = ((ClassB) getWorld().getObjects(ClassB.class).get(0)).getImage();
<image> should be a GreenfootImage where you store the varible
danpost danpost

2015/12/28

#
fejfo wrote...
in class a put :
if(getWorld().getObjects(ClassB.class).isEmpty() = false) <image> = ((ClassB) getWorld().getObjects(ClassB.class).get(0)).getImage();
<image> should be a GreenfootImage where you store the varible
Two problems with this: (1) you cannot set a method call to a value; 'getWorld().getObjects(ClassB.class).isEmpty() = false' does not make any sense. To compare the returned 'isEmpty' value with false, you need to use the conditional equality operator which consists of 2 equal signs ( '==' ). To check for a 'false' value, you could just negate the returned value using the boolean negation operator ( '!' ) instead of comparing the value to 'false'. (2) it is not 'getImage' that needs to be accessed from class B, its the fields, 'image1' and 'image2' that access is wanted to. However, even that will not be enough to deterimine which image is currently showing. Maybe an instance public boolean field is needed within class B to keep track of which image is current.
You need to login to post a reply.