mousePressedOver() true even after sprite is destroyed

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Link to the project or level: [Game Lab - Code.org]
What I expect to happen: [After clicking on the flower (or the fly), the screen should change and if I then click on the area where the fly (or the flower) WAS, I don’t want it to recognize that as the mouse being clicked over the fly (or the flower).]
What actually happens: [After clicking on the flower (or the fly), the screen changes as expected. But if I click on the area where the fly (or the flower) was, the code thinks I clicked on the fly (or the flower) which is no longer there.]
What I’ve tried: [The fly and flower sprites should have been destroyed in lines 141-148. I thought that when a sprite is destroyed it would make it so mousePressedOver(sprite) would no longer be true. But that doesn’t seem to be the case.
I also tried moving the fly & flower sprites off the screen by setting sprite.x=500. But I can still click on the spot where the sprite WAS and get a reaction.
I added a couple console.log messages to show when the code thinks I clicked on the flower and the fly and the messages show up repeatedly when I click in the area where teh flower and fly are at the start.]

So… re positioning the sprite should work but i went for the more obvious answer which relies on your .visible property of both options since you can set the property you can understand that getting that value to be tested could also work, additionally having two event statements isn’t going to be helpful so i centralized both on the two if statements https://studio.code.org/projects/gamelab/WwpatL4ysiLL8HfjrbAQl9O1L55Rwv0rGZeHiEaJSn8/view but this is what i did there was a lot of clutter so hopefully this is what you were looking for!

@jwilson25,

I have seen issues with the .destroy not actually destroying sprites. I’m not sure why, but it’s not the first time I’ve seen that not play out as intended.

The reason moving it doesn’t work is that on lines 62 & 63 of the code the flyFirst is randomly placed back at nearly the same location on the screen. So, it would move off the screen, but immediately on the next pass through the draw loop, it is moved back to the scene of the crime.

Also as @varrience points out, you do call the MousePressedOver twice in the code and that can lead to unintended consequences as well because what may be true the first time it is called may not be true a millisecond later when it is called again.

If I were tackling this, I would probably find a way to randomly place the sprite before starting the draw loop and then I would move the sprite out of the way after the first time it is pressed over.

Good luck!

Mike

The reason moving it doesn’t work is that on lines 62 & 63 of the code the flyFirst is randomly placed back at nearly the same location on the screen.

Yup…I had overlooked that.
And I had tried consolidating the code into a single IF statement as you suggest, but that didn’t work. But I agree that is something to do.

If I were tackling this, I would probably find a way to randomly place the sprite before starting the draw loop and then I would move the sprite out of the way after the first time it is pressed over.

That would eliminate the movement of these two sprites, which I think she wants to keep. But we could add a IF around the random placement to prevent the issue of the sprites coming back “to the scene of the crime.”

Thank you!

Yes, a simple toggle variable that is set to 0 at the beginning and then changed to 1 after the fly is clicked could easily be turned into a conditional statement that would control whether or not the fly would be randomly moving around that spot of the screen and that would solve the problem.

Mike

Thanks for this effort! I like the extendSprite() function, though we’re not up to that, yet.
I thought using a Boolean to keep that code from running once the sprite has been clicked might be a good solution. I like using the visible property to do this. Thank you!

John