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

2017/4/3

cannot find variable 'score'

ALPH3A ALPH3A

2017/4/3

#
hi i need some help regarding the adding score when clicked.
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();
        
    }
    
    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);
        Score score = new Score();
        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 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();
    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);
        }
         
    }
}
[Disallowed URL]
ALPH3A ALPH3A

2017/4/3

#
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++;
    }
}
Nosson1459 Nosson1459

2017/4/3

#
Line 38 in MyWorld has to be outside all methods (line 11).
You need to login to post a reply.