# My code's not working how I want it to

**URL:** <https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477>\
**Category:** Help\
**Tags:** extension\
**Created:** [October 18, 2023, 7:51pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477 "2023-10-18T19:51:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blobbey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/blobbey/32/20831_2.png) [@Blobbey](https://forum.makecode.com/u/Blobbey)\
**Post date:** [October 18, 2023, 7:51pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477/1 "2023-10-18T19:51:54Z")

</div>

I’m trying to make a loading screen-adjacent extension, and the loading sprite is always on top of the text sprite, even when I set the loading sprite’s z. Does anyone have an idea of what’s wrong?

```auto
let changingY1 = 0
let changingY2 = 0
let changingX1 = 0
let changingX2 = 0
function loadingVertical(sprite: Sprite, speed: number, color: number, x1: number, x2: number, z: number) {
sprite.setFlag(SpriteFlag.RelativeToCamera, true)
for (let index = 0; index < 120; index++) {
sprite.image.drawLine(x1, changingY1, x2, changingY2, color)
sprite.z = z
changingY1 = changingY1 + 1
changingY2 = changingY2 + 1
pause(speed)
}
changingY1 = 0
changingY2 = 0
sprites.destroy(sprite)
}
function loadingHorizontal(sprite: Sprite, speed: number, color: number, y1: number, y2: number, z: number) {
sprite.setFlag(SpriteFlag.RelativeToCamera, true)
for (let index = 0; index < 160; index++) {
sprite.image.drawLine(changingX1, y1, changingX2, y2, color)
sprite.z = z
changingX1 = changingX1 + 1
changingX2 = changingX2 + 1
pause(speed)
}
changingX1 = 0
changingX2 = 0
sprites.destroy(sprite)
}
//% color="#1F7EC7" 
//% icon="\uf110"
namespace Loading {
/**
 * Creates a vertical loading screen using the inputted settings.
 */
//% block="make vertical loading screen with speed $speed and color $color on z $z"
export function loadingScreen1(speed: number, color: number, z: number) {
let loadingSprite = sprites.create(img`
`, SpriteKind.Food)
loadingSprite.z = z
loadingVertical(loadingSprite, speed, color, 0, 159, z)
}
/**
 * Creates a horizontal loading screen using the inputted settings.
 */
//% block="make horizontal loading screen with speed $speed and color $color on z $z"
export function loadingScreen2(speed: number, color: number, z: number) {
let loadingSprite = sprites.create(img`
`, SpriteKind.Food)
loadingSprite.z = z
loadingHorizontal(loadingSprite, speed, color, 0, 119, z)
}
}

```

---

<div class="post-metadata">

**Author:** ![Blobbey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/blobbey/32/20831_2.png) [@Blobbey](https://forum.makecode.com/u/Blobbey)\
**Post date:** [October 19, 2023, 6:15pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477/2 "2023-10-19T18:15:53Z")

</div>

@richard, do you know what’s wrong?

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [October 19, 2023, 6:19pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477/3 "2023-10-19T18:19:14Z")

</div>

@Blobbey can you share your whole project, not just the extension code? I don’t see any text sprites in the code you posted.

---

<div class="post-metadata">

**Author:** ![Blobbey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/blobbey/32/20831_2.png) [@Blobbey](https://forum.makecode.com/u/Blobbey)\
**Post date:** [October 19, 2023, 6:53pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477/4 "2023-10-19T18:53:13Z")

</div>

> **[Coding](https://arcade.makecode.com/S61518-60092-06350-34701)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

Here you go!

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [October 19, 2023, 7:20pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477/5 "2023-10-19T19:20:28Z")

</div>

Thanks! Your issue here is that you’re pausing in your extension code, so the text sprite isn’t actually getting created until the loading bar finishes. The z-index is working properly.

There are two possible fixes here:

1. Simply move your text sprite creation above your call to your loading function

2. In your extension, wrap your functions inside a call to `runInBackground` like so:

---

<div class="post-metadata">

**Author:** ![Blobbey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/blobbey/32/20831_2.png) [@Blobbey](https://forum.makecode.com/u/Blobbey)\
**Post date:** [October 19, 2023, 7:36pm UTC](https://forum.makecode.com/t/my-codes-not-working-how-i-want-it-to/23477/6 "2023-10-19T19:36:00Z")

</div>

Okay, thank you so much!
