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

2017/3/17

Avoid randomly generated moving platforms from overlapping

beshie beshie

2017/3/17

#
I'm trying to make a platformer game using icecaps as the platforms. I've already made the icecaps spawn within a certain range and move at a constant speed. My only problem is how to prevent icecaps from generating over each other? I wanted the icecaps to appear at a random location at a constant frequency, but one by one, so that it wouldn't overlap each other. Can anyone help me?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {    
        // Create a new world with 600x450 cells with a cell size of 1x1 pixels.
        super(600, 450, 1);
        prepare();
        addObject(counter, 560, 40);
    }   
    
    public Counter getCounter()
    {
        return counter;
    }

    public void act()
    {
        if(Greenfoot.getRandomNumber(100) < 2)
        {
            addObject(new Ice(), 599, Greenfoot.getRandomNumber(250)+250);
        }
        if(Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Coin(), 599, Greenfoot.getRandomNumber(250)+100);
        }
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the in/**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int sampleMethod(int y)
    {
        return y;
    }
    
    private void prepare()
    {
        Counter counter = new Counter();
        addObject(counter,515,51);
        counter.setLocation(540,26);
        counter.setLocation(529,30);
        counter.setLocation(530,30);
        Ice ice = new Ice();
        addObject(ice,71,364);
        Bailey bailey = new Bailey();
        addObject(bailey,87,320);
        bailey.setLocation(80,315);
        counter.setLocation(578,26);
        counter.setLocation(570,30);
        Coin coin = new Coin();
        addObject(coin,210,230);
        Coin coin2 = new Coin();
        addObject(coin2,320,150);
        Coin coin3 = new Coin();
        addObject(coin3,460,92);
        Coin coin4 = new Coin();
        addObject(coin4,415,290);
        removeObject(coin);
        removeObject(coin4);
        removeObject(coin2);
        removeObject(coin3);
        Coin coin5 = new Coin();
        addObject(coin5,201,252);
        Coin coin6 = new Coin();
        addObject(coin6,337,201);
        Ice ice2 = new Ice();
        addObject(ice2,319,340);
        ice2.setLocation(297,332);
        Ice ice3 = new Ice();
        addObject(ice3,510,390);
        coin5.setLocation(306,287);
        coin6.setLocation(521,349);
        Bes bes = new Bes();
        addObject(bes,79,56);
        bes.setLocation(71,40);
        removeObject(counter);
        removeObject(bes);
    }
}    
      
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ice here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ice extends Actor
{
    /**
     * Act - do whatever the Ice wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY());
        move(-2);
        if (getX() == 0)
        {
           getWorld().removeObject(this);
        }
    }
}
danpost danpost

2017/3/17

#
Add the following to the Ice class:
protected void addedToWorld(World world)
{
    while (isTouching(Ice.class))
    {
        setLocation(599, Greenfoot.getRandomNumber(250)+250);
}
Just be sure that there is room for one at all times you try to add one or the scenario may appear to "freeze".
You need to login to post a reply.