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

2014/11/24

Game Over Screen

MIKA MIKA

2014/11/24

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

/**
 * You play this tank.
 * 
 * @author: Mika Filoteo
 * @version
 */
public class Good_Tank extends Actor
{
    World world;
    int f=0;
    int m=0;
    int mcount=0;
    int count=0;

    public void act() 
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(5);
        }   
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);  
        }   
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5); 
        }   
         if(Greenfoot.isKeyDown("left"))
        {
            setRotation(0);
            move(-5);
        }   
        if(Greenfoot.isKeyDown("z"))
        {
            f++;
            if(f==1)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation());
            }
            if(f==50)
                f=0;
        }
        else
            f=0;
       
        Actor Good_Tank;
        Good_Tank = getOneObjectAtOffset(0,0,Good_Tank.class);  
        if (Good_Tank != null)  
        {  
            Greenfoot.getWorld(new Game_Over_Screen());
            world.removeObjects(world.getObjects(null));  
        }  
        if (world != null) 
        {  
            world.removeObjects(world.getObjects(null));  
        }  
    }
}
I created a separate world for the game over screen but it just doesn't appear. Also, I keep receiving the "cannot find symbol" error at the getWorld(Game_Over_Screen) part.
danpost danpost

2014/11/24

#
That is because there is no method called 'getWorld' in the Greenfoot class.
davmac davmac

2014/11/24

#
Also, I keep receiving the "cannot find symbol" error at the getWorld(Game_Over_Screen) part
Probably because:
  • first, you're using 'Game_Over_Screen' as if it was a variable. It doesn't appear to be defined as a variable. Maybe you meant 'new Game_Over_Screen()'.
  • Secondly: in any case, there is no method in the Greenfoot class called 'getWorld'. You probably meant to call 'setWorld'.
MIKA MIKA

2014/11/24

#
Hi, I fixed that problem! :D I have a new problem now.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 
 * 
 * @author: Mika Filoteo 
 * @version: 11/24/2014
 */
public class Evil_Bullet extends Actor
{
    int count=0;
    
    /**
     * Act - do whatever the Evil_Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        count++;
        move(-9);
         
        World world = getWorld();
        
        Actor Good_Tank;
        Good_Tank = getOneObjectAtOffset(0, 0, Good_Tank.class);
        
        if(Good_Tank !=null)
        {
            world.removeObject(Good_Tank); 
            world.addObject(new Explosion(), getX(), getY());
            world.removeObject(this); 
            Greenfoot.playSound("Explosion.wav"); 
            endGame();
        }
        else
        {
        if(getX() <= 5)
                setLocation(world.getWidth()-8, getY());
        if(getX() >= world.getWidth()-50)
                setLocation(10, getY());
        if(getY() <= 5)
                setLocation(getX(), world.getHeight()-8);
        if(getY() >= world.getHeight()-50)
                setLocation(getX(), 10);
        }
        if(count==55)
                world.removeObject(this);
    }
    
    public void endGame()
    {
        Game_Over_Screen game_over_screen = new Game_Over_Screen();
        Greenfoot.setWorld(game_over_screen);
        ((Game_World)getWorld()).sound2.stop(); 
    }
    }
I've been getting the same terminal window message over and over again, and I don't know how to fix it. :( java.lang.NullPointerException at Evil_Bullet.endGame(Evil_Bullet.java:54) at Evil_Bullet.act(Evil_Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
MIKA MIKA

2014/11/24

#
Line 54 is apparently problematic? Here is my Game_World code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This world is where you play the game.
 * 
 * @author: Mika Filoteo
 * @version: 11/24/2014
 */
public class Game_World extends World
{
    int soundCount = 1;
    GreenfootSound sound2 = new GreenfootSound("Evil.mp3");   

    public Game_World() 
    {    
        super(700, 550, 1); 
        prepare(); 
        sound(); 
        stop();
    }
    
    public void prepare()
    {
        Good_Tank good_tank = new Good_Tank();
        addObject(good_tank, 100, 265);
        Evil_Tank evil_tank = new Evil_Tank();
        addObject(evil_tank, 587, 262);
    }

    public void stop()
    {
        sound2.pause();  
    }
   
    public void sound()    
    {    
        if (soundCount == 1 && sound2 != null)    
        {              
           sound2.playLoop(); 
           soundCount++;
        }    
    }  
}
danpost danpost

2014/11/24

#
You are probably getting a NullPointerException with a trace line something like this:
at Evil_Bullet.endGame(Evil_Bullet.java:54)
This line says that on line 54 of the Evil_Bullet class in the 'endGame' method is where the error occurred. Before calling the 'endGame' method, you removed the actor from the world (line 31) which will now have 'getWorld' return a 'null' value. Since you are changing worlds anyway, it is alright to leave the actor in this world; so, just remove line 31. EDIT: posted prior to seeing the last post and the edited version of the post prior to that (which added the trace).
MIKA MIKA

2014/11/24

#
It worked! Thank you very much, you are a lifesaver!
You need to login to post a reply.