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

2015/2/13

2 or more enemies

MIKA MIKA

2015/2/13

#
I'm a first time programmer. :( What would be the code I need to like switch to a "next level" screen after killing 2 enemies? I'm trying to make a tank game, and here is the code for the good tank's bullet:
    int count=0;

    public void act() 
    {
        count++;
        move(10);
        
        World world = getWorld();
        
        Actor Evil_Tank;
        Evil_Tank = getOneObjectAtOffset(0, 0, Evil_Tank.class);
        if(Evil_Tank !=null)
        {
            world.removeObject(Evil_Tank);
            world.addObject(new Explosion(), getX(), getY());
            Greenfoot.playSound("Explosion.wav");
            victory();
        }
        else
        {
        {
        if(getX() <= 5)
                setLocation(world.getWidth()-8, getY());
        if(getX() >= world.getWidth()-5)
                setLocation(10, getY());
        if(getY() <= 5)
                setLocation(getX(), world.getHeight()-8);
        if(getY() >= world.getHeight()-5)
                setLocation(getX(), 10);
        }
        if(count==55)
                world.removeObject(this);
        }
    }
    
    public void victory()
    {
        Win_Screen win_screen = new Win_Screen();
        Greenfoot.setWorld(win_screen);
        ((Game_World)getWorld()).sound2.stop();
    }
}
I sort of don't know what to do if there's more than 1 tank. How do I go from the game world to the "proceed to the next level" screen?
danpost danpost

2015/2/13

#
There is more than one way to accomplish it and the best way for you is not readily apparent. Here are several possibilities: (1) use an int field in your world subclass to track the number of evil tanks destroyed and add a method to bump the count and check its value for changing worlds in the class; (2) add a static int field in the good tank bullet class to track the number of evil tanks destroyed and do like-wise in this class (you will also need to zero the field in your world subclass constructor); (3) have the world act method detect when the number of evil tanks in the world has dropped to a specific goal (2 less than then number originally added to the world) using 'getObjects(...).size()'.
MIKA MIKA

2015/2/13

#
thank you! :D what would the code for the first option look like?
danpost danpost

2015/2/13

#
MIKA wrote...
thank you! :D what would the code for the first option look like?
In your World subclass:
private int evilTanksDestroyed;
would be declaring the field and
public void bumpEvilTanksDestroyed()
{
    evilTanksDestroyed++;
    if (evilTanksDestroyed == 2) {
    // etc.
}
would be the method called from the good tank bullet class to up the counter and check its value. The method should be called from the good tank bullet class before the bullet is removed from the world upon hitting an evil tank, using
((Game_World)getWorld()).bumpEvilTanksDestroyed();
MIKA MIKA

2015/2/13

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

/**
 * Write a description of class Bullet_Level_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet_Level_2 extends Actor
{
    int count=0;
    private int evilTanksDestroyed;
    
    public void act() 
    {
        bumpEvilTanksDestroyed();
        count++;
        move(10);
         
        World world = getWorld();
        
        Actor Evil_Tank_2;
        Evil_Tank_2 = getOneObjectAtOffset(0, 0, Evil_Tank_2.class);
        if(Evil_Tank_2 !=null)
        {
            world.removeObject(Evil_Tank_2);
            Greenfoot.playSound("Explosion.wav");
        }
        else
        {
        {
        if(getX() <= 5)
                setLocation(world.getWidth()-8, getY());
        if(getX() >= world.getWidth()-5)
                setLocation(10, getY());
        if(getY() <= 5)
                setLocation(getX(), world.getHeight()-8);
        if(getY() >= world.getHeight()-5)
                setLocation(getX(), 10);
        }
        if(count==55)
                world.removeObject(this);
        }
    }
    public void bumpEvilTanksDestroyed()
    {
        evilTanksDestroyed++;
        if (evilTanksDestroyed == 2)
        {
            Win_Screen win_screen = new Win_Screen();
            Greenfoot.setWorld(win_screen);
            ((Game_World_Level_2)getWorld()).sound2.stop();
        }
    }
}
that's my code for the bullet class. :( for some reason, the victory screen appears right as i press "z" to shoot.
MIKA MIKA

2015/2/13

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

/**
 * Write a description of class Game_World_Level_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game_World_Level_2 extends World
{
    int soundCount = 1;
    GreenfootSound sound2 = new GreenfootSound("Evil.mp3");
    private int evilTanksDestroyed;
    
    /**
     * Constructor for objects of class Game_World_Level_2.
     * 
     */
    public Game_World_Level_2()
    {    
        super(700, 550, 1);
        sound();
        prepare();
        next();
    }
    public void prepare()
    {
        Good_Tank_2 good_tank_2 = new Good_Tank_2();
        addObject(good_tank_2, 100, 265); 
        Evil_Tank_2 evil_tank_2 = new Evil_Tank_2(); 
        addObject(evil_tank_2, 566, 122);
        Evil_Tank_2 evil_tank_22 = new Evil_Tank_2();
        addObject(evil_tank_22, 566, 400);
    }
    public void stop()
    {
        sound2.pause();  
    }
    public void sound()    
    {    
        if (soundCount == 1 && sound2 != null)    
        {              
           sound2.playLoop();  
           soundCount++;
        }    
    }  
    public void next()
    {
        if (getObjects(Evil_Tank_2.class).isEmpty()) 
        {
          Greenfoot.setWorld(new Pre_Game_World_Level_3());
        }
    }
    }
that's the game world code.
danpost danpost

2015/2/13

#
You cannot call 'next' from the world constructor block -- it must be called from the act method (in Game_World_Level_2 class);
public void act()
{
    next();
}
In the Bullet2 class, remove lines 12, 16 and everything from line 45 on. At line 27 in the Bullet2 class, insert the following line:
world.bumpEvilTanksDestroyed();
and change line 20 to this:
Game_World_Level_2 world = (Game_World_Level_2) getWorld();
MIKA MIKA

2015/2/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet_Level_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet_Level_2 extends Actor
{
    int count=0;
     
    public void act() 
    {

        count++;
        move(10);
          
        Game_World_Level_2 world = (Game_World_Level_2) getWorld();
         
        Actor Evil_Tank_2;
        Evil_Tank_2 = getOneObjectAtOffset(0, 0, Evil_Tank_2.class);
        if(Evil_Tank_2 !=null)
        {
            world.removeObject(Evil_Tank_2);
            Greenfoot.playSound("Explosion.wav");
            world.bumpEvilTanksDestroyed();
        }
        else
        {
        {
        if(getX() <= 5)
                setLocation(world.getWidth()-8, getY());
        if(getX() >= world.getWidth()-5)
                setLocation(10, getY());
        if(getY() <= 5)
                setLocation(getX(), world.getHeight()-8);
        if(getY() >= world.getHeight()-5)
                setLocation(getX(), 10);
        }
        if(count==55)
                world.removeObject(this);
        }
    }
}
That's the new Bullet_Level_2 class code! It still says there's an error, though. It cannot find the symbol - method bumpEvilTanksDestroyed()
MIKA MIKA

2015/2/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Game_World_Level_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game_World_Level_2 extends World
{
    int soundCount = 1;
    GreenfootSound sound2 = new GreenfootSound("Evil.mp3");
    private int evilTanksDestroyed;
     
    /**
     * Constructor for objects of class Game_World_Level_2.
     * 
     */
    public Game_World_Level_2()
    {    
        super(700, 550, 1);
        sound();
        prepare();
    }
    public void prepare()
    {
        Good_Tank_2 good_tank_2 = new Good_Tank_2();
        addObject(good_tank_2, 100, 265); 
        Evil_Tank_2 evil_tank_2 = new Evil_Tank_2(); 
        addObject(evil_tank_2, 566, 122);
        Evil_Tank_2 evil_tank_22 = new Evil_Tank_2();
        addObject(evil_tank_22, 566, 400);
    }
    public void stop()
    {
        sound2.pause();  
    }
    public void sound()     
    {    
        if (soundCount == 1 && sound2 != null)    
        {              
           sound2.playLoop();  
           soundCount++;
        }    
    }  
    public void bumpEvilTanksDestroyed()
    {
        evilTanksDestroyed++;
        if (evilTanksDestroyed == 2) 
        {
            // etc.
        }
    }
}
That's also the edited code for Game_World_Level_2. :( There is no more error. In fact, the problem is that when the 2 tanks disappear, nothing happens.
danpost danpost

2015/2/16

#
You need to put the code for what you want to do when both tanks are destroyed at line 51 of the Game_World_Level_2 class.
You need to login to post a reply.