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

2016/11/1

im trying to spawn a drone every time i = 4

hockeydude1155 hockeydude1155

2016/11/1

#
public class Drone extends Mover
{
    boolean gameStarted = true;
    //int randSpawn = (Greenfoot.getRandomNumber(300)+50);
    /**
     * Act - do whatever the Drone wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (gameStarted == true)move (-4);
        DroneSpawn();
    }    

    public void DroneSpawn()
    {
        int i = 0;    
        if(i == 4)
        {
            Drone drone = new Drone();
            getWorld().addObject(drone, 900, 200);
            i = 0;
            System.out.println("Drone in game");            
        }
	    while (i < 4) {
	    	i++;
	    	System.out.println(i);
	    }
    }
}
hockeydude1155 hockeydude1155

2016/11/1

#
there is no print out in greenfoot and no drone spawns in
hockeydude1155 hockeydude1155

2016/11/1

#
there is no printout that happens when greenfoot runs it*
hockeydude1155 hockeydude1155

2016/11/1

#
i move he code to the city witch is actual myWorld if you don't change the name and it still doesn't work here's the new code
danpost danpost

2016/11/1

#
hockeydude1155 wrote...
there is no print out in greenfoot and no drone spawns in
That is because 'i' reaches 4 after the 'if' block and is re-initialized to zero the next time the method is executed.
hockeydude1155 hockeydude1155

2016/11/1

#
public class City extends World
{
    public City()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 400, 1, false); 
        populateWorld();
        DroneSpawn();
    }
    public void DroneSpawn()
    {
        int i = 0;    
        if(i == 4)
        {
            Drone drone = new Drone();
            addObject(drone, 900, 200);
            i = 0;
            System.out.println("Drone in game");            
        }
        while (i < 4) {
            i++;
            System.out.println(i);
        }
    }
danpost danpost

2016/11/1

#
Try
i=(i+1)%4;
if (i == 0) addObject(new Drone(), 900, 200);
Your line 12 needs to be moved outside the method, though.
You need to login to post a reply.