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

2013/10/16

Create object with counter

sihaco sihaco

2013/10/16

#
Hi guys, I'm working on an airport simulation program for a school project and I've stumbled upon a little problem. I have a world (like all Gf games) and it's called Airport. What I want is to create a counter in the world which starts with a value of 0 and the number increases and when it's equal to a specific number, let's say 10, a new object of the class Aircraft has to be created. The problem is that the counter never reaches the specific number I need, so no new objects of Aircraft are being created. Here's my code of the world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * De klasse Airport dient voor het presenteren van een luchthaven. Via deze klasse worden aanvliegende vliegtuigen
 * bestuurd en worden vertrekkende vliegtuigen in een bepaalde richting gestuurd. 
 * 
 * @author (Erol & Hakan Simsir) 
 * @version (V1, 10/11/2013/)
 */
public class Airport extends World
{

    /**
     * Constructor for objects of class Airport.
     * 
     */
    
    private int counter;

    public Airport()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        constructAirport();
    }
    
    // voeg alle objecten toe aan de wereld
    public void constructAirport(){
        addObject(new Runway(), 510, 100);
        addObject(new Tarmac(), 507, 447);
        addObject(new Gate(), 311, 490);
        addObject(new Gate(), 461, 490);
        addObject(new Gate(), 611, 490);
        addObject(new Gate(), 761, 490);
        addObject(new Terminal(), 517, 563);
        addObject(new Aircraft(), 70, 520);
        createPlane();
        //addObject(new SplashScreen(), 500, 300);
    }
    
    public void createPlane()
    {
        counter = counter + 1;
        if (counter == 10){
            addObject(new Aircraft(), 70, 520);
            counter = 0;
            System.out.println(counter);
        }        
    }
}
Can anyone see the problem? I'm not getting any syntax errors, but it doesn't work because of my mistake, which I can't find. The method createPlane() is the method I'm creating to reach the effect I need. Thanks in advance! BTW, I'm in my first year, so I'm a rookie, please explain a little bit what you say when you explain the problem. Thanks!
erdelf erdelf

2013/10/16

#
u need to call the createPlane method at least 10 times, but u are calling it only 1 time in ur constructor
Zamoht Zamoht

2013/10/16

#
Add the act method to your world and call createPlane() from there.
public void act()
{
    createPlane();
}
If you want to start your scenario with one aircraft you should change constructAirport() a little:
    public void constructAirport(){  
        addObject(new Runway(), 510, 100);  
        addObject(new Tarmac(), 507, 447);  
        addObject(new Gate(), 311, 490);  
        addObject(new Gate(), 461, 490);  
        addObject(new Gate(), 611, 490);  
        addObject(new Gate(), 761, 490);  
        addObject(new Terminal(), 517, 563);  
        addObject(new Aircraft(), 70, 520);  
        counter = 9;
        createPlane();  
        //addObject(new SplashScreen(), 500, 300);  
    }  
sihaco sihaco

2013/10/16

#
Thanks a lot guys! I really appreciate it, it works well now :)
danpost danpost

2013/10/16

#
To make things a little more clear, the code in the constructor ('public ClassName()') is used to process any action that need done to create or initialize the state of the object. It is only called when 'new ClassName()' is executed. There are two methods in the Actor class that the system automatically runs at specific times. These are the 'addedToWorld(World)' method which is executed once just as the object is placed into a world (when the World class method 'addObject' is used) and the 'act' method which is repeatedly executed as long as the actor is in the active world. These two methods are, by default, empty methods and can be overridden in each subclass of Actor. Since the 'act' method is the one that is repeatedly executed, it stands to reason that if you want a value of a field to change and its value to be checked repeatedly, then the code should go in the 'act' method.
You need to login to post a reply.