I think there's a problem in the event handler code

I have some code where I set up  touch event handler for a sprite

and

a tap handler for a few different images.

My game plays in rounds.  When a time limit expires the round is over.  In order to start they next round you touch the sprite.

At this point my images are visible too but when I touch the image it is going to the sprite handler. 

I only add the events once in the beginning of the code so how is it possible my tap handler for my image is going to the touch handler of my sprite?

I should have added this is somewhat random.  It doesn’t happen all of the time.

It’s really hard to answer your question without seeing some code.

But I guess I can provide some general info:

  • if you register a tap and a touch event handler on a display object, tapping it will trigger both the tap and the touch event handler (the latter will be triggered several times)

  • if you want to prevent a touch/tap event handler from propagating to objects below the one that was touched/tapped, your event handler must return true

Hello _memo

Thanks for that.  I was missing return true in some of my event handlers, but adding that in didn’t make a difference.  After about the third round the image handler event triggers the sprite event. 

I didn’t add any code because there is a lot of it.  The entire program is 2059 lines long.  Let me try the event handler for one image object and the event handler for the sprite object.  Maybe someone will see something I missed:

SPRITE OBJECT EVENT HANDLER:


–  When the flame  is touched  run this code.

play_flame = function(event) 

if play_flame_in_status_flag == false then

    if event.phase == “began” then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
  sfAnimation:play()
       markX = sfAnimation.x    – store x location of object
       markY = sfAnimation.y    – store y location of object
  if newRound == true then
   transition.to( stoneBench, {time=1000, alpha=0}) 
   transition.to( scoreTextLine1, {time=1000, alpha=0}) 
   transition.to( scoreTextLine2, {time=1000, alpha=0}) 
   transition.to( round_ending_score, {time=1000, alpha=0}) 
   transition.to( scoreTextLine3, {time=1000, alpha=0}) 
   transition.to( scoreTextLine4, {time=1000, alpha=0}) 
   transition.to( scoreTextLine5, {time=1000, alpha=0}) 
   transition.to( endStone, {time=1000, alpha=0}) 
   newRound = false
   startTimer()
   scoreNum = 0
   gamescoreText.text = string.format("%-15u", scoreNum)
  end
 
  if isphysics_started == false and event.target == sfAnimation then
   transition.to( fireText, {time=1000, alpha=0}) 
   physics.start()
   isphysics_started = true
  end

    elseif event.target.isFocus then
        if event.phase == “moved” then
         local x = (event.x - event.xStart) + markX
         local y = (event.y - event.yStart) + markY
         sfAnimation.x, sfAnimation.y = x, y   – move object based on calculations above

    elseif event.phase == “ended” or event.phase == “cancelled” then
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
   sfAnimation.myName = “flame”
   sfAnimation.x = firestand.x + 10
   sfAnimation.y = flame_origin
        end
    end
end

return true

end

IMAGE OBJECT EVENT HANDLER



–  Execute the code for a flameblast power card selection


flameBlast = function(event)

print(“physics state”)
print(isphysics_started)

if isphysics_started == true then
 audio.play(flameBlastExplosion)
 blast_stones()
 
 for i = 1, maxSlots do
  if  slot_table[i].powercard == event.target then
   slot_table[i].number_of_cards= slot_table[i].number_of_cards - 1
   number_of_cards = slot_table[i].number_of_cards
   current_number_of_cards_text = slot_table[i].textadd
   current_number_of_cards_text.text = number_of_cards
   if slot_table[i].number_of_cards == 0 then
    slot_table[i].slotFilled = false
    slot_table[i].powercard.alpha = 0
    slot_table[i].powercard.x = centerx
    slot_table[i].powercard.y = centery
    slot_table[i].textadd.alpha = 0
   end
   break  
  end
 end
end

return true
 
end

Thanks for looking!

Hm… unfortunately, I can’t reach any conclusion from looking at the event handlers - how does the code look where you actually register the event handlers? Could you provide a screenshot that shows how the involved display objects are placed related to each other?

I should have added this is somewhat random.  It doesn’t happen all of the time.

It’s really hard to answer your question without seeing some code.

But I guess I can provide some general info:

  • if you register a tap and a touch event handler on a display object, tapping it will trigger both the tap and the touch event handler (the latter will be triggered several times)

  • if you want to prevent a touch/tap event handler from propagating to objects below the one that was touched/tapped, your event handler must return true

Hello _memo

Thanks for that.  I was missing return true in some of my event handlers, but adding that in didn’t make a difference.  After about the third round the image handler event triggers the sprite event. 

I didn’t add any code because there is a lot of it.  The entire program is 2059 lines long.  Let me try the event handler for one image object and the event handler for the sprite object.  Maybe someone will see something I missed:

SPRITE OBJECT EVENT HANDLER:


–  When the flame  is touched  run this code.

play_flame = function(event) 

if play_flame_in_status_flag == false then

    if event.phase == “began” then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
  sfAnimation:play()
       markX = sfAnimation.x    – store x location of object
       markY = sfAnimation.y    – store y location of object
  if newRound == true then
   transition.to( stoneBench, {time=1000, alpha=0}) 
   transition.to( scoreTextLine1, {time=1000, alpha=0}) 
   transition.to( scoreTextLine2, {time=1000, alpha=0}) 
   transition.to( round_ending_score, {time=1000, alpha=0}) 
   transition.to( scoreTextLine3, {time=1000, alpha=0}) 
   transition.to( scoreTextLine4, {time=1000, alpha=0}) 
   transition.to( scoreTextLine5, {time=1000, alpha=0}) 
   transition.to( endStone, {time=1000, alpha=0}) 
   newRound = false
   startTimer()
   scoreNum = 0
   gamescoreText.text = string.format("%-15u", scoreNum)
  end
 
  if isphysics_started == false and event.target == sfAnimation then
   transition.to( fireText, {time=1000, alpha=0}) 
   physics.start()
   isphysics_started = true
  end

    elseif event.target.isFocus then
        if event.phase == “moved” then
         local x = (event.x - event.xStart) + markX
         local y = (event.y - event.yStart) + markY
         sfAnimation.x, sfAnimation.y = x, y   – move object based on calculations above

    elseif event.phase == “ended” or event.phase == “cancelled” then
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
   sfAnimation.myName = “flame”
   sfAnimation.x = firestand.x + 10
   sfAnimation.y = flame_origin
        end
    end
end

return true

end

IMAGE OBJECT EVENT HANDLER



–  Execute the code for a flameblast power card selection


flameBlast = function(event)

print(“physics state”)
print(isphysics_started)

if isphysics_started == true then
 audio.play(flameBlastExplosion)
 blast_stones()
 
 for i = 1, maxSlots do
  if  slot_table[i].powercard == event.target then
   slot_table[i].number_of_cards= slot_table[i].number_of_cards - 1
   number_of_cards = slot_table[i].number_of_cards
   current_number_of_cards_text = slot_table[i].textadd
   current_number_of_cards_text.text = number_of_cards
   if slot_table[i].number_of_cards == 0 then
    slot_table[i].slotFilled = false
    slot_table[i].powercard.alpha = 0
    slot_table[i].powercard.x = centerx
    slot_table[i].powercard.y = centery
    slot_table[i].textadd.alpha = 0
   end
   break  
  end
 end
end

return true
 
end

Thanks for looking!

Hm… unfortunately, I can’t reach any conclusion from looking at the event handlers - how does the code look where you actually register the event handlers? Could you provide a screenshot that shows how the involved display objects are placed related to each other?