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

2012/9/11

counter help

Stephon231 Stephon231

2012/9/11

#
if you have a counter how to you make an actor enact a method when the counter gets to a sertan # like 70.
danpost danpost

2012/9/12

#
That would depend on what code you are using for the counter, whether you just want to enact the method once or continuously after that number is reached, whether you have only the one counter in the world or possibly others, and possibly other things. Supplying details like these would highly increase the chances of getting proper results quickly.
Stephon231 Stephon231

2012/9/12

#
I only have one counter. Does this help?: private int leavesEaten; public Wombat() { leavesEaten = 0; } leavesEaten = leavesEaten + 1; public int getLeavesEaten() { return leavesEaten; }
danpost danpost

2012/9/12

#
You showed this for the Wombat class:
private int leavesEaten;

public Wombat()
{
    leavesEaten = 0;
}

leavesEaten = leavesEaten + 1;

public int getLeavesEaten()
{
    return leavesEaten;
}
Line 8 seems to be out in no-wheres-land. Do you not have 'canSee' and 'eat' methods in this class?
Stephon231 Stephon231

2012/9/12

#
srry here public void eatLeaf() { Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); if(leaf != null) { // eat the leaf... getWorld().removeObject(leaf); leavesEaten = leavesEaten + 1; } } public int getLeavesEaten() { return leavesEaten; } public boolean foundLeaf() { Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); if(leaf != null) { return true; } else { return false; } }
danpost danpost

2012/9/12

#
OK, your counter is 'leavesEaten'; and you want to call (enact) a method (what method?) when the counter reaches 70.
Stephon231 Stephon231

2012/9/12

#
to call an actor into the world and to stop the game
Stephon231 Stephon231

2012/9/12

#
for a method; public void winner { getWorld().addObject(new Winner(), getWorld().getWidth() -9, getWorld().getHeight() - 5); Greenfoot.stop(); }
danpost danpost

2012/9/12

#
If the 'winner' method is in the Wombat class then immediately after the line 'leavesEaten = leavesEaten + 1;' add the line
if (leavesEaten == 70) winner();
Stephon231 Stephon231

2012/9/12

#
how would i make the counter class that i imported, display the amount of leaves eaten.
danpost danpost

2012/9/12

#
If you only have one Wombat in your scenario, then the Wombat class would be the best place to add, keep a reference to, and work the counter.
// instance field
Counter counter = new Counter("Leaves eaten: ");
// add the following method
public void addedToWorld(World world)
{
    world.addObject(counter, 80, 20);
}
// after 'leavesEaten = leavesEaten + 1;'
// and before 'if (leavesEaten == 70)'
// add this one line
counter.setValue(leavesEaten);
You need to login to post a reply.