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

2014/7/3

HELP!!! Scoreboard not working!!

Ichliebejenni Ichliebejenni

2014/7/3

#
my scoreboard is meant to count down lives, and count down the sore, and yet only the score is working. I'm fairly new to this and do not know what has gone wrong. Help!
Ichliebejenni Ichliebejenni

2014/7/3

#
I can only show you my code on Monday if you want to help, because that's when I have access to the code.
NikZ NikZ

2014/7/3

#
What exactly do you want your scoreboard to do? Is it a class, or... If it is an int, you can do scoreboard--; (decreases scoreboard by 1)
Ichliebejenni Ichliebejenni

2014/7/4

#
its a class
Super_Hippo Super_Hippo

2014/7/4

#
Did you import the Scoreboard class? With this, you can show a highscore table. You should not count lives there. You can import the Counter class for that if you want it to be visible or just use an int in the class.
NikZ NikZ

2014/7/4

#
If you want to have a high score table consisting of Greenfoot users, check out the API for UserInfo.
Ichliebejenni Ichliebejenni

2014/7/7

#
My game consists of a rocket launcher shooting rockets at crates to increase the score. The plane is dropping the crates. If a crate hits the bottom of the screen, then a live is lost. A scoreboard is meant to show the lives and the score, but the score isnt working. Here is my code: The Rocket:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
 public Rocket ()
 {
     setRotation (270);
 }
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
 public void act() 
 {
  move (10);
        
  Actor actor = getOneIntersectingObject (Crate.class);
  if (actor != null)
  {
      World airWorld = getWorld();
      airWorld.removeObject (actor);
      airWorld.removeObject (this);
      Greenfoot.playSound("Explosion.mp3");
  }
  else 
  {
            actor = getOneIntersectingObject (Plane.class);
    if (actor != null)
    {
        World airWorld = getWorld();
        airWorld.removeObject (actor);
        airWorld.removeObject (this);
        Greenfoot.playSound("Explosion.mp3");
    }
    else
    {
       actor = getOneIntersectingObject (Plane.class);
       if (actor != null)
       {
            AirRaidWorld airWorld = (AirRaidWorld)getWorld();
            airWorld.removeObject (actor);
            airWorld.removeObject (this);
            if (airWorld.getNumberOfPlanes() == 0)
            {
                airWorld.addPlane();
            }
                    
            airWorld.increaseScore ();
            Greenfoot.playSound("Explosion.mp3");
       }
       else
       {
            int yCoord = getY();
            if (yCoord <= 0)
            {
                World airWorld = getWorld();
                airWorld.removeObject (this);
            }    
       }
    }
  }
 }
}
The Plane: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Plane here. * * @author (your name) * @version (a version number or a date) */ public class Plane extends Actor { /** * Act - do whatever the Plane wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move (2); int xCoord = getX(); if (xCoord <= 0 || xCoord >= 599) { turn (180); } int rand = Greenfoot.getRandomNumber(100); if (rand == 1) { World airWorld = getWorld(); airWorld.addObject (new Crate (), getX(), getY()); } } } The Crate: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Rocket here. * * @author (your name) * @version (a version number or a date) */ public class Crate extends Actor { public Crate () { setRotation (90); } /** * Act - do whatever the Rocket wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move (1); int yCoord = getY(); if (yCoord >= 399) { AirRaidWorld airWorld = (AirRaidWorld)getWorld(); airWorld.removeObject (this); airWorld.loseLife(); } } } The Laucher: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Launcher here. * * @author (your name) * @version (a version number or a date) */ public class Launcher extends Actor { private int delay = 0; /** * Act - do whatever the Launcher wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("right")) { move (5); } if (Greenfoot.isKeyDown("left")) { move(-5); } if (delay > 0) { delay = delay - 1; } else { if (Greenfoot.isKeyDown("space")) { World airWorld = getWorld(); airWorld.addObject(new Rocket(), getX(), getY()); Greenfoot.playSound("RocketTakeoff.mp3"); delay = 25; } } } } AirRaidWorld: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class AirRaidWorld here. * * @author (your name) * @version (a version number or a date) */ public class AirRaidWorld extends World { private int score = 0; private int lives = 5; private StatusBoard scoreBoard; /** * Constructor for objects of class AirRaidWorld. * */ public AirRaidWorld() { super(600, 400, 1); addObject (new Launcher(), 300, 375); addObject (new Plane (), 100, 30); scoreBoard = new StatusBoard (); addObject (scoreBoard, 30, 385); scoreBoard.updateStatus (score, lives); } public void increaseScore () { score = score + 1; if (score % 5 == 0) { addPlane(); } scoreBoard.updateStatus (score, lives); } public int getScore () { return score; } public void loseLife() { lives=lives-1; scoreBoard.updateStatus (score, lives); if (lives == 0) { Greenfoot.stop(); } } public int getLives() { return lives; } public void addPlane () { if (getNumberOfPlanes() < 5) { addObject (new Plane (), 100, 30); } } public int getNumberOfPlanes () { List planeList = getObjects(Plane.class); return planeList.size(); } } StatusBoard (score board): import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class StatusBoard here. * * @author (your name) * @version (a version number or a date) */ public class StatusBoard extends Actor { private static final String scoreLabel = "Score: "; private static final String livesLabel = "Lives: "; private GreenfootImage box; public StatusBoard () { box = new GreenfootImage (60, 30); box.setColor(new Color (0f,0f,0f,0.5f)); box.fillRect (0,0, 100, 30); updateStatus (0, 0); } public void updateStatus (int score, int lives) { GreenfootImage img = new GreenfootImage (box); img.setColor (Color.WHITE); img.drawString(scoreLabel + score, 5, 12); img.drawString(livesLabel + lives, 5, 25); setImage (img); } } Hope this helps :)
danpost danpost

2014/7/7

#
The block of code in the Rocket class from lines 45 through 56 will never execute. The 'else' block starting at line 42 will only execute when 'actor'', whose value is acquired at line 33, IS 'null' (see 'if' condition on line 34 -- the 'if' for the 'else' in question). If 'actor' is 'null' then, you will certainly acquire a 'null' value for 'actor' at line 43, which has the same assignment as line 33.
Ichliebejenni Ichliebejenni

2014/7/7

#
thanks ill try and work it out :)
You need to login to post a reply.