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

2017/5/21

Collision detection

1
2
That0neGuy That0neGuy

2017/5/21

#
Hi there, I am creating a game in which the actor(a panda) is controlled by the user and has to make it across a gap between two cliffs by jumping onto a moving log. On the other side of the cliff there is a bamboo object. I want to try and make it so when the panda hits the bamboo, a text box appears with a question (like a maths question) and the user has to get the answer correct in order to gain points which will be added to the points counter. If the user gets the answer wrong, then the game ends. Please help, thank you.
That0neGuy That0neGuy

2017/5/21

#
I have also shared my scenario
danpost danpost

2017/5/21

#
You can use the 'ask' method of the Greenfoot class to temporarily pause the action to get a response from the user:
if (isTouching(Bamboo.class) && << some condition to indicate the question has not yet been asked >>)
{
    String input = Greenfoot.ask(" << question text >> ");
    if (answer.equals(input))
    {
        score.add(<< amount >>);
        << some action that can be used to indicate question has been asked (used by 'if' condition) >>
    }
    else
    {
        << end game >>
    }
}
That0neGuy That0neGuy

2017/5/22

#
Thank you for your quick response. I am really not amazing with greenfoot and hardly even know the basics to it and also my teacher at college is not very useful either and i am really not sure what to do and how to do it.
Yehuda Yehuda

2017/5/22

#
Everything was pretty simple, all the places with <<>> are meant for you to decide what goes there (depending on how your game works). If the Panda is touching the bamboo (and first time "<< some condition to...") then ask a question. If the answer is correct then add points, otherwise do something else ("<< end game >>").
That0neGuy That0neGuy

2017/5/22

#
And this goes into a separate class or the panda class? Or bamboo class?
That0neGuy That0neGuy

2017/5/22

#
I'm using green foot version 3.0+ and its quite confusing to add the coding you want. There are more shortcuts but again I am a beginner at it.
That0neGuy That0neGuy

2017/5/22

#
And also if I want to repeat the code when the user gets the answer correct but this time when the user hits the bamboo they get a different question instead of the game ending after answering the first question?
danpost danpost

2017/5/22

#
You would use code similar to what I gave in the class of the panda. The 'answer' variable would be of String type and contain the answer in string form. You can put your question text lines and their answers in a double String array field and use an int field to track the question number. Increment and negate the int field when a question is asked. Then negate it again when the panda no longer intersects the bamboo. Only ask a question when the value of the int field is not negative (your second condition in the first line).
That0neGuy That0neGuy

2017/5/22

#
It give me an error saying "cannot find symbol- method ask(java.pang.String)"
danpost danpost

2017/5/22

#
That0neGuy wrote...
It give me an error saying "cannot find symbol- method ask(java.pang.String)"
Show the code you are using. Cannot help without it.
That0neGuy That0neGuy

2017/5/22

#
 /**
     * 
     */
    private void collision()
    {
        if (isTouching(bamboo.class)) {
            String input = Greenfoot.ask(" << question text >> ");
            if (input.equals(3)) {
                score.add(1);
            }
            else
            {
                greenfoot.setworld(new MyWorld());
            
        }
    }
danpost danpost

2017/5/22

#
Looks like you are using it correctly. There is an 'ask' method in the Greenfoot class (here). Line 8 is comparing an int value to a String value (use "3" -- in the quotes) and 'setworld' on line 13 is incorrectly named (should be 'setWorld').
That0neGuy That0neGuy

2017/5/22

#
Still does not work :/
That0neGuy That0neGuy

2017/5/22

#
Trying to use JOption method but still does not work. The character just jumps on top of the bamboo or just goes through it. I am just so frustrated now
There are more replies on the next page.
1
2