Removing and respawning sprites

Hi all,

Programming uber-beginner here. Having a bit of trouble figuring out one (of many) problem(s) with a current small-scale game project: https://editor.p5js.org/jakobfree/sketches/B09u80zKh

I’m trying to create a sprite (“lava”) that floats across the screen of my sketch and does a few things:

  1. if it flies off the screen, it returns to its origin point (“volcano”)
  2. if it hits a specific type of other sprite (“jungle”), it decreases the overall score in the game
  3. if the player hits it with his/her avatar (“islandgod”) the lava sprite is destroyed (using remove) and then the sprite…
    3.5) …will respawn at its origin point to start the process all over again

Right now I’ve got 1-3 working pretty well, but as soon as I use the remove method in p5 play, the sprite is removed permanently, and try as I might I cannot get it back. Now, I know that remove may not be the best solution to my problem, but I’m not sure what else to to do here.

Here’s the code I am using to destroy the sprite:

  islandgod.overlap(lava, stopLava); 

  function stopLava(islandgod, lava) {
  lava.remove();
}

It may be best to just check the sketch out directly. Please let me know if there is any additional info I can supply. Thanks!

1 Like

Do you want to remove it and then create a brand new one at the volcano? Or do you want to relocate it to the volcano? Either approach could work.

What I’ve done as a sort of temporary solution (which may just turn out to be an okay fix anyway) is to turn off the lava sprite’s visibility if/when it hits the player. And I’ve made it so that an invisible lava sprite can’t do damage to anything. Its trajectory is still unaffected of course, so it eventually flies off the screen and then reappears at the volcano thanks to a simple bit of code regarding the boundaries of the sketch.

1 Like