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

2017/6/9

Collision Checking

MichaelMac MichaelMac

2017/6/9

#
Hi, I have recently been attempting to code a game similar to Dig-Dug, in the game I require the actor to have collision detection with a specific block, depending on the direction that he is moving, is there a way that I am able to convert a list of nearby Objects to an array of blocks so that I can check their X and Y coordinate relative to the character. Apologies if this came out convoluted if you need further clarification just ask. Thanks in advance.
Yehuda Yehuda

2017/6/9

#
Greenfoot has a method that creates a list of all the intersecting instances of the specified class. To cycle through all the intersecting instances of Block.class you would use the following:
for (Block blk : getIntersectingObjects(Block.class)) {
    if (blk.getX() == x && blk.getY() == y) {
        //do something for x and y
        return; //then you can leave the loop since you found a block at x,y
    }
}
With the above code you can do something if there is a block at the specified coordinates.
You need to login to post a reply.