How do I constantly check if my object.x is > than 500 and if it is add 1 to my score variable? I want it to be immediately detected when its x coordinate is greater than 500.
by either using a timer function or a Runtime enterFrame listener
I’d recommend the last one for constant checks as it is in sync with visuals.
**UPDATED: I read your post as < 500 initially **
You can’t immediately detect this. You can only measure every frame at the very best, so the value could be much greater than 500 when detected.
physics.setGravity( 0, 0 ) local obj = display.newCircle( 10, 10, 10 ) physics.addBody( obj ) obj:setLinearVelocity( 100, 0 ) function obj.enterFrame( self ) if( self.x \> 500 ) then print(self.x , " Yo! less than 500 " ) end end Runtime:addEventListener("enterFrame", obj)
In this example, the object is moving at 100 pixels per second but starts offset at 10 pixels, so you’ll detect the crossing at just after 5 seconds or about 295 frames at 60fps. If I did my math right the object will be about 1.4 pixels past 500 at this point. So, not exact, but pretty close.
by either using a timer function or a Runtime enterFrame listener
I’d recommend the last one for constant checks as it is in sync with visuals.
**UPDATED: I read your post as < 500 initially **
You can’t immediately detect this. You can only measure every frame at the very best, so the value could be much greater than 500 when detected.
physics.setGravity( 0, 0 ) local obj = display.newCircle( 10, 10, 10 ) physics.addBody( obj ) obj:setLinearVelocity( 100, 0 ) function obj.enterFrame( self ) if( self.x \> 500 ) then print(self.x , " Yo! less than 500 " ) end end Runtime:addEventListener("enterFrame", obj)
In this example, the object is moving at 100 pixels per second but starts offset at 10 pixels, so you’ll detect the crossing at just after 5 seconds or about 295 frames at 60fps. If I did my math right the object will be about 1.4 pixels past 500 at this point. So, not exact, but pretty close.