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

2013/2/15

Balloon Loop

Eli.A Eli.A

2013/2/15

#
Okay, I'm making a game where balloons slowly go across the screen (right-left). You have to shoot the balloons. Everything works (thank God). The only problemo is that I want the balloons to appear on the right side by themselves and then when they get to the end just leave. I want it to be an endless loop pretty much. I would prefer for them to be separated by probably just a random number between 1-3 seconds. can you please help me with the code? Here is my balloon code so far.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ballon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ballon extends Actor
{
    /**
     * Act - do whatever the Ballon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
{
  move(-4);
   if (Greenfoot.getRandomNumber(100)<50)
{
 setLocation(getX(),getY() + 5);
}
if (getX()<10)
{
World world = getWorld();
 world.removeObject(this);
}
}
}
danpost danpost

2013/2/16

#
At this point, the Balloon class moves the object across the screen from right to left, drifting up and down a little, and removing itself if it reaches the left edge of the world. So far, so good. The spawning of the balloons and controlling the rate of spawning is done elsewhere (not in the Balloon class). Usually this kind of thing is done in your world class code. You will need an instance field in the world to act as a timer. It will need to be set to something in the range of about 60 to 180 (about 60 per second for normal speed scenarios). If you named the field 'spawnTimer', then:
// the field declaration would be
private int spawnTimer;
// in the world act method you would have something like
if (spawnTimer>0)
{ // run timer
    spawnTimer--;
}
if (spawnTimer == 0)
{ // add balloon and set timer
    addObject(new Balloon(), getWidth()-10, getHeight()-200);
    spawnTimer=60*(1+Greenfoot.getRandomNumber(3));
}
Eli.A Eli.A

2013/2/16

#
Okay, I copied and pasted your code. It is giving me an error though :( The error is "illegal start of type". I put where the error is located in the code before
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        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()
    {
        Player player = new Player();
        addObject(player, 50, 250);
    }
private int spawnTimer;  
// error is under this line 
if (spawnTimer>0)  
{ // run timer  
    spawnTimer--;  
}  
if (spawnTimer == 0)  
{ // add balloon and set timer  
    addObject(new Balloon(), getWidth()-10, getHeight()-200);  
    spawnTimer=60*(1+Greenfoot.getRandomNumber(3));  
}  
}
You need to login to post a reply.