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

2017/3/30

add score when clicked

ALPH3A ALPH3A

2017/3/30

#
Hello, i am still new to greenfoot. i want to make my actor(the dot) add to my score class when clicked.
danpost danpost

2017/3/30

#
Use:
if (Greenfoot.mouseClicked(this))
in the act method of the class of the actor for the condition to execute its code-block, which should add to the score counter. For further assistance, please post your current codes for counter and dot.
ALPH3A ALPH3A

2017/3/30

#
thanks!
ALPH3A ALPH3A

2017/3/30

#
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
{

    /**
     * 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();
    }

    /**
     * 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);
        score score = new score();
        addObject(score,108,58);
        score.setLocation(99,58);
    }
}
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++;
    }
}
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 prize
{
    /**
     * 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();
        
        
    }    
    
    
    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);
        }
        
    }
}
Nosson1459 Nosson1459

2017/3/30

#
These are the things you should do:
  • You can change lines 32 - 33 in MyWorld to
    addObject(score,99,58);
  • Move line 31 to be line 11.
  • Add this method into the MyWorld class:
    public score getScoreCounter() {
        return score; // this is the field that you created so if you change your classes to capital don't change this
    }
  • In dot's act method you can do:
    If (Greenfoot.mouseClicked(this)); {
        ((MyWorld)getWorld()).getScoreCounter().addScore();
    }
  • Change all your class names to start with a capital letter which is the proper way to do it in Java.
You need to login to post a reply.