Objects offscreen

After following on after the space shooter tutorial I have adding my own sounds to the enemies when they fire however if an enemy still lives and exits the screen I can still hear the enemies firing is there a way i can stop my enemies from firing once they leave the screen? Thanks
I have tried destroyed object behaviour but that didn’t work very well.

Destroy when out of screen should work, so what’s wrong with it?


Well if i let all the enemies past me and exit the screen I can still hear the bullets being fired if the destroy object outside of screen is applied to their bullets.
if i add destroy outside of screen to the enemy itself they get destroyed before they enter the screen in the first place.

So, activate the behavior only when you need it. There’s an action for that.

Gruk is correct. Make sure you only have your events for firing/sounds/etc active if the object is in the visible area.

That’s pretty easy by comparing it’s X to the center of your main layer’s camera. So like:
X position of Object > CameraX("",0)-(CameraWidth("",0)/2)
X position of Object < CameraX("",0)+(CameraWidth("",0)/2)

You could also do the same for Y position just to be safe.

1 Like

Hiya thank you for trying to help.

I have tried doing the position but no such luck so far i also tried the camera width on the condition side and didn’t have much luck there

You have to do the full expression. Just CameraX() won’t give you anything.

At a high level: Never type expressions. Use the expression builder. If you select Camera X position from the expression builder it will show you all of the required paramters.


I feel very confused now I am not sure what to do

i can’t seem to find a way of this appearing on my screen I have only been using gdevelop about 2 weeks now so I am still very new to it.

  1. Click to add a new condition in the event.
  2. Search for Position.
  3. Select the Compare X position of an object from the list.
  4. On the right, select your object.
  5. Within the Sign of the Test field, select >.
  6. Within the Value to compare field, click the Blue box on the right. This is the expression builder.
  7. Once the new search box pops up, search for Camera
  8. Within the results list, select Camera center X position. It will then show you the list of parameters you have to provide, including layer and camera number. (Camera number is always 0).
  9. You need to select whatever layer your objects are on. Do not leave it blank. If it’s your base layer, put in “” anyway.
  10. After that point, you’ll need to add a - (minus) after the expression gets added to the value box. You’ll then need to click the expression builder again and repeat the steps above, but select Width of a camera.

What you’re doing with an expression is saying “Is your object’s X position currently anywhere within the range of the Center of the camera minus or plus 1/2 it’s width”, which would basically cover any coordinates within your camera’s view.

And if you want them to be on the screen, both events need to be in the same event box (conditions in the same box are treated as if you were saying “THIS AND THIS”)


Hiya thank you for walking me through this so I got it all typed in.

So now once the enemy leaves the screen it does only one SFX of the gun were as before it was going on indefinitely so massive improvement so far. I imagine there is some way of adjusting some value so it cuts off a bit sooner?

You used CameraWidth, but you need to use half of it, as @Silver-Streak wrote above.
Center - half makes one edge.
Center + half makes the other edge.

Thank you so much both of you it worked :grinning::grinning::grinning::grinning:

1 Like