Hello, i am still new to greenfoot. i want to make my actor(the dot) add to my score class when clicked.
if (Greenfoot.mouseClicked(this))
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);
}
}
}addObject(score,99,58);
public score getScoreCounter() {
return score; // this is the field that you created so if you change your classes to capital don't change this
}If (Greenfoot.mouseClicked(this)); {
((MyWorld)getWorld()).getScoreCounter().addScore();
}