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

2016/4/14

How to Check Very Specific Collision for Platformer

Geotan Geotan

2016/4/14

#
Pretty much I want to check an entire area for an object. Here is my attempted code:
    public boolean isOnGround()
    {
        for (int x = -this.getImage().getWidth() / 2; x <= this.getImage().getWidth(); x++)
        {
            Actor obstacle = getOneObjectAtOffset(x, this.getImage().getHeight() / 2, Obstacle.class);
            if (obstacle != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return isOnGround();
    }
But it only really ends up checking when x = (positive) this.getImage().getWidth() because it loops until that. Here is the game and full code for testing.
danpost danpost

2016/4/14

#
Fortunately, line 15 will never execute. If it did, you would have an endless looping of execution and your program would "freeze" (remove that line). Now, the reason it never executes is because on the first iteration of the 'for' loop, you have one assignment statement that will execute followed by an 'if' statement that will exit the method returning a 'true' or 'false' value no matter whether the condition was true or not. So, no looping by the 'for' structure will ever take place. The method might as well be written as follows:
public boolean isOnGround()
{
    return getOneObjectAtOffset(-getImage().getWidth()/2, getImage().getHeight()/2, Obstacle.class);
}
which basically says to inform me if any Obstacle object is intersecting the lower-left corner of my image. Maybe you do understand what or how the collision methods work. If you wanted to know if any part of the image of the actor is intersecting any part of an image of an Obstacle type object, just use the 'getOneIntersectingObject' method. There is no need to check each pixel location yourself; the pre-defined method will do it for you.
Geotan Geotan

2016/4/14

#
I have line 15 because otherwise Greenfoot returns an error, missing return statement. I don't want to use getOneIntersectingObject() because then the character actually intersects the image and I don't want that.
Geotan Geotan

2016/4/14

#
I figured out the solution, I think it's because I had return false in the if statement. I rewrote the code a little to this:
    public boolean isOnGround()
    {    
        for(int x = -this.getImage().getWidth() / 2; x < this.getImage().getHeight() / 2; x++)
        {
            Actor ground = getOneObjectAtOffset(x, this.getImage().getHeight() / 2, Obstacle.class);
            if(ground != null)
            {
                setLocation(this.getX(), ground.getY() - ground.getImage().getHeight());
                return true;
            }
        }
        return false;
    }
Now it works perfectly.
danpost danpost

2016/4/15

#
Geotan wrote...
I rewrote the code a little to this: < Code Omitted > Now it works perfectly.
Not quite -- it appears your limiting condition within the 'for' structure is wrong -- should it not be 'x < this.getImage().getWidth()/2'?
Geotan Geotan

2016/4/15

#
Thanks for pointing that out. Luckily in this game the pictures are just squares, but I may make something using this code later.
nathanathan nathanathan

2016/4/15

#
Hi logs
danpost danpost

2016/4/15

#
Geotan wrote...
Thanks for pointing that out. Luckily in this game the pictures are just squares, but I may make something using this code later.
Later, you may end up improving the code even more. To see my way of creating a jump-n-run type movement, check out my Jump and Run Demo w/Moving Platform scenario.
You need to login to post a reply.