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

2016/4/21

help with score counter

AdamSteele AdamSteele

2016/4/21

#
I need help with my score counter. I trieed some code and it deosnt seem to be working heres the code in my actor
Actor PIZZA = getOneIntersectingObject(PIZZA.class);
if(PIZZA!=null)
{
    World myWorld = getWorld();
    myWorld.removeObject(PIZZA);
    myWorld space = (myWorld)myWorld;
    Counter counter = myWorld.getCounter();
    counter.addScore();
    myWorld.removeObject(this);
}
And heres the code in myWorld
public class myWorld extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class myWorld.
     * 
     */
    public myWorld()
    {    
        super(640, 480, 1); 
        addObject(new MainCharacter(), 10, 390);
        addObject(new platformc(), 10, 410);
        addObject(new platformc(), 45, 410);
        addObject(new platformc(), 150, 380);
        addObject(new platformc(), 251, 180);
        addObject(new platformc(), 280, 180);
        addObject(new platformc(), 300, 210);
        addObject(new platformc(), 320, 250);
        addObject(new platformc(), 340, 290);
        addObject(new platformc(), 360, 330);
        addObject(new platformc(), 400, 330);
        addObject(new platformc(), 440, 330);
        addObject(new platformc(), 573, 330);
        addObject(new platformc(), 630, 330);
        addObject(new redTube(), 505, 350);
        addObject(counter, 45, 30);    
        addObject(new ladder(), 191, 275);
    }
    public Counter getCounter()
    {
        return counter;
        
    }
}
and heres the code in my Counter class
import greenfoot.*;
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
{
    int score = 0;
    /**
     * Act - do whatever the Counter 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.GREEN, Color.BLACK));
        
    }    
    
    public void addScore()
    {
        score++;
    }
}
it throws the error in my actor class after the myWorld.getCounter saying cannot find symbol. any help is appreciated please. Thanks
danpost danpost

2016/4/21

#
Line 6 declares a local variable of type 'myworld' called 'space'. Yet, when trying to get the counter on line 7, you use 'myWorld' which is declared as a 'World' object on line 4. Since the counter is in the 'myWorld' class, you cannot use a variable of type World to access it.
You need to login to post a reply.