Jump to content

Determine body overlap against the group\Preventing sprite creation at certain points


bjorny
 Share

Recommended Posts

Hi everyone,

 

I'm trying to make a simple puzzle game where the player has to move around horizontally and place blocks to build a certain structure (kind of like 2D Minecraft, but this is not the point :)). Something like this:

 

post-10138-0-52005900-1410881803.png

 

(this is only a sketch, of course :))

 

The problem is, I want the player to be able to place blocks only if they wouldn't overlap with each other - I made a separate invisible sprite to serve as a 'bounding box' where the block would be placed if it's not overlapping with the others (the body is highlighted), but I'm having real troubles making the game keep track of whether the bounding box is overlapping the blocks group.

 

I tried several ways, for instance:

 

 

boundOverlap: function (bound){
        
            this.blocking = true;
        
    },

 

update: function () {

      this.blocking = false;

      this.physics.arcade.overlap(this.bound, this.blocks, this.boundOverlap, null, this);

 

}.

 

 

But then I realised that the value of this.blocking just keeps switching with the speed of light.

 

How do I make the dude place the blocks at the specified distance?

 

I'm sorry if this is a dumb issue, but I really can't figure it out - I also couldn't find anything in the docs or examples about this.

Link to comment
Share on other sites

The easiest way to do this (and the way Minecraft and basically any other game you care to mention that involves crafting via blocks or squares) is via a grid. If you store the objects in your world as a grid, you can then easily check to see if an object exists or doesn't exist at a certain point in that grid. The problem with a grid is that it forces any objects that use the grid to adhere rigidly to that grid, so if you want truly arbitrary placement, then you'll either need to increase the 'fineness' of the grid and check multiple grid squares, or abandon that and go for a routine which instead does the kind of overlap testing you've been having trouble with.

 

To do it with overlaps, rather than testing for overlapping every single frame, just test for it when you want to place a block:

placeBlock: function() {  if (!this.physics.arcade.overlap(this.bound, this.blocks)) {    // you can place the block, as nothing is overlapping  }  else {    // you can't place the block  }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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