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

2018/2/28

Collision and counter

hosfeldli hosfeldli

2018/2/28

#
I really am struggling with a game that adds resources that will be later used for building, collision detection is all messed up what is a better alternative if (isTouching(Survivor.class) && (Greenfoot.isKeyDown("space"))) { woodCounter+= 1; getWorld().showText("Wood:" + woodCounter, 100, 100); }
Vercility Vercility

2018/2/28

#
What is your problem? "Messed up" is not a description of a problem, how are we supposed to help when we dont know whats wrong lol.
hosfeldli hosfeldli

2018/3/1

#
it adds to the counter all the time even is it isn't touching the Survivor
danpost danpost

2018/3/1

#
hosfeldli wrote...
it adds to the counter all the time even is it isn't touching the Survivor
So, I presume you only what it to increase by one for each time the "space" is pressed. You will need a boolean field to track the state of the "space" key:
// add field
private boolean spaceDown;

// in code
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = !spaceDown;
    if (spaceDown && isTouching(Survivor.class))
    {
        woodCounter++;
        getWorld().showText("Wood:" + woodCounter, 100, 100);
    }
}
Check the size of the image assigned to the Survivor object and that of 'this' object.
hosfeldli hosfeldli

2018/3/1

#
thank you very much that works perfect.
You need to login to post a reply.