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

2017/11/16

landing on a platform

rutger362 rutger362

2017/11/16

#
I want to let enemy check if there's a platform above it and when it's above it, then let him jump. But I want it to only jump when it sees the platform. Here is my code that doesn't work, I hope someone can help me out:
public boolean checkAbove()
    {
        Actor ground = getObjectsInRange(10);
        if (ground != null)
        {
            System.out.println("true" );
            return true;
        }
        System.out.println(ground);
       return false;
    }

    public void jump()
    {
        if (checkAbove())
        {
            vSpeed = vSpeed - jumpStrength;
            jumping = true;
            fall();
        }
    }
rutger362 rutger362

2017/11/16

#
This was my own solution just now but that doesn't work quite well either:
public boolean checkAbove()
    {
        List ground = getObjectsInRange(45, ground.class);
        if (ground.size()>0)
        {
            System.out.println("true" );
            return true;
        }
        System.out.println(ground);
       return false;
    }

    public void jump()
    {
        if (checkAbove())
        {
            vSpeed = vSpeed - jumpStrength;
            jumping = true;
            fall();
        }
    }
danpost danpost

2017/11/16

#
The only difference between the two codes other than what is printed to the terminal is the range ('10' and '45'). Checking against 'null' whether a single ground object is in range is equivalent to checking if more than zero is in range (other than the fact that you get a reference to one that is in range, if one is, with the first code set). Nowhere is there any checking of the position of a near platform with respect to the enemy, which is something you will need to do.
You need to login to post a reply.