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

2014/7/8

Some one help me spot the issues?

Amightymuffin Amightymuffin

2014/7/8

#
ok so im trying to cap the number of wombats spawning so they dont explode and lag the prgram to death, but now they dont spawn at all?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Road here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Road extends World
{
   int delay = 1;
    /**
     * Constructor for objects of class Road.
     * 
     */
    public Road()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 

        prepare();
        spawn();
    }


    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        BABY1 baby1 = new BABY1();
        addObject(baby1, 49, 630);
        BABY1 baby12 = new BABY1();
        addObject(baby12, 62, 361);
        BABY1 baby13 = new BABY1();
        addObject(baby13, 80, 181);
        baby13.setLocation(60, 319);
        baby1.setLocation(61, 406);
        Wombat wombat = new Wombat();
        addObject(wombat, 736, 329);
    }
    public void spawn()  
    {  
       if (Greenfoot.getRandomNumber(100) == 1)   
        {  
         if (getObjects(Wombat.class).size() < 300) 
         {    
          addObject( new Wombat(),500+Greenfoot.getRandomNumber(798),1+Greenfoot.getRandomNumber(798));    
         }  
        }
    }
}
and in the wombat there is not much as i put the spawning stuff in the world file
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;  
/**
 * Write a description of class Wombat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Wombat extends Actor
{
 int delay = 1;
    /**
     * Act - do whatever the Wombat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-4);
        
        int yCoord = getY();
        int xCoord = getX();
       // if (yCoord <= 0 || yCoord >= 799)
        //{
        //    turn (180);
        //}
        if (xCoord ==0)
                {
                    World Road = getWorld();
                    Road.removeObject (this);
                }
        
                
          
    
    
}
}
danpost danpost

2014/7/8

#
Your 'spawn' method is only called one time -- at the time you instantiate your world (during compilation or resetting of the program). This is because you placed the call to it in your world constructor block, 'public Road()'. You, instead, need to call it from an 'act' method. If you did not know, the 'act' method is an empty World class method (there is also one in the Actor class) that you can override to give your world things to do while it is the active world (and actors things to do while they are in the active world). You just need to remove line 23 from the Road class and add the following method to it:
public void act()
{
    spawn();
}
BTW, you do not need to import 'java.util.List' unless you are creating a reference to a List object. You can use methods that return List objects and execute methods on them using the 'dot' operator without needing the import (as line 48 in your Road class).
Amightymuffin Amightymuffin

2014/7/8

#
thanks for the help i forgot about the act() function i think this now crashes when the dolphins hit the end of the screen?
public class Dolphin extends Actor
{
    /**
     * Act - do whatever the Dolphin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(7);
        Actor actor = getOneIntersectingObject (Wombat.class);
         {
            
                int xCoord = getX();
                if (xCoord >= 799)
                {
                    World Road = getWorld();
                    Road.removeObject (this);
                }
                 
           if (actor != null)
            {
                Road RoadWorld = (Road)getWorld();
                RoadWorld.removeObject (actor);
                RoadWorld.removeObject (this);
                
         }
        }
}
}
You need to login to post a reply.