Sprite Array Help

Is there any possible way to create 50 sprites from the [on each (img) tile create sprite with image (img) and kind (spriteKind)] block from the tileUtils extension, put them in an array, and delete the sprites, but keep the array?

1 Like

Yes… kinda… maybe? I would have to do some testing, ofc. I know that, for example, the “distance between (sprite) and (otherSprite)” block in the sprite util extension still returns a correct value for a sprite in a variable, even after said sprite is destroyed, which means that data can be preserved. I would also want to know what you are doing, and if it would be better to simply turn sprites invisible rather than fully destroy them? Are you trying to save sprites using the Settings Extension? Such things must be considered, I suppose.

why not just make an array of sprites? you can add them with


unless you need to get data (like position) from the existing sprites after they’re created i don’t see the point of adding them in and immediately deleting them

1 Like

making them invisible might fix a bit of lag, but not much. Unless you are running on hardware when looking at the fps, your computer is really underpowered, or you are expecting the rest of your game code to be quite laggy, I don’t see why you are starting with a baseline of only 200 fps. Games usually run much higher than that, even with 30 sprites involved. You might have some other issues making your game much more laggy. Besides that, I do have something for you. I added a “undestroy” block to my extension woofwoofcodes/pxt-javascript-extras and here is a demo on how you might use it: https://arcade.makecode.com/S27963-40696-66616-14916
(It took so long to figure this out! And I have no idea if everything is working correctly. If you find any bugs just @ me!)

Yeah, usually games run a lot higher than that, at least on my computer, even my school chromebook. (I get my fps numbers from the same place. Technically, I believe the screen only updates at 30 fps, but game logic runs at the displayed fps)

Oh and to avoid them looking like they are popping up from the side of the screen, you can increase those "60"s and "80"s to be more like "70"s and "90"s, which will make them appear/disappear off screen. Just make sure you don’t mess up the negative numbers and make them positive. This does pause all physics / movement for the sprite while they are destroyed.

Other side effects of the “undestroy” block are that sprites destroyed with effects will look really weird when they are undestroyed. It will also mess with other extensions, depending on how they handle sprite deaths, because some will continue running after the sprite returns and some have “on sprite destroyed” handlers that get rid of the extension’s effect on the sprite. Basically you might have to re-setup the sprite somewhat if you are using certain features, for example the Follow Sprite effect will disappear.

Hate to say this, but it probably won’t help much. I believe that off screen sprites are already filtered out of the rendering code.

Rendering isn’t what makes them laggy. That’s why sprites being off screen doesn’t help much with the lag. What makes them laggy is all the physics, collision checks, etc, which is why deleting them improves the game so much. If you need them to still be moving, you could try turning “ghost through sprites” on so that they aren’t doing sprite collision detection, though idk how much that will actually help depending on how much of that is already filtered. Also, you mentioned moving them around. Are you using custom movement code, or something that would be looping through many sprites and doing calculations? I still can imagine why your project runs at a baseline of 200 fps without sprites unless something else is already creating lag. Maybe the 3D extension?

1 Like

ghosting sprites is insanely helpful for reducing lag
image

2 Likes

I believe it stops interacting with walls and other sprites. This would be good if you froze the movement of all sprites so they don’t end up inside walls, but if you rely on walls, it isn’t a good option. I’m curious to know how many fps you get when you run an empty new project…

1 Like