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

2011/6/11

On fixing 'Infect'

1
2
3
4
danpost danpost

2011/6/11

#
You may have to check to see if a rock is there first. Or (I don't know), maybe being on or under the rock might make you less prone to be infected.
kiarocks kiarocks

2011/6/11

#
What?
danpost danpost

2011/6/11

#
Show me the code there. On previous -- never mind. It was just a thought, but would take more programming.
kiarocks kiarocks

2011/6/11

#
public class InfectWorld extends World
{

    /**
     * Constructor for objects of class InfectWorld.
     * 
     */
    public InfectWorld()
    {
        super(13, 13, 60);

        prepare();
    }
        Greenfoot.setSpeed(identifier expected)(25);
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rock rock = new Rock();
        addObject(rock, 5, 4);
        Rock rock2 = new Rock();
        addObject(rock2, 8, 5);
        Rock rock3 = new Rock();
        addObject(rock3, 5, 8);
        Rock rock4 = new Rock();
        addObject(rock4, 2, 9);
        Rock rock5 = new Rock();
        addObject(rock5, 9, 9);
        Rock rock6 = new Rock();
        addObject(rock6, 10, 6);
        Rock rock7 = new Rock();
        addObject(rock7, 7, 3);
        for (int uninf = 0; uninf < 36; uninf++)
    {
        int myX = Greenfoot.getRandomNumber(15);
        int myY= Greenfoot.getRandomNumber(13);
        UnInfected uninfected = new UnInfected();
        addObject(uninfected, myX, myY);
    } 
        addObject(uninfected8, 5, 2);
        InfectLv1 infectlv1 = new InfectLv1();
        addObject(infectlv1, 11, 1);
        InfectLv2 infectlv2 = new InfectLv2();
        addObject(infectlv2, 13, 3);
       
    }
}
kiarocks kiarocks

2011/6/11

#
the error is in parentheses.
danpost danpost

2011/6/11

#
Move your close bracket '}' to after the setSpeed statement. Also, remove line number 43 from the code listed above.
kiarocks kiarocks

2011/6/11

#
Ok
kiarocks kiarocks

2011/6/12

#
dan, from the other post, they just turn now. they wont move, even at high speeds.
danpost danpost

2011/6/12

#
The speed of execution will not change what it does. Let's see the code for the Actor class (the Act() and related methods).
kiarocks kiarocks

2011/6/13

#
code is there at the scenario. im trying to macke it so that it wont change once it is infected, but how do i do that? Here is my fail code:
/**
     * Check to see if can be infected from Lv1
     */
    public boolean infect()
    {
        if(Image) =ppl3.png;
        {return false;}
        else{
            return ((getObjectsInRange(5, InfectLv1.class)).size() != 0); 
        }
    }
danpost danpost

2011/6/13

#
I'll check it out.
kiarocks kiarocks

2011/6/13

#
ok
kiarocks kiarocks

2011/6/13

#
i think it has to do with the pixels cause they work fine in cell size 25 and above
danpost danpost

2011/6/13

#
I looked at your code in the scenario. All in all, good job for your first project. Still a little rusty on when and when not to use brackets. My style is good for me; I do not know it would be good for you. But, I find it a lot easier to keep my bracketing this way. **************** MY POINTERS ****************************** When adding objects that do not need any changes from what is set for an Actor, it is not neccessary to create a new Object variable for it. Instead of:
	Rock rock = new Rock();
	addObject(rock, myX, myY);
you can simply write:
	addObject(new Rock(), myX, myY);
************************************************************** You wrote in 'public boolean canMove()' (found in several Actor classes):
        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
            return false;
        }
        else if (x < 0 || y < 0) {
            return false;
        }
        List rocks = myWorld.getObjectsAt(x, y, Actor.class);
         if(rocks.isEmpty()) 
         
             return true;
            
        
        else {
            return false;
        }
I would write:
        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { return false; }
        if (x < 0 || y < 0) { return false; }
        // test for Actors
        return ((myWorld.getObjectsAt(x, y, Actor.class)).size() == 0);
The else on line 5 in yours is totally unneccessary as execution only gets to that line in the else case. Again, creating an Object variable is not needed, hence my return statement. As you can see, the code is easier to follow when avoiding unneccessaries. Notice how my 3 executable statement lines does what your 12 executable statement lines do. ALSO: Your 'return true;' statement is missing brackets around it. ************************************************************ Your 'public void act()' for UnInfected class is:
    public void act() 
    {
        if(canMove()) {
            move();
        }
        else {
            turnRandom();
        }
        {getinfected();
        }
        {getinfected2();
        }
    }
The brackets surrounding 'getinfected();' and 'getinfected2();' should not be there. I would write it as follows:
    public void act() 
    {
        if(canMove()) { move(); } else { turnRandom(); }
        getinfected();
        getinfected2();
    }
Can you see how much easier it is to follow? (My 3 lines = your 10 lines) I pray this is of some help to you. Best wishes and may God bless you as you continue on your course.
danpost danpost

2011/6/13

#
As far as preventing infecteds from changing again, give UnInfecteds a boolean (true/false) variable, call it something like 'gotInfected' as such (add the line below 'private int direction')
private boolean gotInfected = false;
Then, when infecting, if this value is 'false', infect and change the value to 'true', else do not infect (again). Actually, you could make it an integer set to zero, and track the stage of infection; the higher the value, the worse the case of infection. If the value is zero, he is not infected yet.
There are more replies on the next page.
1
2
3
4