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

2017/4/3

it just keeps adding score,

ALPH3A ALPH3A

2017/4/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
     Score score = new Score();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        prepare();
        
    }
    
    public Score getScoreCounter() {
    return score; // this is the field that you created so if you change your classes to capital don't change this
    }
    
    
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Dot dot = new Dot();
        addObject(dot,500,300);
        
        addObject(score,108,58);
        
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class dot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
 
 
public class Dot extends Actor
{
    /**
     * Act - do whatever the dot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    turnrandom();
    if (Greenfoot.mouseClicked(this));
      {
    ((MyWorld)getWorld()).getScoreCounter().addScore();
    }
        
         
    }    
     
     
    public void turnrandom()
    {
        move(5);
        if (Greenfoot.getRandomNumber(100) <2)
        {
            turn(90);
        }
       if (Greenfoot.getRandomNumber(100) <2)
        {
            turn(360);
        }
        if (Greenfoot.getRandomNumber(100) <2)
        {
            turn(270);
        }
        if (Greenfoot.getRandomNumber(100) <2)
        {
            turn(180);
        }
         
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
//import java.awt.Color;
 
/**
 * Write a description of class score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.BLUE, Color.WHITE));
         
    }    
     
    public void addScore()
    {
        score ++ ;
    }
}
i want to make it so that it only adds score when the dot is clicked
ALPH3A ALPH3A

2017/4/3

#
also, If possible could it make it so that if i miss it when clicking it displays game over text.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends Actor
{
    /**
     * Act - do whatever the Gameover wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Gameover () 
    {
        setImage(new GreenfootImage("Game Over", 70, Color.BLACK, Color.WHITE));
        
    }
}
danpost danpost

2017/4/3

#
Add at line 24 in Dot class:
if (Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this))
{
    getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
    Greenfoot.stop();
}
ALPH3A ALPH3A

2017/4/3

#
Hi, thank you for the help with the game over screen. But when i run the game it just adds score without me clicking the button, could you help so that it adds 1 when it is clicked
danpost danpost

2017/4/3

#
ALPH3A wrote...
Hi, thank you for the help with the game over screen. But when i run the game it just adds score without me clicking the button, could you help so that it adds 1 when it is clicked
Remove the semi-colon ( ; ) from the end of line 20 in the Dot class.
You need to login to post a reply.