How to detect when a sprite out of screen and delete it? (SOLVED)

Hi guys

I am trying to make a shooting game using defold

when i control the role to shoot the bullets ,i occured a problem that the sprite buffer was not enough ,i can increase the buffer size ,but i think it will cost to much memory?

so i try to detect the bullet ,when it goes out the screen ,the game will delete it
but i found two ways to get screen size is gui.get_height() and render.get_height(),and the width ,too
there is a error says this two function only call by the GUI instance and render.

is there any other way i did not find out to do this?

thanks!

Sure, the memory will increase, but not by much. I wouldn’t worry about that.

BUT you shouldn’t just leave a lot of stray bullets far off screen. It’s much better to delete them. When it comes to detecting when something goes off screen I suppose you have a couple of different options:

  1. Use tonumber(sys.get_config("display.width")) and tonumber(sys.get_config("display.height")) to get the width and height as defined in game.project
  2. Use render.get_window_width() and render.get_window_height() to get the current window width and height (in case the window has been resized). As you’ve already seen, these functions can only be called from a render script. For that you need a custom render script that you can modify. Once you’ve read these values from your render script you can store them in a Lua module or possibly in a global variable and access those values from your scripts.
  3. Add collision objects (triggers) outside of the screen and detect collisions with them.
6 Likes

Resurrection!

I’m trying to draw the screen bounds to the go, to make sure I know they’re accurate. It works initially, but as soon as the window is resized the bounds get out of whack.

I’m using render.get_window_width() and render. get_window_height() and msg.post("@render:", “draw_line”) to draw the lines.

Is there a way to accurately detect the screen bounds from a game object script, if the game dimensions have changed?

2 Likes

You should be able to set a window listener, it will be called when the window resizes:

Thanks! I now have the dimension set in project settings and the actual screen dimensions. With stretch_projection it works fine, but I use fixed_fit_projection (to keep the gameworld from stretching).

How can I calculate the screen corners from the perspective of the gameworld when using fixed_fit_projection? Please see where I’m at currently.

Not stretched (cross covers the entire area perfectly):

Stretched horizontally:

Stretched vertically:

1 Like

Have you found a solution?

Oh yes, and a great one that makes all troubles go away:

By setting projection to FIXED_AUTO, it works like I wanted it above.

1 Like

Thanks!