bjorny Posted September 16, 2014 Share Posted September 16, 2014 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: (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 More sharing options...
lewster32 Posted September 16, 2014 Share Posted September 16, 2014 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 More sharing options...
bjorny Posted September 16, 2014 Author Share Posted September 16, 2014 Wow, how could I be so ignorant not to think about this) Thanks a lot, lewster32! The grid scenario is also a good point, I will take note of this as well. Link to comment Share on other sites More sharing options...
Recommended Posts