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

2016/10/12

White Blood Cell

1
2
LimeLava LimeLava

2016/10/12

#
So i am doing the White Blood Cell practice in the book and i am to the point where i am subtracting the score whenever the Virus of Bacteria touches the wall but whenever they touch the wall they set the score to -15 for bacteria and -100 for a virus instead of subtracting that number from the current score. Code for adding score when you "eat" a bacteria and Virus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void checkCollision()
    {
        if(isTouching(Virus.class))
            {
                removeTouching(Virus.class);
                score = score + 20;
                getWorld().showText("Score" + score,80,25);
            }
        if (isTouching(Bacteria.class))
            {
                removeTouching(Bacteria.class);
                score = score + 10;
                Greenfoot.playSound("slurp.wav");
                getWorld().showText("Score:" + score,80,25);
           }
    }
Code for subtracting score when the bacteria touches the edge
1
2
3
4
5
6
if (getX() == 0)
        {
            score = score - 15;
            getWorld().showText("Score:" + score,80,25);
            getWorld().removeObject(this);
        }
code for subtracting score when the virus touched the edge
1
2
3
4
5
6
if (getX() == 0)
        {
            score = score - 100;
            getWorld().showText("Score:" + score,80,25);
            getWorld().removeObject(this);  
        }
By the way i have kind of changed what the book tells you to do an i have kept the scoring mechanisms in there respecive classes instead of moving it all to the Bloodsteam( the world)
Super_Hippo Super_Hippo

2016/10/12

#
If all classes (or all objects of all classes) have their own score variable, then one of it does not have anything to do with the other. They have the same name but it is a different variable. All of them start at 0, if you increase the variable in the first class, the subtraction in the second and third class result in the negative number no matter what happened to the other variable.
danpost danpost

2016/10/12

#
You apparently have three different scores -- one in the class of the Virus, one in the class of the Bacteria and one in the class of the white blood cell. EDIT: looks like I was a little slow here.
LimeLava LimeLava

2016/10/12

#
Ok. thank you for your help
LimeLava LimeLava

2016/10/17

#
I assume i am supposed to create a new thread for this but how do i share the value between the classes?
danpost danpost

2016/10/17

#
LimeLava wrote...
how do i share the value between the classes?
Well, I presume that it is a game score value, which infers that it should probably be in your World subclass:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// field
private int gameScore;
 
// method for getting the value
public int getGameScore()
{
    return gameScore;
}
 
// method for adding to the value
public void bumpGameScore(int points)
{
    gameScore += points;
}
Now, in any Actor subclass, using a World subclass name of MyWorld, you can do the following:
1
2
3
4
5
// getting the game score
int score = ((MyWorld)getWorld()).getGameScore();
 
// bumping the game score
((MyWorld)getWorld()).bumpGameScore(20);
You can expand on the 'bumpGameScore' method to check for game over. Like if you wanted that a game score of 100 wins:
1
2
3
4
5
6
7
8
9
public void bumpGameScore(int points)
{
    gameScore += points;
    if (gameScore >= 100)
    {
        addObject(new GameOver(), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }
}
LimeLava LimeLava

2016/10/18

#
I have done all of this and have it in all of the places so my code now looks like this. The World (Bloodstream)
1
2
3
4
5
6
7
8
9
10
11
12
public class Bloodstream extends World
{
    int time;
    private int score;
    public int getscore()
    {
        return score;
    }
    public void bumpscore(int points)
    {
        score += points;
    }
White Cell the cell that eats the other stuff and then adds the points
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void checkCollision()
    {
        if(isTouching(Virus.class))
            {
                removeTouching(Virus.class);
                ((Bloodstream)getWorld()).bumpscore(20);
                getWorld().showText("Score" + score,80,25);
            }
        if (isTouching(Bacteria.class))
            {
                removeTouching(Bacteria.class);
                ((Bloodstream)getWorld()).bumpscore(10);
                Greenfoot.playSound("slurp.wav");
                getWorld().showText("Score:" + score,80,25);
           }
    }
Bacteria (One of the things getting eaten)
1
2
3
4
5
6
if (getX() == 0)
        {
            ((Bloodstream)getWorld()).bumpscore(-15);
            getWorld().showText("Score:" + score,80,25);
            getWorld().removeObject(this);
        }
Virus (Another thing getting eaten and like Bacteria removes score if it touches the wall)
1
2
3
4
5
6
if (getX() == 0)
        {
            ((Bloodstream)getWorld()).bumpscore(-100);
            getWorld().showText("Score:" + score,80,25);
            getWorld().removeObject(this);  
        }
I have int score = ((Bloodstream)getWorld()).getscore(); at the top of all of them except Bloodstream But then i have a error when i try to load the world, this is the error java.lang.NullPointerException at WhiteCell.<init>(WhiteCell.java:12) at Bloodstream.prepare(Bloodstream.java:81) at Bloodstream.<init>(Bloodstream.java:29) These are the places it highlights when i click on them 1.
1
2
3
public class WhiteCell extends Actor
{
    int score = ((Bloodstream)getWorld()).getscore();
2. This is in the stuff from prepare when it starts the world
1
WhiteCell whitecell = new WhiteCell();
3. That is is the constructor for the world
1
prepare();
danpost danpost

2016/10/18

#
Here is what is happening: your world constructor calls the prepare method, which create a new WhiteCell object causing the that new object's fields created and assigned their default values followed by running the constructor of it class. At this point, the object is not yet in any world; yet, you are trying to assign a value to the score field via world the actor is in. But, because it is not in any world, you cannot get a score value via the 'getWorld' method. Hence the 'nullPointerException'. Again, the problem is you are trying to keep a score field in more than one location. Having the one in the world is fine; but, the actor needs to get is value from the world when it needs it, not when it is created. Remove:
1
int score = ((Bloodstream)getWorld()).getscore();
from the WhiteCell class. It is not that you cannot use that line of code within the class -- it is that you must use it from within a method that is called from the act method (or from the act method itself); and you only need to use it when needed (like when the score is changed so you can check the score for game over, for example.
LimeLava LimeLava

2016/10/19

#
What I am getting from this is that I need to move the line that you just stated to the act method. Or should I move it to the area where I add score for touching the virus of bacteria?
danpost danpost

2016/10/19

#
LimeLava wrote...
What I am getting from this is that I need to move the line that you just stated to the act method. Or should I move it to the area where I add score for touching the virus of bacteria?
Move it to where you add score for touching the virus of bacteria -- and anywhere else you add to the score value. It should be placed immediately after where the score is changed. Then you can ask if the score has reached a specific value to trigger game over.
LimeLava LimeLava

2016/10/19

#
OK thanks.
LimeLava LimeLava

2016/10/20

#
I have all of that and It works now but it never adds or removes score when I touch the Virus of Bacteria or when they touch the wall. I have the amount i want to remove or add in the
1
((Bloodstream)getWorld()).bumpscore(20);
danpost danpost

2016/10/20

#
LimeLava wrote...
I have all of that and It works now but it never adds or removes score when I touch the Virus of Bacteria or when they touch the wall. I have the amount i want to remove or add in the
1
((Bloodstream)getWorld()).bumpscore(20);
Will need to see your Bloodstream class to find out what is happening.
LimeLava LimeLava

2016/10/20

#
OK Ill just give you the whole thing except for the prepare stuff.
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
public class Bloodstream extends World
{
    int time;
    private int score;
    public int getscore()
    {
        return score;
    }
    public void bumpscore(int points)
    {
        score += points;
    }  
    /**
     * Constructor: Set up the staring objects.
     */
    public Bloodstream()
    {   
        super(780, 360, 1);
        setPaintOrder(Border.class);
        prepare();
        showText("Score:" + score,80,25);
        time = 2000;
    }
    public void ShowEndMessage()
    {
      
    }
 
    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
             
        }
        if (Greenfoot.getRandomNumber(100) < 2)
        {
            Lining lining = new Lining();
            addObject(new Lining(), 779, 359);
        }
        if (Greenfoot.getRandomNumber(100) < 2)
        {
            Lining lining2 = new Lining();
            addObject(new Lining(), 779, 1);
        }
        if (Greenfoot.getRandomNumber(100) <1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(359));
        }
        if (Greenfoot.getRandomNumber(100) <6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(359));
        }
        time = time -1;
        showText("Time:" + time,650,25);
        score = 0;
        if (time==0)
        {
            Greenfoot.stop();
             
        }
        text();
    }
danpost danpost

2016/10/20

#
Insert a copy of line 21 after line 11. Also, remove line 59 (why are you zeroing the score in the act method like that?).
There are more replies on the next page.
1
2