Jump to content

Check for collision between sprite and TilemapLayer


Recommended Posts

Hello everyone,

I'm making a basic tower defence game, and am trying to prevent towers being placed on the pathway. In this case, the pathway is everywhere which is not in this.layers.collision.

The following code creates a sprite which is rectangular/square in shape. The size of the sprite seems to be coming from somewhere else in my code, can't pinpoint where, but I've made it smaller so it definitely does not overlap other tiles (used the debugger to visualise this). The x and y co-ordinates are the position of the mouse/input, translated to the top-left corner of it's tile. The tiles are 35 pixels square, so a mouse position of [358, 362] would become [350, 350].

        this.pathwayPlacementRectangle = game.add.sprite(x + 8, y + 8, null);
        game.physics.enable(this.pathwayPlacementRectangle, Phaser.Physics.ARCADE);
        this.pathwayPlacementRectangle.scale.setTo(0.5, 0.5);
        var onPathway = true;

        if (game.physics.arcade.collide(this.pathwayPlacementRectangle, this.layers.collision)) {
            onPathway = false;

I've offset the rectangle a bit and made it smaller in case it was clashing with the edges of the collision layer.

The problem is onPathway is always false, even when this.pathwayPlacementRectangle is not located over the collision layer.

If I replace this.layers.collision with another group in the game, such as some moving sprites, the code works as expected. Similarly, if I replace this.pathwayPlacementRectangle with something else, like a group of sprites, the code again works as how you would expect.

Possibly I'm doing something wrong with the sprite creation. Can anyone point me in the right direction please?



Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...