Jump to content

Check Collision - create object


DbD
 Share

Recommended Posts

Hi, i'm trying to create an object to the right of another object. I want it to check if there is an object that exists already, and if not then create the same object to the right 48 px. I tried this: 

grass = game.add.sprite(0, 0, 'grass');        if (!grass.body.touching.right) {        game.add.sprite(grass+48, 0, 'grass');    }

but no luck. any solutions?

 

p.s. i'm fairly new to the Phaser framework, and i'm still learning how to do things like collision checking in phaser. Thanks!

Link to comment
Share on other sites

This is a function I am using from one of the phaser tutorials, it checks for overlap instead of collision though.

 

checkOverlap: function(spriteA, spriteB) {
var boundsA = spriteA.getBounds();
var boundsB = spriteB.getBounds();

return Phaser.Rectangle.intersects(boundsA, boundsB

 

It returns a matrix instead of just an X or Y value though, so you'll need to place the grass keeping this in mind. Then the return just returns a true or false for your conditional.

 

Hope that helps!

}

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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