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

2017/11/5

isTouching help

ArchMageGaming ArchMageGaming

2017/11/5

#
Hi, I am trying to create a simple system that imitates gravity where the Guy actor is always going down unless touching the ground actor, which then he stops going down, kind of like gravity. The going down part works, what doesn't work is that he does not stop going down after touching the ground.
public class Guy extends Characters
{
    boolean inAir = true;
    int airTime = 0;
    public void act() 
    {
         if(isTouching(Ground.class)){
            inAir = false;
        }
        if(inAir = true){
            setLocation(getX(), getY()+3);      
        }
    }    
}
disregard airtime, it will be used in a separate thing. Everything compiles correctly, and I don't fully understand why this doesn't work. I've tried creating another if statement saying that when inAir is false, to stop all movement, but that doesn't work either.
danpost danpost

2017/11/5

#
One major problem is on line 10 where you actually set 'inAir' to true and use 'true' as the boolean condition instead of just asking if 'inAir' is true. Use either:
if (inAir == true) // using '==' for equality comparison
// or
if (inAir) // using the value itself
After that change, there will be no place where 'inAir' will go from a 'false' value to a 'true' one. Maybe lines 7 through 9 can be replaced with:
inAir = ! isTouching(Ground.class);
ArchMageGaming ArchMageGaming

2017/11/5

#
Oh yeah! Sorry, I forgot basic boolean logic. I always do that :P Thanks!
You need to login to post a reply.