I just can't figure out the solution ;(

I’ve been working on a PVZ game for fun, and I’ve been trying to add in the code that prevents you from placing plants on tiles where plants already exist. I’ve created a variable called “CanPlant” and I’ve tried setting it to false whenever the player overlaps a plant, but it only ever works on the last peashooter I’ve placed.

If I have two peashooters, I cannot place another one on top of the last peashooter I just placed, but I can place it on any peashooter placed prior to it.

I also tried creating an invisible sprite that spawns when a peashooter spawns, and if you overlap with that it sets “CanPlace” to false, but the same issue arises.

I’m so close yet so far, does anyone know the solution? https://arcade.makecode.com/S88710-92644-30700-44480

1 Like

I would just ditch the variable approach and simply check if the crosshair overlaps with a sprite of the plant kind. Let me know if this helps.

Overlap blocks are very glitchy I recommend ditching them and checking for the closest sprite to detect overlaps instead. I’ve run into overlap issues many times, most notably with my isometric city builder a few years ago
You can read my topic on it for some more info. [Tutorial] Making an Enemy Follow Multiple Sprites :snake: it’s about making enemies follow multiple sprites by finding the nearest sprite. You can isolate the nearest sprite part of the code, combine it with a range the size of the sprite, and you get a perfect overlap function.

2 Likes

Oh yeah I tried that approach too, along with a coordinates based block is meant to prevent you from placing when the distance between the crosshait and peashooter are zero, both had the same issue as my other attempts…

1 Like

Ooh, since I have no clue how arrays work, I have a very frustrating solution, I can spawn an invisible sprite on each grid, to which my crosshair must overlap with it to place a plant, once a plant is placed, the sprite on that tile is destroyed.

1 Like