Probably has something to do with the event stack.
I guess technically, since the first tap event is called, within that event call you are adding to the event stack of the same object. Maybe it looks like this:
rect:event:tap -> state1Cb (state1Cb adds the next event to the stack
rect:event:tap ->state2Cb (Corona clears the prev event, but notices this one too, so calls it)
rect:event:tap ->state1Cb (Corona clears the prev event, but notices this one too, so calls it)
rect:event:tap ->state2Cb (Corona clears the prev event, but notices this one too, so calls it)
rect:event:tap ->state1Cb (Corona clears the prev event, but notices this one too, so calls it)
Looks like an endless cycle.
You might just change the one function to point to alternate to another function:
local function\_stack = {}
function\_stack[1] = state1Cb
function\_stack[2] = state2Cb
-- create a large button
local rect = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
rect:setFillColor( 128, 64, 64 )
-- state1
function state1Cb( evt )
print("state1")
end
-- state2
function state2Cb( evt )
print("state2")
end
function rect\_tap\_handler(event)
function\_stack[event.target.state](event)
if (rect.state == 1) then
event.target.state= 2
else
event.target.state= 1
end
end
-- start
rect.state = 1
rect:addEventListener( "tap", rect\_tap\_handler)
Essentially you have one addEventListener call, and within that one, it calls the appropriate function based on the current state.
I am with you though, it seems odd that Corona would not know that the event added does not correspond with the current event it is firing.
One last thought that might work, dunno though, is to add return true to the touch event handlers that you already have:
-- create a large button
local rect = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
rect:setFillColor( 128, 64, 64 )
-- state1
function state1Cb( evt )
print("state1")
rect:removeEventListener( "tap", state1Cb )
rect:addEventListener( "tap", state2Cb )
return true
end
-- state2
function state2Cb( evt )
print("state2")
rect:removeEventListener( "tap", state2Cb )
rect:addEventListener( "tap", state1Cb )
return true
end
-- start
rect:addEventListener( "tap", state1Cb )
return true tells Corona to stop going down the objects that are visible looking for another object that might have a touch event… it stops its search right there at that object. [import]uid: 8541 topic_id: 2762 reply_id: 8418[/import]