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

2019/3/25

Trying to create a counter

Vladiboy Vladiboy

2019/3/25

#
Hello, I try to create a counter for my greenfoot "pong" game, but it dosent work.
if (block != null) {
dy = -dy;
getWorld().removeObject(block);
Counter counter = Spielfeld.getCounter();
counter.bumpCount(1);
It says that .getCounter() can not be referenced from a static context, but ive created the methode getCounter in Spielfeld in the right way ??? Can someone help me ?
Super_Hippo Super_Hippo

2019/3/25

#
"Spielfeld" is a class. You are trying to call the method on the class. getCounter is not static. It probably looks like this:
public Counter getCounter()
{
    return counter;
}
"counter" is a field from a "Spielfeld" object. So you can create more than one "Spielfeld" object and each of them would have their own "counter" field. That's why you can't call this method on a class. You need to call it on the correct "Spielfeld" object. So if the "Spielfeld" object is the current world (which I guess), then you can get a reference to the Counter like this:
((Spielfeld) getWorld()).getCounter();
Vladiboy Vladiboy

2019/3/27

#
Thanks
You need to login to post a reply.