# Manual Sprite Liftetime Management

**URL:** <https://forum.makecode.com/t/manual-sprite-liftetime-management/29107>\
**Category:** Help\
**Created:** [June 5, 2024, 5:02pm UTC](https://forum.makecode.com/t/manual-sprite-liftetime-management/29107 "2024-06-05T17:02:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alex/32/12391_2.png) [@Alex](https://forum.makecode.com/u/Alex)\
**Post date:** [June 5, 2024, 5:02pm UTC](https://forum.makecode.com/t/manual-sprite-liftetime-management/29107/1 "2024-06-05T17:02:32Z")

</div>

So i’ve recently decided to get back into Makecode for a little bit after spending a couple of months learning C++ Gamedev, and one of the things I’ve been finding hard to adjust to is not being able the manage the lifetimes of my sprites. I can use the sprites.destroyAllSpritesOfKind function but I feel as though its limiting to need to make a SpriteKind that is forced to be destroyed. I’ve tried storing an array of sprites but that causes a null access exception. Is there a alternative solution?

---

<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:** [June 5, 2024, 5:03pm UTC](https://forum.makecode.com/t/manual-sprite-liftetime-management/29107/2 "2024-06-05T17:03:03Z")

</div>

@Alex can you share the code that’s giving you the exception?

---

<div class="post-metadata">

**Author:** ![Alex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alex/32/12391_2.png) [@Alex](https://forum.makecode.com/u/Alex)\
**Post date:** [June 5, 2024, 6:20pm UTC](https://forum.makecode.com/t/manual-sprite-liftetime-management/29107/3 "2024-06-05T18:20:51Z")

</div>

```auto
let SpriteArr : Array<Sprite>

SpriteArr.push(sprites.create(SpriteTex.White))

```

> Uncaught Dereferencing null/undefined value.  
> at (main.ts:1:1)

---

<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:** [June 5, 2024, 6:22pm UTC](https://forum.makecode.com/t/manual-sprite-liftetime-management/29107/4 "2024-06-05T18:22:03Z")

</div>

@Alex you need to assign that variable:

```typescript
let spriteArray: Array<Sprite> = []
spriteArray.push(sprites.create(SpriteTex.White))

```

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [June 5, 2024, 10:36pm UTC](https://forum.makecode.com/t/manual-sprite-liftetime-management/29107/5 "2024-06-05T22:36:46Z")

</div>

Hi, @Alex !

Also keep in mind that, depending on what specifically you’re trying to do, there may be built-in tools that can help you.

MakeCode is event-driven, so some aspects of sprite lifetime could be handled in, say, the sprite overlap event handlers.

There also is a sprite flag called _auto-destroy_. If a sprite with auto-destroy leaves the screen, then the sprite is automatically released without having to manage it explicitly.

```typescript
let projectile: Sprite = sprites.create(img`.`, SpriteKind.Projectile)
projectile.setFlag(SpriteFlag.AutoDestroy, true)
projectile.setVelocity(25, 25)

```

Of course, there’s nothing wrong with managing sprite lifecycle yourself! Just know that there may be other tools available to help you.
