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

2013/1/13

i want to add a new counter

1
2
ctgreenfoot ctgreenfoot

2013/1/13

#
however when i add it in the world prepare method, it comes up with the error message: cannot find symbol - constructor SimpleCounter(). here is my code:
import greenfoot.*;
import java.awt.Color;

/**
 * Counter that displays a number with a text prefix.
 * See the ScoreBoard class. It is very similar to this, but the image includes a box with a border.
 *
 * @author Michael Kolling 
 * @version 2.0
 */
public class SimpleCounter extends Actor
{
    private int    counterValue;
    private String counterText;

    public SimpleCounter(String text)
    {
        counterText  = text;
        counterValue = 5;

        int counterWidth  = (counterText.length() + 2) * 10;
        int counterHeight = 16;

        GreenfootImage image = new GreenfootImage(counterWidth, counterHeight);
        setImage(image);
        updateImage();
    }

    public void act()
    {
    }

    public void add(int amount)
    {
        counterValue = counterValue + amount;
        updateImage();
    }

    public void subtract(int amount)
    {
        counterValue = counterValue - amount;
        updateImage();
    }

    public int getValue()
    {
        return counterValue;
    }

    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setColor(Color.BLACK);
        image.drawString(counterText + counterValue, 1, 12);
    }
}
vonmeth vonmeth

2013/1/13

#
This is your constructor:
public SimpleCounter(String text)  
    {  
        counterText  = text;  
        counterValue = 5;  
  
        int counterWidth  = (counterText.length() + 2) * 10;  
        int counterHeight = 16;  
  
        GreenfootImage image = new GreenfootImage(counterWidth, counterHeight);  
        setImage(image);  
        updateImage();  
    }  
public SimpleCounter(String text) means when you create a new one, you need to pass a string to it. Like "new SimpleCounter("Stuff: ");"
ctgreenfoot ctgreenfoot

2013/1/13

#
im confused. what is a string? and what string would i use in this instance?
vonmeth vonmeth

2013/1/13

#
A string in an object. It is basically an array of chars (characters). Basically it is text.
char[] textArray = { 'h', 'e', 'l', 'l', 'o' };
String helloText = new String(textArray);
SimpleCounter counter = new SimpleCounter(helloText);

// or simply (same ending result)
String text = "hello";
SimpleCounter counter = new SimpleCounter(text);

// or simply (same ending result)
SimpleCounter counter = new SimpleCounter("hello");
Supply whatever string (text) you want to be displayed in your counter. Say your counter is going to display the number of bullets your rocket has left. You could supply it the string "Bullets: ". Say you have 15 bullets left. It would display, "Bullets: 15"
image.drawString(counterText + counterValue, 1, 12);
That code takes the string you supplied and puts it together with the counterValue, and displays them together (concatenates them).
ctgreenfoot ctgreenfoot

2013/1/13

#
it works! thanks. however I still have a problem. when i click compile, the world prepare method shows up and highlights newLobster() saying: cannot find symbol - constructor Lobster(). here are my codes:
 * Write a description of class CrabWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CrabWorld extends World
{

    private GreenfootSound backgroundMusic = new GreenfootSound("background.mp3");

    public void started()
    {
        backgroundMusic.playLoop();
    }

    public void stopped()
    {
        backgroundMusic.stop();
    }

    /**
     * Constructor for objects of class CrabWorld.
     * 
     */
    public CrabWorld()
    {    
        super(1000, 800, 1); 

        prepareWorld();


    }

    private void prepareWorld()
    {
        Counter counter = new Counter();
        addObject(counter, 100, 70);

        Crab crab = new Crab(counter);
        addObject(crab, 500, 400);

        CountdownClock countdownClock = new CountdownClock();
        addObject(countdownClock, 100, 40);
        
        SimpleCounter simpleCounter = new SimpleCounter("Lives:");
        addObject(simpleCounter, 120, 100);
        
        if (counter.getValue() == 20)  
        {  
            RedWorm redWorm = new RedWorm();  
            addObject(redWorm, (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));  
        }  

        for (int item=0; item<Greenfoot.getRandomNumber(11)+10; item++)
            addObject (new Worm(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));

        for (int item=0; item<2; item++)
            addObject (new Lobster(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));

    }
}
import greenfoot.*;

public class Lobster extends Animals
{
    private SimpleCounter simpleCounter;

    public Lobster(SimpleCounter lifeCounter)
    {
        simpleCounter = lifeCounter;
    }

    public void act() 
    {
        moveAround();
        eatCrab();

    }  

    private void moveAround()
    {
        move(5);
        if (Greenfoot.getRandomNumber (100) < 10)
        {
            turn(Greenfoot.getRandomNumber (90)-45);
        }
        if (atWorldEdge())
        {
            turn(180);
        }
    }

    public boolean atWorldEdge()
    {
        if (getX() <= 5 || 
        getX() >= getWorld() . getWidth() -5 ||
        getY() <= 5 ||
        getY() >= getWorld() . getHeight() -5)
            return true;
        return false;
    }

    private void eatCrab()
    {
        Actor crab;
        crab = (Crab)getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());
            crab.setLocation(x, y);
            getImage().scale(getImage().getWidth()*110/100, getImage().getHeight()*110/100);    
            simpleCounter.subtract(1);
            Greenfoot.playSound ("eating.wav");


        }
    }
}
vonmeth vonmeth

2013/1/13

#
Your constructor:
public Lobster(SimpleCounter lifeCounter)  
    {  
        simpleCounter = lifeCounter;  
    }  
Where you create your lobster:
SimpleCounter simpleCounter = new SimpleCounter("Lives:");  
        addObject(simpleCounter, 120, 100);

for (int item=0; item<2; item++)  
            addObject (new Lobster(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight()))); 
We are getting the same exact error as earlier. We were missing parameters last time, and we are missing the same thing this time.
ctgreenfoot ctgreenfoot

2013/1/13

#
i thought that might be it but I cannot think which/where the parameters are missing. is it in the new Lobster()?
ctgreenfoot ctgreenfoot

2013/1/13

#
i put new Lobster(SimpleCounter) and it worked! is that what i was meant to do?
vonmeth vonmeth

2013/1/13

#
Aye! I was hoping if I nudged you towards the answer you would figure it out. Good job! =)
ctgreenfoot ctgreenfoot

2013/1/13

#
however i know encountered another problem. i want the scenario to stop when the crab runs out of lives, however when i wrote: (simpleCounter == 0); { Greenfoot.stop(); } it says incomparable types: SimpleCounter and int
ctgreenfoot ctgreenfoot

2013/1/13

#
oh theres also an if in front of the first line
vonmeth vonmeth

2013/1/13

#
simpleCounter is the object, not the value. You need to call the method that gets the value. That method is getValue(). You would call it the same way you called subtract. So like, simpleCounter.getValue() == 0;
ctgreenfoot ctgreenfoot

2013/1/13

#
now it says cannot find symbol - variable get value, however i checked and it does exist in the simple counter code
vonmeth vonmeth

2013/1/13

#
Where are you calling this? Post the entire class, if you could.
ctgreenfoot ctgreenfoot

2013/1/13

#
import greenfoot.*;

public class Lobster extends Animals
{
    private SimpleCounter simpleCounter;

    public Lobster(SimpleCounter lifeCounter)
    {
        simpleCounter = lifeCounter;
    }

    public void act() 
    {
        moveAround();
        eatCrab();

    }  

    private void moveAround()
    {
        move(5);
        if (Greenfoot.getRandomNumber (100) < 10)
        {
            turn(Greenfoot.getRandomNumber (90)-45);
        }
        if (atWorldEdge())
        {
            turn(180);
        }
    }

    public boolean atWorldEdge()
    {
        if (getX() <= 5 || 
        getX() >= getWorld() . getWidth() -5 ||
        getY() <= 5 ||
        getY() >= getWorld() . getHeight() -5)
            return true;
        return false;
    }

    private void eatCrab()
    {
        Actor crab;
        crab = (Crab)getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());
            crab.setLocation(x, y);
            getImage().scale(getImage().getWidth()*110/100, getImage().getHeight()*110/100);    
            simpleCounter.subtract(1);
            Greenfoot.playSound ("eating.wav");

        }
        
        if (simpleCounter.getValue == 0);
        {
            Greenfoot.stop();
        }
    }
}
There are more replies on the next page.
1
2