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

2018/4/16

Non static method cannot be referenced from a static context

IlikeThePrequels IlikeThePrequels

2018/4/16

#
Can someone help me fix this error for my score counter? Here is where I'm getting these issues public void checkCollision() { Actor bee2 = (Actor) getOneIntersectingObject(Bee2.class); if(bee2 != null) { getWorld().removeObject(bee2); getWorld().removeObject(this); Greenfoot.playSound("splat.mp3"); Counter counter = Fields.obtainCounter(); Counter.numberCount(+1); } } The last 2 lines are where my errors are at.
danpost danpost

2018/4/16

#
The last line should begin with counter -- not Counter. The line before it is probably giving you a static content type error message,but I cannot be sure without seeing your obtainCounter method in your Fields class (or does Fields refer to something else).
IlikeThePrequels IlikeThePrequels

2018/4/16

#
Here is my obtainCounter() method public Counter obtainCounter() { return AddUp; }
IlikeThePrequels IlikeThePrequels

2018/4/16

#
danpost wrote...
The last line should begin with counter -- not Counter. The line before it is probably giving you a static content type error message,but I cannot be sure without seeing your obtainCounter method in your Fields class (or does Fields refer to something else).
Here is my obtainCounter() method public Counter obtainCounter() { return AddUp; }
danpost danpost

2018/4/16

#
IlikeThePrequels wrote...
Here is my obtainCounter() method << Code Omitted >>
That counter is belongs to your Fields object -- not to your Fields class. You cannot access it through the class; you can only get it by way of the Fields object create by the class -- the World object your actors are/were in. That means you need to retain that Fields object before removing this from the world. Try this:
public void checkCollision()
{
    Actor bee2 = (Actor) getOneIntersectingObject(Bee2.class);
    if(bee2 != null)
    {
        getWorld().removeObject(bee2);
        Fields fields = (Fields)getWorld();
        getWorld().removeObject(this);
        Greenfoot.playSound("splat.mp3");
        Counter counter = fields.obtainCounter();
        counter.numberCount(+1);
    }
}
This can be shortened to this:
public void checkCollision()
{
    if(isTouching(Bee2.class))
    {
        ((Fields)getWorld()).obtainCounter().numberCount(+1);
        removeTouching(Bee2.class);
        getWorld().removeObject(this);
        Greenfoot.playSound("splat.mp3");
    }
}
You need to login to post a reply.