I'm probably doing something wrong

In this program, I have an array of images stored in a variable called frames. I wrote some code to manually update my sprite’s image in a game.onUpdate based on some tick counting.

In onUpdate, if I reference the array of images through closure, it seems to run fine.

However, if I attempt to read the array of images off of my sprite’s custom data*, there’s a crash in the runtime.

I’m sure this represents a misunderstanding on my part, but I’d love some help understanding what I’m doing wrong. (cc @jwunderl)

*comment out 65 and uncomment 68

Hmmm, weird. Looks like it’s not identifying it as an array properly if it’s stored in ‘any’. Giving a type hint fixes this: https://makecode.com/_KHv65WWkDFa6

@mmoskal is this expected / known behavior? I’m guessing just not identifying as an array vs an object literal?

Quick note, the data field is a getter so you should just be accessing as mySprite.data like you would .x and .y, without parens. I’m guessing this means we probably have wonky behavior if you have a getter returning a function, hmmm…

… of course it is. Thanks.

I think that code was inspired by code in the sprite data extension which… ahem… also invokes data like a method.

1 Like

Oopsy fixed.


It works if you do it like this:

    const animation = mySprite.data.animation as Image[]

In other words the compiler has to know that animation is an array.

I submitted https://github.com/microsoft/pxt-arcade/issues/2161 but I think this will be non-trivial to fix, so don’t hold your breath.

1 Like

As for .data(), see https://github.com/microsoft/pxt/pull/7317

1 Like