Help, and I think it is easy, but after my stroke it is hard to read
basicly I need to drag an image that is on top of another, but when I hold the tab to drag it and the one under it drags it, but I need to top one only.
sorry if it sounds weird it is hard for what I am saying. I am a nerd and I cannot read, but strokes are strange, I can kinda work nerd
You need to add return true to the bottom of your touch event listener, like in these examples:
Adding return true makes it so that the touch event stops with the first object. Without it, the touch event will propagate to all overlapping objects.
When you touch the screen, Solar2D will check if there are any objects sitting directly under the touch.
It also checks whether any of those objects are listening for touch events (i.e. did any of them have a touch event listener added: myObject:addEventListener("touch", myTouchfunction)).
It then notifies all of the ‘touchable’ objects of the touch event, starting with the top object and working its way down to the bottom of the pile.
Adding return true to the end of a touch function is a way of telling Solar2D “if this object gets touched, it will handle the touch event - no need to check if other objects below are also listening for touch events”.
One more thing to keep in mind is that the touch event only gets applied to the object while you’re finger is directly on it, so it you start a touch on one object and move your finger very quickly onto another you may find that the “began” phase is triggered on the first object, but the “ended” phase is on the other. To prevent this from happening, you can ‘lock’ the touch onto the event so that the began, moved and ended phase all get passed to the same touch listener function even if you move away from the object.
You just need to remember to remove this lock when the touch ends. Example:
local myObject = display.newRect(0, 0, 50, 50)
function myObject:touch( event )
if ( event.phase == "began" ) then
-- This locks the touch onto the object
display.getCurrentStage():setFocus( self )
--this is a common shorthand way of keeping track of whether we have locked the touch to the object
self.isFocus = true
elseif ( self.isFocus ) then
if ( event.phase == "moved" ) then
--do your movement stuff here
elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
--Passing nil to this function unlocks the touch from the object, so that the next touch can be applied to any object
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
--this is the critical part to letting Solar2D know not to pass the touch event on to any more objects
return true
end
myObject:addEventListener( "touch", myObject )