Jump to content

Collision with game.add.graphics and a sprite


Recommended Posts


I would like to make a line shape (with a background color) and have a collision event fire when a sprite is on top of the line shape? How can I do that?

shapeGr = game.add.graphics(0, 0); game.physics.enable(shapeGr, Phaser.Physics.ARCADE);shapeGr.moveTo(250, 100);shapeGr.lineTo(250, 0);// Try to overlap:game.physics.arcade.overlap(thing.body, shapeGr, gotHit, null, this);


Link to comment
Share on other sites

  • 2 months later...

You can do this a number of ways, with BitmapData (draw the line on the BitmapData instance then use it as the key for a Sprite) like in this example, or simply by adding the Graphics object as a child of the Sprite, like below:

shapeGr = game.add.graphics();
shapeGr.moveTo(250, 100);
shapeGr.lineTo(250, 0);

// Remove the 10 pixel padding added to graphics by default
shapeGr.boundsPadding = 0;

// Create an empty sprite as a container
shapeSprite = game.add.sprite(0, 0);

// Add the graphics to the sprite as a child

// Enable physics on the sprite (as graphics objects cannot have bodies applied)
game.physics.enable(shapeSprite, Phaser.Physics.ARCADE);

// Overlap should now work
game.physics.arcade.overlap(thing.body, shapeSprite, gotHit, null, this);
Link to comment
Share on other sites


  • Recently Browsing   0 members

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