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

so I’m trying to make a game, but if I add around 30 enemies (even if their off screen), the game will run at 20 fps rather than 200. I’m asking if there’s a way to keep the sprites in an array, but so that the game doesn’t lag.

my game was lagging and lag is my issue

I guess I could make the sprites invisible if there far away.

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!)

I got 200 fps from the show stats button in the menu of the console, and 20 from the same thing.

1 Like

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.

the sprites might still supposed to be moving so making the invisible would be better.

1 Like

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.

weird, because if I had like 5 sprites, it wouldn’t lag, but 30 would cause it to lag, even if the sprites are off screen (I guess it could be the way I move them around that lags the game).

1 Like

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

Should I make it do that when the sprites don’t need to be moving, that they stay still and have no acceleration and such? I am using my own movement code to move them, but the game lagged even if there was no movement code. Also, my game is 2D, not 3D, and uses the overworld extension for switching screens.

1 Like

ghosting sprites is insanely helpful for reducing lag
image

1 Like

what is everything that ghost mySprite does?

1 Like

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

so when the sprite needs to stop, ghost it and stop acceleration?