I’m having an issue with event handlers. In the code below there are two event handlers, each triggered by a touch event on a graphic.
The problem is that whichever one I choose first is the only one that ever gets executed. If I click and hold on the object that triggers the “slider” event handler first, then release it, then try to click and hold on the other object, I’m still triggering the “slider” handler code. If I click and hold on the object that triggers the “doPlunger” handler code, then, no matter what I do, that code is always triggered.
The code is about as short as I can make it and be sure that everything that needs to be seen is.
Any advice would be greatly appreciated.
Sean.
[code]
local ui = require(“ui”)
local plunger = display.newImage(“plunger.png”)
plunger.x = 305
plunger.y = 444
local obs = display.newImage(“marble-blue.png”);
obs.x = display.contentWidth/2;
obs.y = 490
function slider(event)
local t = event.target
local phase = event.phase
print (“in slider”)
if “began” == phase then
– Make target the top-most object
– local parent = t.parent
– parent:insert( t )
display.getCurrentStage():setFocus( t )
– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move
t.x = event.x - t.x0
if(t.x < 0) then
t.x = 0;
end
if(t.x > 320) then
t.x = 320;
end
end
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end
function doPlunger(event)
local t = event.target
local phase = event.phase
print (“in doPlunger”)
if “began” == phase then
– Make target the top-most object
– local parent = t.parent
– parent:insert( t )
display.getCurrentStage():setFocus( t )
– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif “ended” == phase then
force = 10 * (t.y0 - t.y)
print (“force=”,force)
print (“t.y=”, t.y)
elseif t.isFocus then
if “moved” == phase then
– Make object move
t.y = event.y - t.y0
if(t.y > 497) then
t.y = 497;
end
if(t.y < 450) then
t.y = 444;
end
end
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end
display.setStatusBar( display.HiddenStatusBar )
obs:addEventListener(“touch”, slider)
plunger:addEventListener(“touch”, doPlunger)
physics.addBody( obs, “static”, { density = 1.0, friction = 0.3, bounce = 1.3, radius = 45 } )
physics.addBody( plunger, “kinematic”, { density = 1.0, friction = 0.3, bounce = 2 } )
[/code] [import]uid: 4993 topic_id: 7202 reply_id: 307202[/import]