How to detect object at the bottom of screen or off screen

Hey guy i am having a little problem developing my new app. i am trying to detect when a object y position  is grater then the screen size, if its grater then the screen size it would remove the object  this my code 

 function spawnblcok()

     block = display.newImageRect(“block01.png”, 45, 45);

    block:setReferencePoint(display.CenterReferencePoint);

    block.x = math.random(-10, 300);

    block.y = -40;

    transition.to( block, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600,});

    physics.addBody(block, “dynamic”, {density = 0.1, bounce = 0.1, friction = .1, radius = 0});

    --Adding touch event

      block:addEventListener(“touch”, touchUp)

if block. y > display.contentHeight then 

 print  “remove objects”

  end

end

 total_block = 10

timer1 = timer.performWithDelay(1000, spawnblcok, total_block); 

this is the part of code thats not working

if block. y > display.contentHeight then 

 print  “remove objects”

  end

can someone help thanks ! :slight_smile:

Your problem is that spawnBlock only checks your Y position once.   There are two ways to do this.  One is to use an enterFrame listener on the Runtime that watches the current position of block.  It checks 30 times per second (for a 30 fps app, or 60 times a sec for a 60 fps app).  If it sees the block go off screen, then you can have it remove it at that time.

The other thing to do is to use physics and create a wall that’s off screen a little bit and when block collides with wall, it removes it.

thanks Rob i look into that you are a life saver bro 

Your problem is that spawnBlock only checks your Y position once.   There are two ways to do this.  One is to use an enterFrame listener on the Runtime that watches the current position of block.  It checks 30 times per second (for a 30 fps app, or 60 times a sec for a 60 fps app).  If it sees the block go off screen, then you can have it remove it at that time.

The other thing to do is to use physics and create a wall that’s off screen a little bit and when block collides with wall, it removes it.

thanks Rob i look into that you are a life saver bro