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

2016/2/26

Variable issue

GreenMan15 GreenMan15

2016/2/26

#
Hello, first of all I added an instance variable named numberOfBacteriaTouched into my class bloodstream. However Whenever I go to the white blood cell editor then go to the checkCollison method. I type in bloodstream.numberOfBacteriaTouched +1, it does recognize it and even if I enter bloodstream.numberOfBacteriaTouched it says method cannot be found. I don't know what is happening. II. here are the rules to increase these four instance variables: a. when the white blood cell touches a bacteria, increase variable numberOfBacteriaTouched by one
GreenMan15 GreenMan15

2016/2/26

#
I made a silly mistake all I needed to do was create a method then increase my variable by one then call that method.
GreenMan15 GreenMan15

2016/2/26

#
It says variable bloodstream could not be found when I try to implement my method in class whitecell. bloodstream.incNumberOfWhiteCellsTouched(); invalid?
danpost danpost

2016/2/26

#
GreenMan15 wrote...
It says variable bloodstream could not be found when I try to implement my method in class whitecell. bloodstream.incNumberOfWhiteCellsTouched(); invalid?
You will need to post the class in question for any help.
GreenMan15 GreenMan15

2016/2/26

#
public class Bloodstream extends World { private int numberOfVirusMissed; // where I created my instance variable public void incNumberOfVirusMissed(){ numberOfVirusMissed++; //Create method to increase variable by one } if (getX() == 0) { //implement my method in class virus getWorld().removeObject(this); bloodstream.incNumberOfVirusMissed(); }
danpost danpost

2016/2/26

#
It is the virus class where the issue is showing up and that which you need to post -- not the Bloodstream class
GreenMan15 GreenMan15

2016/2/26

#
public class Virus extends Actor { /** * Float along, slowly rotating. */ public void act() { setLocation(getX()-8, getY()); turn(-1); if (getX() == 0) { getWorld().removeObject(this); bloodstream.incNumberOfVirusMissed(); } } }
danpost danpost

2016/2/26

#
No wonder, the variable 'bloodstream' in the virus class (1) not declared anywhere and (2) is not assigned any value (or object reference). You want 'bloodstream' to refer to the world that the virus is in; therefore, you get the reference as follows:
World world;
world = getWorld();
Bloodstream bloodstream;
bloodstream = (Bloodstream) world;
Line 2 sets 'world' to a reference to your Bloodstream world; but, the compiler only knows that the world is a World object -- not that it is specifically a Bloodstream object. In order to access the 'incNumberOfVirusMissed' method of the Bloodstream class, the compiler needs to be told that the world IS a Bloodstream object. Lines 3 and 4 are used to basically let the compiler know that the world is indeed a Bloodstream object. In short, it can be coded as follows:
Bloodstream bloodstream = (Bloodstream) getWorld();
and you can eliminate the variable totally when calling the method by using the following:
((Bloodstream)getWorld()).incNumberOfVirusMissed();
GreenMan15 GreenMan15

2016/2/26

#
Thank you so Much! It worked!
You need to login to post a reply.