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

2015/12/28

Please help: Score increases by 2 (unintentionally)

halien69 halien69

2015/12/28

#
Hi, I'm trying to do the following: 1. If clickwithMouse on ball, remove ball and decrease score by 1 2. If click on World, add a new ball and increase score by 1 I have managed to do these, except ... the score is being increased by 2. For example, I start with one ball in the World (score = 1). I click on World, second ball is added (score = 2), I click on World, third ball added (score = 4), I click on World, fourth ball (score =8) etc Interestingly, to remove the last ball (the one that gave me a score of 8), I need to click on it 4 times! I've been looking at this for hours and no luck, can't figure out where I am going wrong. I appreciate any assistance you may give. I include my codes below. Cheers,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class MyWorld extends World
{
    private Counter counter;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        addNewBall();
        counter = new Counter();
        addObject(counter, 151,6);

    }
    
    public void addNewBall()
    {
        int X = Greenfoot.getRandomNumber(600);
        int Y = Greenfoot.getRandomNumber(300);
        int direction = Greenfoot.getRandomNumber(360);
        addObject(new Ball(direction),X,Y);
    }
    
    public void addScore()
    {
        counter.upScore();
    }
    
    public void subtractScore()
    {
        counter.downScore();
    }

}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int score;
    
    public Counter() //the constructor class
    {
        score = 1; //initalise score to zero
        setImage (new GreenfootImage(300,40));
        update();
    }
    
    public void upScore()
    {
        score++;
        update();
    }
    
    public void downScore()
    {
        score--;
        update();
    }
    
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.WHITE);
        img.drawString("Score: " + score, 5, 30);
    }
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    private int speed = 3;
    /**
     * A ball constructor - create a new ball 
     */
    public Ball(int direction)
    {
        turn(direction);
    }

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.

        move(speed);
        if (atWorldEdge())
        {
            turn(153);
        } 
        changeBalls();
    }   


    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    public void changeBalls()
    {
        World world = getWorld();
        MyWorld myWorld = (MyWorld) getWorld();
        
        if (Greenfoot.mouseClicked(this))
        {
            world.removeObject(this);
            myWorld.subtractScore();
        }        
        //

        if (Greenfoot.mouseClicked(world))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            world.addObject(new Ball(10), mouse.getX(), mouse.getY());
            myWorld.addScore();
        }
    }

}

danpost danpost

2015/12/28

#
I bet if you stop the scenario a drag the latest ball added into the world, you will find more balls underneath it (first 2, then 4, then 8). This is because ALL balls in the world are each, individually, executing the code to add a ball into the world where the mouse was clicked in the world. In other words, lines 62 through 67 should not be located in the Ball class, but similar code should be in your MyWorld class act method (or a method it calls).
halien69 halien69

2015/12/29

#
Thanks! That solved it! I simply copied the text between lines 62 to 67 into the MyWorld class act method, and everytime the mouse clicked the world (and not a ball) I added a new ball. One difficulty I had was to pass the MyWorld class onto the mouse clicks. This was done by setting
private World world;
at the start of the class and then
world = new MyWorld();
if (Greenfoot.mouseClicked(world))
{
do stuff();
}
Cheers,
danpost danpost

2015/12/30

#
I do not see how what you did "solved it". The two lines:
world = new MyWorld();
if (Greenfoot.mouseClicked(world))
is equivalent to
if (Greenfoot.moueClicked(new MyWorld())
It is like saying create a World object and instantly check to see if the mouse has clicked on it. The World object just created is not active and cannot yet be clicked on. It is the current World object that you want to check for a click on. You can refer to the currently active World object by using the 'this' keyword in the MyWorld class:
if (Greenfoot.mouseClicked(this))
You need to login to post a reply.