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

2016/3/9

Scoring

lexboy112333 lexboy112333

2016/3/9

#
So I am making a White Blood Cell game. My objective of this forum is to remove 1 score when the virus touches the Blood cell. But when ever it touches the virus, it subtracts it for every millisecond its touching the virus. Can anyone help me with this?
1
2
3
4
5
6
7
8
9
10
public void addScore()
     {    
       if (isTouching(WhiteCell.class))
        {
             
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(-1); 
                      
                   }
     }
danpost danpost

2016/3/9

#
You are tracking the time that it is touching a bloodstream object and not counting the instances of making contact with a bloodstream object. To count the initial contacts, you will need to introduce an instance field to track the state of contact. This can be a boolean field, an int field, or an object reference field. It could even be an array of object references (which would be a lot harder to implement). A boolean will only indicate that one was touched and not track how many may be in contact. An int can track how many are in contact at a time, which may catch most of the contact (if one move off while another moves on, that change will not be tracked). A single object reference would do a little better than a boolean in that secondary contacts will be found as soon as the one referenced moves off. Using an array of object references can be used to catch all contacts; but, it would be much more difficult to code and CPU intensive, to boot.
lexboy112333 lexboy112333

2016/3/9

#
I am still lost. So currently, the virus collides with the blood cell and it subtracts 1 point for every pixel that it is colliding with. Now, if I made the blood cell remove the Virus it would subtract 1 but I want it to go through the blood cell. Can you post a example of what you mean?
danpost danpost

2016/3/9

#
lexboy112333 wrote...
So currently, the virus collides with the blood cell and it subtracts 1 point for every pixel that it is colliding with.
That is not quite what is happening. It is subtracting one for every act cycle that the two are touching. A boolean field might be used as follows:
1
2
3
4
5
6
7
8
9
10
11
12
// instance field
private boolean touchingWhiteCell;
 
// action code
if (touchingWhiteCell != isTouching(WhiteCell.class)) // checking for change in state
{
    touchingWhiteCell = ! touchingWhiteCell; // recording change in state
    if (touchingWhiteCell) // checking for initial contact (as opposed to no longer touching)
    {
        ((Bloodstream)getWorld()).addScore(-1);
    }
}
However, if a second white cell touching before the first one un-touches, it will not be caught. The code given will only determine when a change in touching ANY white cell occurs ("touching any" as opposed to "not touching any").
danpost danpost

2016/3/9

#
Using an int field, it might be something like this:
1
2
3
4
5
6
7
8
9
10
11
// instance field
private int numWhiteCellsTouching;
 
// action code
int num = getIntersectingObjects(WhiteCell.class).size();
if (num > numWhiteCellsTouching) // checking if number of white cells touching increased
{
    int scored = num-numWhiteCellsTouching;
    ((Bloodstream)getWorld()).addScore(scored);
}
numWhiteCellsTouching = num; // recording new count of touched white cells
Again, this is not exact as if one touches when one leaves, it will not be detected. It will, however, detect more than using a boolean field if you have your white cells populated to the point where they often get close to each other.
lexboy112333 lexboy112333

2016/3/10

#
Thank you. It helped me a lot.
You need to login to post a reply.