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

2018/9/17

Hitting Walls

CosmicCaleb CosmicCaleb

2018/9/17

#
I want to make it so that when my one bacteria hits the left side of the world, all the bacteria speed up by one speed. They start at -6, and when one hits the wall they speed up to -7 and so on. I have this code but it totally does not work
 /**
     * Float along the bloodstream, slowly rotating.
     */
    public void act() 
    {
        edge();
        turn(2);

        
        setLocation(getX()-6, getY());
        
        if (isAtEdge())
        {
            getWorld().removeObject(this);
            
        }
    }
    public void edge()
    {
        if (getX() < 2)
        {
            setLocation(getX()-1, getY());
        }
    }
}
danpost danpost

2018/9/17

#
You will need a field to hold the current speed. Not only that, it must be a single field that all bacteria can access. Therefore, it must either be a normal field placed in your World subclass or a static field in your Bacteria class and initially set by your World constructor. You can then use it in your Bacteria class for moving and increment it when the left edge is reached. Remove the edge method (lines 18 through 24) from the class as well as the call to it from the act method (line 6). Then, replace line 14 with what you had at line 22 (unless you add more bacteria via the act method in your subclass of World).
CosmicCaleb CosmicCaleb

2018/9/18

#
I'm pretty new to this, so that sounded like Spanish. Could I have some code to see what you mean?
danpost danpost

2018/9/18

#
CosmicCaleb wrote...
I'm pretty new to this, so that sounded like Spanish. Could I have some code to see what you mean?
A field is a variable that is declared within a class, but outside of any code blocks. A variable declared within a code block only exists during the execution of the block. Once an execution block terminates, variables declared within the block no longer exist. Example 1:
public void act()
{ // beginning of a code block
    int speed = 6; // declaring of a variable
    move(speed); // speed will always equal '6' here
    speed++; // increments value (just to further show what happens)
} // end of code block ( 'speed' no longer exists)
Example 2:
public void act()
{
    if (true)
    {
        // beginning of 'if' block
        int speed = 6; // variable declared
        move(speed); // speed will always equals '6' here
        speed++; // increments value (just to further show what happens)
    } // end of 'if' block ('speed'' no longer exists)
}
Example 3:
private int speed = 6; // variable declared (aka:  instance field)

public void act()
{
    move(speed); // speed will increase each act
    speed++; // increments value (just to further show what happens)
}
In example 3, each actor will have its own speed value. That is, line 6 will only increase the speed of one actor (the actor that the method was called on) -- not all actors of its type. Notice that the variable is declared in the block of the class -- not in an executable code block (constructor or method). Example 4:
public static int speed = 6; // 'class' variable declared (aka: static field)

public void act()
{
    move(speed);
    if (true) speed++;
}
In example 4, all actors of the class' type will move at whatever value the speed variable is set to when acted on. The value is initially set when the project is compiled and does change when the project is reset. Therefore, it must be initially set (or reset) by code in the world constructor:
public class MyWorld extends World
{
    public MyWorld()
    {
        super(600, 400, 1);
        Bacteria.speed = 6;
        // prepare world here
    }
}
You need to login to post a reply.