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

2015/12/24

Score won't increase

RobotDJS RobotDJS

2015/12/24

#
I tried to make it so if there was a collision between Projectile and Target and the Image (for Target) was a certain image then the score would increase by 1 and it was change to a different image. Everything works except the score, which doesn't increase. Here is the code for the World
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ProjectileWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProjectileWorld extends World
{
    private long startMillis = 0;
    private int numFrames = 0;
    private final int groundHeight;
    
    public static final double MAX_FORCE = 8.5;
    public static final double MIN_FORCE = 3.5;
    public static final double GRAVITY = 0.1;
    public static final int LOWEST_ANGLE = 30;
    
    private Player[] players;
    private int curPlayer;
    private int turnCountdown;
    private Scoreboard sb;

    /**
     * Constructor for objects of class ProjectileWorld.
     * 
     */
    public ProjectileWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1, false); 
        
        groundHeight = 550;
        getBackground().setColor(new java.awt.Color(0.8f, 0.95f, 1.0f, 1.0f));
        getBackground().fill();
        getBackground().setColor(new java.awt.Color(0.4f, 0.7f, 0.4f, 1.0f));
        getBackground().fillRect(0, groundHeight, getWidth(), getHeight());
        sb = new Scoreboard();
        addObject(sb,70,30);
        addObject(new Target(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight() - 250) + 200);
        players = new Player[] {new HumanPlayer()};
        
        for (Player p : players)
        {
            addObject(p, Greenfoot.getRandomNumber(getWidth() - 100), groundHeight);
        }
        curPlayer = 0;
        turnCountdown = 10;
    }
    
    public void updateScore()
    {
        sb.changeScore();
    }
    
    public void started()
    {
        numFrames = 0;
        startMillis = System.currentTimeMillis();
    }
    
    private double t;
    
    public void act()
    {
        numFrames += 1;
        t = getTimePerFrame();
        
        if (turnCountdown > 0)
        {
            turnCountdown -= 1;
            if (turnCountdown == 0)
            {
                players[curPlayer].startTurn();
            }
        }
    }
    
    public double getTimePerFrame()
    {
        return (double)(System.currentTimeMillis() - startMillis) / (1000.0 * (double)(numFrames));
    }
    
    public boolean hitsGround(double startX, double startY, double endX, double endY)
    {
        return endY > groundHeight;
    }
    
    public boolean hitsTarget(double startX, double startY, double endX, double endY)
    {
        return endY > groundHeight;
    }
    
    public void landed()
    {
        curPlayer = (curPlayer + 1) % players.length;
        turnCountdown = 10;
    }
}
Here is the code for the Target
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class target here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Target extends Actor
{
    private GreenfootImage target = new GreenfootImage("teddybear.png");
    private GreenfootImage dead = new GreenfootImage("skull.png");
    /**
     * Act - do whatever the target wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkCollision();
    }
    
    public void checkCollision()
    {
        Actor collided;
        collided = getOneIntersectingObject(Projectile.class);
        if (collided != null)
        {
            if (getImage() == target)
            {
               ProjectileWorld world = (ProjectileWorld)getWorld();
               world.updateScore();
            }
               setImage(dead);
        }
    }
}
Here is the code for the Scoreboard
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Scoreboard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scoreboard extends Actor
{
    private int score = 0;
    
    public Scoreboard()
    {
        GreenfootImage img = new GreenfootImage(100,30);
        img.drawString("Score: " + score, 5, 25);
        setImage(img);
    }
    
    public void changeScore()
    {
        score = score + 1;
        GreenfootImage img = getImage();
        img.clear();
        img.drawString("Score: " + score, 5, 25);
    }
}
If someone could help that'd be great. Thanks!
danpost danpost

2015/12/24

#
Your Target object image is never really set to the image stored assigned to 'target' at line 11 of the Target class. You can either add a constructor to set the target image or just change line 28 to check if it is not set to the 'dead' image.
RobotDJS RobotDJS

2015/12/26

#
How do check to see if it is set to 'dead' image?
RobotDJS RobotDJS

2015/12/26

#
Would this fix the glitch?
danpost danpost

2015/12/27

#
RobotDJS wrote...
How do check to see if it is set to 'dead' image?
The same way you check to see if it is the 'target' image at line 28 of your Target class. If you want to check if it is NOT the 'dead' image, use !=.
RobotDJS wrote...
Would this fix the glitch?
Yes. I believe so.
RobotDJS RobotDJS

2015/12/27

#
Thanks. It worked.
You need to login to post a reply.