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

2014/3/2

World question

casper4444 casper4444

2014/3/2

#
Hello, I want my counter to start at different numbers in my worlds. How do I call the world? I now have
        if (getWorld.Level1)) {
            score = 30;
        }
        if (getWorld(Level2)) {
            score = 31;
        }   
        if (getWorld(Level3)) {
            score = 32;
        }
this but that is a complete guess.
danpost danpost

2014/3/2

#
First off, there is not method 'getWorld(World)', unless you wrote one yourself (but you did not post it, if you had). 'getWorld' is an Actor class method that uses no parameters. It only returns the world object that the said actor resides (as a World object -- that is, not as a Level1, Level2, or Level3 object). However, you can determine which subWorld class it is by using the 'instanceof' keyword:
if (getWorld() instanceof Level1) score = 30;
if (getWorld() instanceof Level2) score = 31;
if (getWorld() instanceof Level3) score = 32;
There might be an easier way to set the score -- do your Level1, Level2 and Level3 subclasses have an intermediate superworld before World, or do they extend World directly?
casper4444 casper4444

2014/3/2

#
They extend directly from the World. I now inserted the code and it had no errors, but the score in the game says 0, no matter what I write in the code. The code says score = 30 but it won't work
danpost danpost

2014/3/2

#
casper4444 wrote...
They extend directly from the World. I now inserted the code and it had no errors, but the score in the game says 0, no matter what I write in the code. The code says score = 30 but it won't work
Then 'score' is not referring to the field you are trying to set. Would need to see more code to help.
danpost danpost

2014/3/2

#
You probably are using mik's Counter class have the following line (or something very similar) in all three level World subclasses (in the constructor or prepare method):
Counter counter = new Counter();
If you have this line in each World subclass, then follow it immediately with this:
counter.setValue(30);
Adjust the value for each world subclass. Then, your actor does not have to deal with setting its value.
casper4444 casper4444

2014/3/2

#
I do have it from mik, but when I insert the line like what you said, it ''cannot find the symbol - method setValue(int)'' This is my counter class code now:
public class Counter extends Actor
{
    private int score;
    public Counter() 
    {
        if (getWorld() instanceof Level1) {
            score = 30;
        }
        if (getWorld() instanceof Level2) {
            score = 31;
        }   
        if (getWorld() instanceof Level3) {
            score = 32;
        }
        setImage (new GreenfootImage(200,30));
        update();
    }    

    public void addScore()
    {
        score--;
        update();
    }

    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.WHITE);
        img.setFont(img.getFont().deriveFont(18f));
        img.drawString("Enemies left: " + score, 4, 20);
    }

    /**
     * Return the current counter value.
     */
    public int getScore()
    {
        return score;
    }
}
and this is the code in the Level1 (that piece):
private void prepare()
    {
        counter = new Counter();
        counter.setValue(30);
        addObject(counter, 754, 389);
because I don't know if I did it right
danpost danpost

2014/3/2

#
Remove lines 6 through 14 of the Counter class.
danpost danpost

2014/3/2

#
And add the 'setValue' method to the Counter class:
public void setValue(int amount)
{
    score = amount;
    update();
}
You could change the name of the method to 'setScore', if you wish (since that is what it is setting). If you do, do not forget to change the calling statements to reflect the change.
You need to login to post a reply.