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

2015/5/17

Need help with tree collision

randombruh randombruh

2015/5/17

#
Hey guys I'm working on my assignment, Clara. And she goes through trees, I don't know to set up collision so she doesn't go through them. The scene is in a Pacman scenario and don't know what to type please help. This is my code so far: public void avoidTrees() { if(treeFront()) { getDirection(); } }
danpost danpost

2015/5/17

#
You must have more code than that. You should have an 'act' method and at least a 'treeFront' and a 'getDirection' method (and I am sure you have more); but, you only show this 'avoidTrees' one which calls two other methods that are not given. Why? If Clara is going through trees, then somewhere in your code you have either a 'move' call or a 'setLocation' calls that causes her to do so. Without seeing a lot more of your code, it would be impossible to help.
randombruh randombruh

2015/5/17

#
I do have an act method. But this is just another method I don't need the entire act method for now. I'm just focusing on making my avoidTrees method, so Clara doesn't float past them.
danpost danpost

2015/5/17

#
If movement is occurring when you do not wish it to, then you need to qualify (put a condition on, using 'if') the movement. Since there are no statements in your given code that "moves" clara, no help can be afforded.
randombruh randombruh

2015/5/18

#
Ok here is entire code public final String UP = "up"; public final String DOWN = "down"; public final String LEFT = "left"; public final String RIGHT = "right"; //this boolean will check if the key was pressed, set to false public boolean wasKeyPressed = false; public int setAnimationSpeed = 0; public boolean avoidTrees = false; /** * Act method * * Runs of every frame */ public void act() { //if the key was pressed == true then the wasKeyPressed boolean becomes true aswell. if(isKeyPressed() == true) { wasKeyPressed = true; } //if boolean waskeypressed == true clara will move at the speed of 1 //the animationSpeed method will set the speed of the animation if(wasKeyPressed == true) { //Clara will move at this speed animationSpeed(); move(1); } //this function will make the user, use the keyboard keys to move Clara around the map moveClara(); //this method will collect any leaves she is on collectLeaves(); //this method will make Clara collect the mushrooms giving her super powers collectMushrooms(); //this method makes clara move to the other side of the world wrapAroundWorld(); //this method will make Clara stop if she is infront of a tree avoidTrees(); } //this function will make Clara move when arrow keys are pressed on the keyboard public void moveClara() { //if the key pressed is left then Clara will set her direction to left if(Greenfoot.isKeyDown("left")) { setDirection("left"); } //if the key pressed is right then Clara will set her direction to right if(Greenfoot.isKeyDown("right")) { setDirection("right"); } //if the key pressed is up then Clara will set her direction to up if(Greenfoot.isKeyDown("up")) { setDirection("up"); } //if the key pressed is down then Clara will set her direction to down if(Greenfoot.isKeyDown("down")) { setDirection("down"); } } //this method wil make clara collect any leaves she is on public void collectLeaves() { if(onLeaf()) { removeLeaf(); playLeafEatenSound(); } } //this method will make clara collect any mushrooms on the map and giving her superpowers public void collectMushrooms() { if(onMushroom()) { removeMushroom(); } } //this method is used to check if the key is pressed, and will return true if it is else false public boolean isKeyPressed() { if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown(RIGHT) || Greenfoot.isKeyDown(UP)|| Greenfoot.isKeyDown(DOWN)) { return true; } else { return false; } } //this method will set the animationSpeed for Clara public void animationSpeed() { //need to make clara move at certain speed need a number to set speed and make it stay like that setAnimationSpeed++; if(setAnimationSpeed >= 35) { animate(); setAnimationSpeed = 0; } } //this method will make Clara stop if there is a treefront public void avoidTrees() { if(treeFront()) { avoidTrees = true; getDirection(); } } }
danpost danpost

2015/5/18

#
That cannot be the entire code. There are several methods called here whose codes are not given -- 'setLocation(String)', 'removeLeaf', 'removeMushroom', 'playLeafEatenSound', 'wrapAroundWorld' and 'animate' are some (if not all). These methods may be in a superclass, but I could not ascertain that because you also left out the class declaration statement ('public class Clara extends ?????'). Alright, now this is a simplified version of your act method:
public void act()
{
    if(isKeyPressed() == true)
    {
        wasKeyPressed = true;
    }
    if(wasKeyPressed == true)
    {
        animationSpeed();
        move(1);
    }        
    moveClara();
    collectLeaves();
    collectMushrooms();
    wrapAroundWorld();
    avoidTrees();
}
My lines 3 through 6, will set the 'wasKeyPressed' field to 'true' once an arrow key is detected down. Nowhere do I see where the value of this field will be turned back to 'false'. It will stay 'true' indefinitely once set to 'true'. Therefore the two methods called on my lines 9 and 10 will not stop executing after any arrow key is first pressed. Yes, that means Clara will not stop moving regardless of any tree avoiding that you may have. Another thing is your 'avoidTrees' field. Nowhere do I see where its value is being used. I would think that its value would be used to determine if movement was possible or not. Also, it is not ever being set back to 'false' anywhere, either. So, two things, (1) you need to add code in your act method that will upon specific conditions return the value of your boolean fields back to 'false' and (2) use the value of these booleans to regulate the movement of clara.
randombruh randombruh

2015/5/18

#
I sent you the entire code lol. Do you have Steam or something that I can have a chat with u?
danpost danpost

2015/5/18

#
There are several lines at the beginning of the class that you did not include. You could show them.
randombruh randombruh

2015/5/19

#
ok
Mark1984 Mark1984

2015/10/15

#
Hey randombruh, thanks for the help with the code, absolute lifesaver - would i be able to contact you with help for the other parts of the game? regards
You need to login to post a reply.