# How to iterate trough a list of spritekinds?

**URL:** <https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083>\
**Category:** Help\
**Created:** [February 8, 2022, 5:06pm UTC](https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083 "2022-02-08T17:06:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanchik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/hasanchik/32/5885_2.png) [@hasanchik](https://forum.makecode.com/u/hasanchik)\
**Post date:** [February 8, 2022, 5:06pm UTC](https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083/1 "2022-02-08T17:06:20Z")

</div>

I need help iterating trough a list of spritekinds! What im trying to do is first iterate trough a spritekind, and then iterate trough all sprites that spritekind has.

```auto
let optimizeSpriteKind = [SpriteKind.Coin]

/*
function IsInScreen(sprite: Sprite) {
    let ox = scene.cameraProperty(CameraProperty.X)
    let oy = scene.cameraProperty(CameraProperty.Y)

    if (sprite.x + sprite.width > ox - (xw / 2)) {
        if (sprite.x - sprite.width < ox + (xw / 2)) {
            if (sprite.y + sprite.height > oy - (yh / 2)) {
                if (sprite.y - sprite.height < oy + (yh / 2)) {
                    return true
                }
            }
        }
    }
    return false
}
*/

forever(function () {
    for (let kind of optimizeSpriteKind) {
        let allSpritesKind = sprites.allOfKind(kind)
        for (let sprite of allSpritesKind) {
            if (sprite.isOutOfScreen(game.currentScene().camera)) {
                sprite.setFlag(SpriteFlag.Invisible, false) //testing
            } else {
                sprite.setFlag(SpriteFlag.Invisible, true) // testing
            }
        }
    }
})

```

Once again i have no idea why this doesnt work

---

<div class="post-metadata">

**Author:** ![CrownYou](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@CrownYou](https://forum.makecode.com/u/CrownYou)\
**Post date:** [February 8, 2022, 5:37pm UTC](https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083/2 "2022-02-08T17:37:56Z")

</div>

![problem](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/18e54b37e477fd8fcb890f7cfc0d6b68db482040.png)  
You can try this to iterate through a list of sprite kinds.

---

<div class="post-metadata">

**Author:** ![hasanchik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/hasanchik/32/5885_2.png) [@hasanchik](https://forum.makecode.com/u/hasanchik)\
**Post date:** [February 8, 2022, 5:45pm UTC](https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083/3 "2022-02-08T17:45:24Z")

</div>

> [@hasanchik](#):
>
> ```auto
> for (let kind of optimizeSpriteKind) {
> let allSpritesKind = sprites.allOfKind(kind)
> for (let sprite of allSpritesKind) {
> if (sprite.isOutOfScreen(game.currentScene().camera)) {
> sprite.setFlag(SpriteFlag.Invisible, false) //testing
> } else {
> sprite.setFlag(SpriteFlag.Invisible, true) // testing
> }
> }
> }
> 
> ```

I know, but im going to iterate trough a list of spritekinds and then iterate trough a list of all sprites with that spritekind

---

<div class="post-metadata">

**Author:** ![CrownYou](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@CrownYou](https://forum.makecode.com/u/CrownYou)\
**Post date:** [February 9, 2022, 6:03pm UTC](https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083/4 "2022-02-09T18:03:57Z")

</div>

![myAnswer](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/b9b8334dfd0d388550a558370a6a2d3b8f702165.png)  
I got what you mean, try this: add all the spritekinds to an empty array manually, and then, iterate through the array to pick the expected spritekind. Next, iterate through the sprites of the spritekind to pick the expected sprite.  
I think it will definitely help you!

---

<div class="post-metadata">

**Author:** ![hasanchik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/hasanchik/32/5885_2.png) [@hasanchik](https://forum.makecode.com/u/hasanchik)\
**Post date:** [February 9, 2022, 7:35pm UTC](https://forum.makecode.com/t/how-to-iterate-trough-a-list-of-spritekinds/12083/5 "2022-02-09T19:35:36Z")

</div>

Thanks for the help, but i’ve already figured it out: the spritekinds initialize a bit later than when the array is made, so all the values in the array are undefined. I just added a “do after 500ms” code with the timer extension to fix this
