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

2017/5/2

How to make a command depend on a counter value

wowi132 wowi132

2017/5/2

#
I am trying to make it so that when my actor hits a specific object, it loads a new world depending the current score - a counter. I am trying to do this in the following code, but i realize i cannot compare a counter to an integer (counter <=10). Could anyone possibly help me how the code should look? thanks a lot :)
public void act() 
    {
      Labyrinth LabWorld = (Labyrinth) getWorld();
      Counter counter = LabWorld.getCounter();
      Win1();
    } 
    private boolean WinScreen1()
    {
        if (counter <= 10)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void Win1()
    {
      Actor ditzel;
      ditzel = getOneObjectAtOffset(0,0, Ditzel.class);
        if (ditzel != null)
      {
          if(Winscreen1 = true)
          {
            World world;
            world = getWorld();
            world.removeObject(ditzel);
            Greenfoot.setWorld(new WinScreen1());
        }
      }
    }
}
I am referencing a previously made counter, as well as a new world thats called WInScreen1
danpost danpost

2017/5/2

#
Move lines 3 and 4 to before the 'if' line (line 9) in the WinScreen1 method. Then replace 'counter' in line 9 with whatever it takes to get the value of the counter field from the counter object. Show the class of the counter if you have problems with this. I cannot say about line 24, other than you are using an assignment operator instead of an equality comparison operator. As such, the condition, provided the 'Winscreen1' field is a boolean to begin with, will always be true.
wowi132 wowi132

2017/5/2

#
Thank you so much for your help :) I changed line 24 to <= instead, and i still get the error 'cannot find symbol - variable Winscreen1 ' even though defined the Winscreen1 in lines 7 through 17. As for the 'counter' thing in line 9, i couldn't find anything, but here's the code for the counter class.
public class Counter extends Actor
{
    private int totalCount = 0;

    public Counter()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    } 
}
thanks so much for your help :)
danpost danpost

2017/5/2

#
wowi132 wrote...
I changed line 24 to <= instead, and i still get the error 'cannot find symbol - variable Winscreen1 ' even though defined the Winscreen1 in lines 7 through 17.
What is defined in lines 7 through 17 is a method called WinScreen1. The error message for line 24 says it cannot find a variable, not a method. Method calls ALWAYS have a set of parenthesis after the name of the method. Therefore, line 24 should be:
if (WinScreen1())
// or
if (WinScreen1() == true)
As for the 'counter' thing in line 9, i couldn't find anything, but here's the code for the counter class. < Code Omitted >
You need a way to get the value of 'totalCount' from the Counter object. Add the following method to the Counter class:
public int getCount()
{
    return totalCount;
}
Then, you can replace 'counter' on line 9 with 'counter.getCount()'.
You need to login to post a reply.