# Getting a Sprite to "Destroy" to show new level in Gamelab

**URL:** <https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326>\
**Category:** Coding and Debugging Help\
**Created:** [December 5, 2022, 8:42pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326 "2022-12-05T20:42:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 5, 2022, 8:42pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/1 "2022-12-05T20:42:24Z")

</div>

Hello All,

I am an instructor new to [code.org](http://code.org) and i have a couple students making projects in here independent of the set curricula. Both students technically have more Gamelab experience than I do! I’ve done more “real world” coding.

My student and I can’t figure out how to advance to the next level in this project. The idea is that once the character walks through (to the right) and touches the trophy, a You Win screen appears and then, once you click on that, the background sprite is ‘destroyed’ and now the character is on a different level (Different sprite bg).

> **[Check out what I made](https://studio.code.org/projects/gamelab/Y9nsOsp_uvNYGbEMasDlOF5S0kQ61QnNMWt0dKjItsc)**
>
> I wrote the code myself with Code.org

Any suggestions would be great! Thank you, Bridget

**Link to the project or level:** [replace with link to the curriculum or get the “Share” link to a project]  
**What I expect to happen:** [replace with a detailed description of the behavior you’re trying to create]  
**What actually happens:** [replace with a detailed description of what actually happens when the code runs including any errors or unexpected behavior]  
**What I’ve tried:** [replace with a detailed description of what you’ve already tried to do to solve the problem]

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 5, 2022, 10:59pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/2 "2022-12-05T22:59:38Z")

</div>

@batchcooley.bridget ,

Looks like a fun game! I can see they have learned quite a bit.

This suggestion should help them, but if they get down the road and it isn’t working, feel free to check back in.

I would suggest they create a level variable at the top of the game. For example: `var level=1;`

Then, inside the draw loop, there could be a conditional statement checking for the level. ` If level == 1, { ... }`

The background and other elements you want to change could be where the … is now.

That could be expanded to be an if else block and if the level is 2, it could have a different background etc.

I hope this is enough to get them started. Have them give it a shot and see if they can add a 2nd level, but check back in if you need a pair of eyes on it.

Good luck!

Mike

---

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 6, 2022, 11:15pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/3 "2022-12-06T23:15:50Z")

</div>

Thank you, Mike! He and I looked through this today and we shall see how he’s able to build on it! I’ll put in an update.

---

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 9, 2022, 8:04pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/4 "2022-12-09T20:04:27Z")

</div>

Helllo again! We have made some progress. The level DOES change, but now I think the background is not re-drawing properly because we see the sprites every single re-draw. It’s a cool effect, but not desired. If anyone has a chance to take a look at this, that is much appreciated! Thank you!

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [December 10, 2022, 2:36am UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/5 "2022-12-10T02:36:03Z")

</div>

@batchcooley.bridget,

Glad the level change is working for you. The sprites are creating a trail because the background. is not being drawn consistently. The way the code is written, the drawSprites is only being drawn when the mouse is clicking the “win”. To fix this, the first step is to change the beginning value of level from 2 to 0. Then, change the mousePressedOver(win) actions ONLY to set a new value of level = 2. Then create another if statement (not nested) so that if level == 2, the bg2 is visible and the 1st background is hidden. Hope that makes sense. Good luck!  
~Michelle

---

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 19, 2022, 8:40pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/6 "2022-12-19T20:40:20Z")

</div>

Hi, thank you Michelle! I am so proud of the student, he was able to work this out himself, actually, before I could get back to him! I appreciate your post.

Of course this leads to my next question. Getting his sprites to reset into the same place is leading to issues. I am trying to think through how to reset the sprites’ positions when the level changes. He had the idea of creating a function so that maybe the function can just be called, and then the positions could be reset. This makes sense to me but we aren’t getting it to work! Ack, another call for help! Thank you everybody!

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 20, 2022, 4:12pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/7 "2022-12-20T16:12:25Z")

</div>

Has he tried to implement this yet? What are the problems?

I assume he is trying to change the x and y values of the sprites? The trick would be determining when to do that.

A function would be a good way to do it.

Let us know once he tries if it isnt working.

Mike

---

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 20, 2022, 6:26pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/8 "2022-12-20T18:26:58Z")

</div>

That’s exactly where we are at. I can’t quite figure out why his functions won’t work ☹

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 20, 2022, 7:30pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/9 "2022-12-20T19:30:50Z")

</div>

Ok. Maybe I’m missing something because when he passes the first level, he has it coded so if you click on “win”, it changes the background and moves the kid back to the left and it seems to be working … maybe he figured it out?

If not, let me know.

thanks,

Mike

---

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 20, 2022, 10:07pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/10 "2022-12-20T22:07:58Z")

</div>

Well, we coded in that “kid” x,y position in the particular conditional statement he has there. But he (we) had wanted a function so he could set those x, y coordinates just once and then re-call them at each level since there are a lot of sprites to be re-typing the coordinates over and over (or re-pasting, but you know a lot of code).

In trying different aspects / placements of the functions, I wasn’t successful. I just don’t know If i don’t understand properly, or if there’s some sort of [code.org](http://code.org) thing i am missing. Thanks!

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 21, 2022, 3:53pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/11 "2022-12-21T15:53:38Z")

</div>

Ok… I may have to see what he tried to be able to see why it’s not working.

Here’s a remix I made where I created a function called resetSprites that essentially does what your game does when clicking on the finish box.

> **[Check out what I made](https://studio.code.org/projects/gamelab/Ae5HCJD5Cct09n8FiLcMd3W0SCJBM81K0HK-6aIBQbw)**
>
> I wrote the code myself with Code.org

Mike

---

<div class="post-metadata">

**Author:** ![batchcooley.bridget](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batchcooley.bridget](https://forum.code.org/u/batchcooley.bridget)\
**Post date:** [December 21, 2022, 5:31pm UTC](https://forum.code.org/t/getting-a-sprite-to-destroy-to-show-new-level-in-gamelab/37326/12 "2022-12-21T17:31:26Z")

</div>

Hello! This looks very interesting. Thank you for the quick reply and I will report back on how it goes working with the student. We are wrapping up for the semester but I would like us to come back to these projects we are doing in a couple months and finalize them!
