I am making a maze game and need to ensure the maze walls can be collided with. I have two ideas for how to do this:
Make create individual pieces of maze wall, arrange them in the shape of the maze I want, and then program collision detection for each one.
Make one sprite for the walls of the entire maze and use pixel perfect collision to allow the play to go between the walls and still be hit by them.
As I find the process of setting the x and y coordinates of static sprites in games tedious, I really hope to not have to use the first solution. But I've never actually programmed pixel perfect collision in any language before and I'm struggling to find a website that covers it for Phaser 3 or understand in general. Can someone provide an example?
The best solution would be to use a
tilemap, with atileset(s) and using the (opensource) tool Tiled. (you could even use collision shape, instead of the usual boxes here an example)By the way, this method, is the one, that I would recommend.
But to keep things easy, and without extra tools, an option is to use phaser's tilemap with an array ( here are some other examples ), here you just have to define a grid/map (and atleast one layer) and than you can use it for collision. The only thing is if you want to use a single big image with the whole maze, you would have to match the colliding tiles with the maze on the image.
This solution is not pixel perfect, since the collisions are in tiles, but you could make the tile smaller, if you need more details. (Although you could improve the collision accuracy, in the
collideoroverlapfunction link to documentation )Here some demo code: