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

2021/4/14

How do I make actors randomly generated on the screen infinitely?

ExistingProof ExistingProof

2021/4/14

#
I´m making a simple game where the plane moves on the screen and is avoiding clouds on the screen (appearing on the right, moving to the left and then disappearing). How do I make them keep appearing randomly on the right of the screen? Right now they are at fixed locations from the start. Thanks a lot!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(20, 10, 50); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Balon balon = new Balon();
        addObject(balon,13,3);
        Balon balon2 = new Balon();
        addObject(balon2,16,3);
        Lietadlo lietadlo = new Lietadlo();
        addObject(lietadlo,4,5);
        Cloud cloud = new Cloud();
        addObject(cloud,12,6);
        Cloud cloud2 = new Cloud();
        addObject(cloud,16,6);
        cloud.setLocation(16,8);
        cloud2.setLocation(16,5);
    }
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

2021/4/14

#
Add Cloud objects from an act method in your MyWorld class (it is a World method that is repeatedly called by greenfoot while your scenario is running).
You need to login to post a reply.