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

2014/10/19

lives counter not working

1
2
wslade wslade

2014/10/19

#
I have a score counter and lives counter. Both use the Score code but I can't seem to get the lives counter to work properly. It will compile but when the player is hit I get an error 'NullPointerException at Player.act(Player.java:31) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) ' error rather than the lives counter updating. Here is my code. What is wrong with it?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Player extends Actor
{ 
    /**
     * creates integer to hold values for missile fire rate
     */
    private int canShoot = 0;
    private int count = 3;
    private Score lives;
   


    public void act() 
    {
        /**
         * move around and fire missiles, destroyed by enemies, recharges missile fire
         */
        moveUp();
        moveDown();
        moveLeft();
        moveRight();
        fire();
        destroyed();
        canShoot--;
        
      
        
        if(count >= 0);
        {
            getWorld().addObject(new Lives3(), 245, 20);                       
        }
        if (count <= -1);
        {
            getWorld().addObject(new Lives2(), 245, 20); 
        }
        if (count <= -2);
        {
            getWorld().addObject(new Lives1(), 245, 20); 
        }
        if (count <= -3);
        {
            getWorld().addObject(new Lives0(), 245, 20); 
        }
    }    

    private void moveUp()
    {
        /**
         * if up button is pressed move ship up
         */
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - 3);
        }  
    }          

    private void moveDown()
    {
        /**
         * if down button is pressed move ship down
         */
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + 3);
        }
    }

    private void moveLeft()
    {
        /**
         * if left button is pressed move ship left
         */
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - 3, getY()) ;
        }  
    }          

    private void moveRight()
    {
        /**
         * if right button is pressed move ship right
         */
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + 3, getY() );
        }
    }

    public void fire()
    {
        /**
         *if space button is pressed spawn missile below player, play missile sound, sets fire rate,
         *if v is pressed allow "unlimited" fire rate
         */

        if (Greenfoot.isKeyDown("space") && (canShoot <= 0))
        {
            getWorld().addObject(new Missile(), getX()+ 5, getY()); 
            canShoot = 45;
            Greenfoot.playSound("Missile.wav");
            if (Greenfoot.isKeyDown("v"))
            {
                canShoot = 0;  
            }
        }
    }

    public void destroyed()
    {
        /**
         * if hit by enemy - remove from game, create explosion image, create game over image,
         * play explosion sound, delay game, remove explosion, play game over sound
         */
        if  (getOneIntersectingObject(Enemy.class) != null)
        {                        	 
            getWorld().addObject(new Explosion(), getX(), getY());
            lives.add(-1);           
            getWorld().removeObject(this);           
            Greenfoot.playSound("Explosion2.wav");
              
            if (count >=1)
            {
              setLocation(20 , 200);    
            }
            else if (count <=0)
            getWorld().addObject(new GameOver(), 400, 300);
            {Greenfoot.playSound("Gameover.wav");           
          }
        }
    }

    public Player(Score lifeCount)
    {
        lives = lifeCount;
        lives.add(3);
        
        
    }
}

NikZ NikZ

2014/10/19

#
Maybe the world this Actor is in is null?
Super_Hippo Super_Hippo

2014/10/19

#
Why do you create a new LivesX object every act cycle? In line 120 you remove the player. If you remove it, you can't set the location of it afterwards or get the world.
NikZ NikZ

2014/10/19

#
If you set location or anything like that, it would say "Actor not in World. An attempt ... blah, blah, blah", wouldn't it?
wslade wslade

2014/10/20

#
I tried changing my destroy method to this but I still have the same issue. I know it has something to do with my removing the Player but I want to player to 'disappear' and then either reappear if my count isn't zero or have the game end. How can I change my code to get this to happen?
public void destroyed()
    {
        /**
         * if hit by enemy - remove from game, create explosion image, create game over image,
         * play explosion sound, delay game, remove explosion, play game over sound
         */
        if  (getOneIntersectingObject(Enemy.class) != null)
        {                        	 
            getWorld().addObject(new Explosion(), getX(), getY());
            lives.add(-1);           
            getWorld().removeObject(this);           
            Greenfoot.playSound("Explosion2.wav");
              
            if (count >=1)
            {
                Score lifeCount = new Score();
                getWorld().addObject(new Player(lifeCount), 50, 300);    
            }
            else if (count <=0)
            {
            getWorld().addObject(new GameOver(), 400, 300);
            Greenfoot.playSound("Gameover.wav");           
          }
        }
    }
wslade wslade

2014/10/20

#
Okay I have fixed my nullpointerexception error by adding a resurrect method so the lives counter works but it won't end the game when my count is -3. Any ideas?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Player extends Actor
{ 
    /**
     * creates integer to hold values for missile fire rate
     */
    private int canShoot = 0;
    private int count = 0;
    
    private Score lives;
   
 public Player(Score lifeCount)
    {
        lives = lifeCount;
        
        
        
    }

    public void act() 
    {
        /**
         * move around and fire missiles, destroyed by enemies, recharges missile fire
         */
        moveUp();
        moveDown();
        moveLeft();
        moveRight();
        fire();
        destroyed();
        canShoot--;
        
      
        
        if(count >= 0);
        {
            getWorld().addObject(new Lives3(), 245, 20);                       
        }
        if (count <= -1);
        {
            getWorld().addObject(new Lives2(), 245, 20); 
        }
        if (count <= -2);
        {
            getWorld().addObject(new Lives1(), 245, 20); 
        }
        if (count <= -3);
        {
            getWorld().addObject(new Lives0(), 245, 20); 
        }
   }  

    private void moveUp()
    {
        /**
         * if up button is pressed move ship up
         */
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - 3);
        }  
    }          

    private void moveDown()
    {
        /**
         * if down button is pressed move ship down
         */
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + 3);
        }
    }

    private void moveLeft()
    {
        /**
         * if left button is pressed move ship left
         */
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - 3, getY()) ;
        }  
    }          

    private void moveRight()
    {
        /**
         * if right button is pressed move ship right
         */
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + 3, getY() );
        }
    }

    public void fire()
    {
        /**
         *if space button is pressed spawn missile below player, play missile sound, sets fire rate,
         *if v is pressed allow "unlimited" fire rate
         */

        if (Greenfoot.isKeyDown("space") && (canShoot <= 0))
        {
            getWorld().addObject(new Missile(), getX()+ 5, getY()); 
            canShoot = 45;
            Greenfoot.playSound("Missile.wav");
            if (Greenfoot.isKeyDown("v"))
            {
                canShoot = 0;  
            }
        }
    }
    
private void resurrect()
    {
        /**
         * sets location of resurrection at right side of screen
         */
        
            {
              setLocation(20 , 200);    
            }
                   
          }
        
    public void destroyed()
    {
        /**
         * if hit by enemy - remove from game, create explosion image, create game over image,
         * play explosion sound, delay game, remove explosion, play game over sound
         */
        if  (getOneIntersectingObject(Enemy.class) != null)
        {                        	 
            Actor enemy = getOneIntersectingObject(Enemy.class);
            getWorld().removeObject(enemy);
            getWorld().addObject(new Explosion(), getX(), getY());
            lives.add(-1);        
            Greenfoot.playSound("Explosion2.wav");
            
 if (count >=-2)
            {
              resurrect();  
            }
            if (count <=-3)
            {Greenfoot.playSound("Gameover.wav");           
          }
            
                     
          }
        }
    }

   

Alwin_Gerrits Alwin_Gerrits

2014/10/20

#
I don't see the problem immediatly. Did you consider using the command System.out.println() to see if count actually even reaches -3? And another question why do you use a double if for counter instead of if, else if? I mean you check above or equal to -2, but then check smaller then or equal to -3. Again that seems like a place you should probably be using else if, but i'm left wondering if count changes per 1 then why use <=-3 instead of ==-3? If that's not the problem and you can't find it with System.out.println() then can you at least tell me what you're using the if-statements at the beginning for? Because it seems to me like placing lives is a bit useless, but then again you're already placing player back with the resurrect(). Allso this again reacts with -3. Is that intentional? Just a bit odd to have 4 lives... Anyway, if you want a real end to your game instead of just playing a tune you can use a couple of things. 1) You can use a message popup with the JOptionPane command. (it creates a small window with a titel, some other text and a couple of options which you can use to call other commands) (if you want this I can give an example, it's a bit hard to use) 2) you can use a message popup with the setImage command. (it just makes a small picture, but you won't have options, but you can always just put some command behind this line in your code) (allso see setImage discussion ) 3) you can use a message popup with the messagebox command. (I personally don't know this one even a bit, but see for yourself if you like it: messagebox discussion )
Super_Hippo Super_Hippo

2014/10/20

#
Your code doesn't try to "end" the game somewhere. If you have contact with an enemy, you remove the enemy. If you have a life left, you will move to (20|200). If you're "dead", then you will stay at the same position and play the 'Gameover' sound which you might can't hear because the "Explosion2" sound plays at the same time. To end the game, you have to add it in line 148. So ''getWorld().removeObject(this)' or just Greenfoot.stop'.
Alwin_Gerrits Alwin_Gerrits

2014/10/20

#
I'm more trying to create a screen in between 2 games. Obviously if you lose you want to reset the game right? So deleting something like the world might not be the best option in that case. That's why i'm creating the screen in between 2 games. You simply give a message the player lost or won then either just restart the game or give them an option screen with the option to restart the game. I'm using simular idea in the platform game i'm working on. If you press 'esc' in my game you get to a menu from which you can deside to change the level you're playing. To me that seemed simular to what a 'you're dead' or 'end' screen would do. Right? I mean stopping the program perminently would be an option, but in reality you probably shouldn't do that as beïng a programmer..... in my opinion that is of course :)
Super_Hippo Super_Hippo

2014/10/20

#
I didn't say that he should delete the world. ^^ The main part of my last post was, that the code is maybe executed as it should, but there is nothing that 'ends the game'. And that is what he wanted. Of course you can change the world then or whatever, but all this won't be achieved with only playing a sound. ;)
Alwin_Gerrits Alwin_Gerrits

2014/10/20

#
True that
wslade wslade

2014/10/20

#
Thanks for your ideas. Adding the Greenfoot.stop and changing the if statements in the destroyed code to if else statement did the trick. Thanks again!
Alwin_Gerrits Alwin_Gerrits

2014/10/20

#
Turns out we fixed this one together Hippo. Nice job ^^
Super_Hippo Super_Hippo

2014/10/20

#
Actually the 'else if' didn't solve the problem, but you were right that it is useless to check both ;)
danpost danpost

2014/10/21

#
Really, using 'else if' will check both still. What would be more appropriate is using just 'else', being if the first condition is not true, the second conditional check (which should be removed) will certainly be true.
There are more replies on the next page.
1
2