obstacle in front of character and bomb detection with no physics

hi,

  • i would do this :

my character is behind a obstacle when i clic on this obstacle i receive for example:

character 1 touched

obstacle touched

But what i want receive is only :

obstacle touched

I love that my obstacle prevents one can with a click on my character.  I have put my main.lua below :

--my main.lua : local backgroundInvisible=display.newRect(0,0,2000,2000) backgroundInvisible.alpha=0.01 backgroundInvisible.myId=200 local character={} for i=1,5 do &nbsp;&nbsp;&nbsp; character[i]=display.newCircle(100,100,20) &nbsp;&nbsp;&nbsp; character[i].myId=(i) &nbsp;&nbsp;&nbsp; character[i]:setFillColor(0,0,1)--blue end local obstacle={} for i=1,2 do &nbsp;&nbsp;&nbsp; obstacle[i]=display.newRect(100,100,200,200) &nbsp;&nbsp;&nbsp; obstacle[i].myId=40 &nbsp;&nbsp;&nbsp; obstacle[i]:setFillColor(0,1,0) end local bomb={} for i=1,2 do &nbsp;&nbsp;&nbsp; bomb[i]=display.newCircle(120,100,10) &nbsp;&nbsp;&nbsp; bomb[i].myId=50 &nbsp;&nbsp;&nbsp; bomb[i]:setFillColor(1,0,0) end local function touchCharacter(event) &nbsp;&nbsp;&nbsp; if&nbsp; event.phase =="ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; target = event.target &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for k=1,200 do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if target.myId == (k) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if k \<=5 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("character",k,"touched") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif k == 40 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("obstacle touched") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif k == 50 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("boum!!!!") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif k == 200 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("background touched") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end--for &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end--if &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end--if &nbsp;&nbsp;&nbsp; end--if end--touchCharacter for i=1,#character do &nbsp;&nbsp;&nbsp; character[i]:addEventListener("touch",touchCharacter) end for i=1,#obstacle do &nbsp;&nbsp;&nbsp; obstacle[i]:addEventListener("touch",touchCharacter) end for i=1,#bomb do &nbsp;&nbsp;&nbsp; bomb[i]:addEventListener("touch",touchCharacter) end backgroundInvisible: addEventListener("touch",touchCharacter)
  • my second question is about my bomb

when i clic on a bomb and if a character is near of this bomb i would receive  :

boum!!!

character 1 touched

except do this but it is resource hungry, have you a better solution ?

local cnttest=0 local function testPosition(k,bomb,character) print(bomb[k].x, bomb[k].y) for i=1,#character do print(character[i].x,character[i].y) &nbsp; if character[i].x==bomb[k].x+10 or character[i].x-10==bomb[k].x and character[i].y==bomb[k].y+10 or character[i].y-10==bomb[k].y then &nbsp; cnttest=cnttest +1 &nbsp;&nbsp;&nbsp; if cnttest == 1 then &nbsp;&nbsp;&nbsp; print("boum!!!", "character", i, "touched") &nbsp;&nbsp;&nbsp; end &nbsp; end end end

thank you :slight_smile:

Hi,

In your touchCharacter function, instead of iterating k from 1 to 200 could you do:

if target.myId\<=5 then &nbsp; print ("character " .. target.myId .. " touched) elseif target.myId==40 then &nbsp; print ("obstacle touched") elseif target.myId==50 then &nbsp; print ("boom!") elseif target.myId==200 then &nbsp; print ("background touched") end

If you want to test if any of your characters are close to the bomb, you will need to iterate over each character and work out the distance to the bomb.

You could calculate the distance using Pythagorus, so:

for i=1, #character do &nbsp; --Grab a local variable for the character &nbsp; local c = character[i] &nbsp; --Calculate the distance using the square root of (x distance squared)+(y distance squared) &nbsp; local distance = math.sqrt( math.pow(c.x-bomb.x, 2) + math.pow(c.y-bomb.y) ) &nbsp; if (distance\<10) then &nbsp; &nbsp; &nbsp; --If you get here, the distance between the character x value and the character y value is less than 10 pixels &nbsp; end end

Hope that helps.

hi happymongoose,

thanks for your snippet the first is lighter than me.

but you have not understand my question ; i would that if my character is behind an obstacle the touch reveal only the obstacle and not the character.

for the second snippet, it’s almost the same than my code, exept than you have only a condition, so its faster.

@espace3d, I think you might need “return true” at the end of your touch listener:

http://coronageek.com/1162/handling-corona-sdk-tap-events-vs-touch-events/

This will, in practice, stop the touch from propagating down through the other objects “below” your top-most object. Let me know if I’ve misunderstood your question.

Hi Alex@Panc,

Thanks for the links it’s solve my problem. you have fully understood my question :slight_smile:

Have a good day

Topic solved.

Hi,

In your touchCharacter function, instead of iterating k from 1 to 200 could you do:

if target.myId\<=5 then &nbsp; print ("character " .. target.myId .. " touched) elseif target.myId==40 then &nbsp; print ("obstacle touched") elseif target.myId==50 then &nbsp; print ("boom!") elseif target.myId==200 then &nbsp; print ("background touched") end

If you want to test if any of your characters are close to the bomb, you will need to iterate over each character and work out the distance to the bomb.

You could calculate the distance using Pythagorus, so:

for i=1, #character do &nbsp; --Grab a local variable for the character &nbsp; local c = character[i] &nbsp; --Calculate the distance using the square root of (x distance squared)+(y distance squared) &nbsp; local distance = math.sqrt( math.pow(c.x-bomb.x, 2) + math.pow(c.y-bomb.y) ) &nbsp; if (distance\<10) then &nbsp; &nbsp; &nbsp; --If you get here, the distance between the character x value and the character y value is less than 10 pixels &nbsp; end end

Hope that helps.

hi happymongoose,

thanks for your snippet the first is lighter than me.

but you have not understand my question ; i would that if my character is behind an obstacle the touch reveal only the obstacle and not the character.

for the second snippet, it’s almost the same than my code, exept than you have only a condition, so its faster.

@espace3d, I think you might need “return true” at the end of your touch listener:

http://coronageek.com/1162/handling-corona-sdk-tap-events-vs-touch-events/

This will, in practice, stop the touch from propagating down through the other objects “below” your top-most object. Let me know if I’ve misunderstood your question.

Hi Alex@Panc,

Thanks for the links it’s solve my problem. you have fully understood my question :slight_smile:

Have a good day

Topic solved.