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

2016/5/17

Gliding up and down randomly while moving left

gmacias gmacias

2016/5/17

#
Trying to make this diver move up and down randomly while he travels left. Can't figure out how to do it :-/ Diver Class
public class Diver extends Actor
{
    /**
     * Contructor: Initialise the floating speed to a random value.
     */
    public Diver()
    {
        
    }
    
    /**
     * Float along. Disappear when reaching the left edge.
     */
    public void act() 
    {
        setLocation(getX()-10, getY());
        
        if (getX() == 0) 
        {
            getWorld().removeObject(this);
        }
    }    
}
valdes valdes

2016/5/18

#
You could do something like this First, declare two instance variables
    private int valueY;
    private int time = 0;
movement code
        if (time<=0) {
// 50-50 chance for going up or down
                if (Greenfoot.getRandomNumber(2) == 0) {
// 10 is number of pixels up, you can chance it
                    valueY = getY() + 10;
// 149 is your bottom limit of the world, change it
                    if (valueY > 149) valueY = 149;
                } else {
// 10 is number of pixels down, you can chance it
                    valueY = getY() - 10;
// 50 is your top limit of the world, change it
                    if (valueY < 50) valueY = 50;
                }
            time = 30; // number of cycles in the same Y coordinate, you can change it
        } else {
            time--;
        }
        setLocation(getX() - 10, valueY);        
        // here goes your code for reaching end of world
gmacias gmacias

2016/5/19

#
Thank you, valdes! I will try it.
valdes valdes

2016/5/19

#
Let me know if that is what you wanted or if I understand it incorrectly
gmacias gmacias

2016/5/22

#
Hi Valdes. OK, I tried it but all the divers tend to go to the top of the world. I would like them to be more spread out on the Y axis
danpost danpost

2016/5/22

#
You could set and change some random target y values to go to. Something like this:
// instance field
private yTarget =200;

// in act
int yDirection = (int)Math.signum(yTarget-getY());
setLocation(getX()-10, getY()+yDirection*10);
if (yDirection == 0 || (int)Math.signum(yTarget-getY()) != yDirection)
{
    yTarget = Greenfoot.getRandomNumber(getWorld().getHeight());
}
valdes valdes

2016/5/23

#
Yes @gmacias, they are on top, because I put it that way. The objects are moving between the pixels 50 and 149, in my code change those values to the ones you want. Also you have to check out your code for creating the divers, to spread them in the beggining of the game. If you wanted a zigzag motion, then use @danpost code. It will move the objects along the entire Y axis. If you want to restrict that, change the random number generated for yTarget.
gmacias gmacias

2016/5/23

#
Still not working. I'm thinking we need to create a separate variable for setting the location?? Not sure though. Here is what I have:
//Diver Class:
public class Diver extends Actor
{
    private int valueY;
    private int time = 0;
  
    /**
     * This method makes divers float along while moving up and down randomly. 
     * They disappear when they reach the left edge. If the diver touches the 
     * shark, the game will stop. 
     */
    public void act() 
    {
        setLocation(getX() - 1, valueY);
        
        if (time<=0) 
        {
            if (Greenfoot.getRandomNumber(2) == 0) 
            {
                valueY = getY() + 10;
                if (valueY > getWorld().getHeight()) 
                valueY = getWorld().getHeight();
            } 
            else 
            {
                valueY = getY() - 10;
                if (valueY < 10) 
                valueY = 10;
            }
            time = 30;
        } 
        else 
        {
            time--;
        }

        if (getX() == 0) 
        {
            getWorld().removeObject(this);
        }
        else
        {
            if (isTouching(Shark.class))
            {
                Greenfoot.stop(); 
            }
        }
    }    
}
World Class:
public class Ocean extends World
{
    /**
     * This constructor creates the size and color of the Ocean. 
     */
    public Ocean()
    {    
        super(780, 360, 1); 
        Color ocean = new Color (0, 0, 104);
        getBackground().setColor(ocean);
        getBackground().fill();
        prepare();
    }

    /**
     * This method adds various objects to the ocean scene a random intervals
     * and locations. 
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Fish(), 779, Greenfoot.getRandomNumber(360));
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new SeaStar(), 779, Greenfoot.getRandomNumber(360));
        }
        
        if (Greenfoot.getRandomNumber (1000) < 1)
        {
            addObject(new Diver(), 779, Greenfoot.getRandomNumber(360));
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Seaweed(), 779, 325);
        }
        
        if (Greenfoot.getRandomNumber(1000) < 1)
        {
            addObject(new Boat(), 779, 10);
        }
    }

    /**
     * This method prepares the starting scene with a Shark and Seaweed.
     */
    private void prepare()
    {
        Shark whitecell = new Shark();
        addObject(whitecell,55,188);
        Seaweed Seaweed = new Seaweed();
        addObject(Seaweed,194,202);
        Seaweed Seaweed2 = new Seaweed();
        addObject(Seaweed2,318,156);
        Seaweed Seaweed3 = new Seaweed();
        addObject(Seaweed3,385,164);
        Seaweed Seaweed4 = new Seaweed();
        addObject(Seaweed4,452,155);
        Seaweed Seaweed5 = new Seaweed();
        addObject(Seaweed5,537,175);
        Seaweed.setLocation(42,323);
        Seaweed2.setLocation(71,309);
        Seaweed3.setLocation(236,312);
        Seaweed4.setLocation(470,303);
        Seaweed5.setLocation(501,320);
        Seaweed Seaweed6 = new Seaweed();
        addObject(Seaweed6,417,98);
        Seaweed6.setLocation(440,310);
        Seaweed Seaweed7 = new Seaweed();
        addObject(Seaweed7,687,166);
        Seaweed7.setLocation(739,318);
    }
}
valdes valdes

2016/5/23

#
The error is that you have line setLocation(...) at the top of your act method(). In that case, valueY has no value, so it takes the default, zero. That is why all objects appear at the top. add the following method to Diver class
    protected void addedToWorld(World world) {
        valueY = getY();
    }
That method is called automatically by Greenfoot when you insert an object into a world. This method sets valueY with the original value you created from the random insertion into the world. It should work fine now, I tested it.
gmacias gmacias

2016/5/23

#
It works perfectly now! Thank you, @valdes!
You need to login to post a reply.