How do I do isSensor?

Hello, I was wondering how to do isSensor while using lua. I have added the physics engine into my code however I have no idea on how to actually do isSensor. I have tried using an event listener to start the “began” function however nothing happens. This is the code we have attempted to use.

local function range(event) if event.phase=="began" then print("test"..event.target.id) return true end if event.phase=="ended" then return false end end Runtime:addEventListener("postCollision",range)

Any idea on what I am doing wrong or how to fix it?

First, set the object to .isSensor = true, then do everything else as normal. Collisions with isSensor objects won’t be resolved, and they’ll only report their collision events.

I think in your case the problem is that you’re using “postCollision” instead of “collision”. “postCollision” listens for when the collision finishes, so I don’t think it has an event.phase. Change “postCollision” to “collision” and see how that helps.

  • Caleb
local function inRange(event) while event.phase=="began" do print("test") enemy1health=enemy1health-1 health.text="Health"..enemy1health return true end if event.phase=="ended" then return false end end obj1:addEventListener("collision",inRange) Runtime:addEventListener("collision",inRange)

it only displays once, when the enemy touches the edge of the circle. After it touches the first time it does not display the print line again even though the the enemy is still in range of the tower. We are using a while loop so it should display as long as long as the enemy is in range, but the while does not seem to work. What are we doing wrong?

For one thing, a ‘while’ loop repeats as fast as it can, so if it ever did work, it would hang the whole program forever :slight_smile:

For another, collision only fires for “began” phase once - when the collision begins. So to do what it looks like you’re trying to do, you could add an “isCollisionOccurring” flag somewhere, then set it to true when the collision begins and false when the collision ends, then in your enterFrame listener check for if isCollisionOccurring, etc.

  • Caleb

First, set the object to .isSensor = true, then do everything else as normal. Collisions with isSensor objects won’t be resolved, and they’ll only report their collision events.

I think in your case the problem is that you’re using “postCollision” instead of “collision”. “postCollision” listens for when the collision finishes, so I don’t think it has an event.phase. Change “postCollision” to “collision” and see how that helps.

  • Caleb
local function inRange(event) while event.phase=="began" do print("test") enemy1health=enemy1health-1 health.text="Health"..enemy1health return true end if event.phase=="ended" then return false end end obj1:addEventListener("collision",inRange) Runtime:addEventListener("collision",inRange)

it only displays once, when the enemy touches the edge of the circle. After it touches the first time it does not display the print line again even though the the enemy is still in range of the tower. We are using a while loop so it should display as long as long as the enemy is in range, but the while does not seem to work. What are we doing wrong?

For one thing, a ‘while’ loop repeats as fast as it can, so if it ever did work, it would hang the whole program forever :slight_smile:

For another, collision only fires for “began” phase once - when the collision begins. So to do what it looks like you’re trying to do, you could add an “isCollisionOccurring” flag somewhere, then set it to true when the collision begins and false when the collision ends, then in your enterFrame listener check for if isCollisionOccurring, etc.

  • Caleb