How would you create a game where you can drag an object and press a button at the same time? This is what my code looks like, but it does not work:
[code]
system.activate(“multitouch”)
local function onTouch( event )
local t = event.target
– Print info about the event. For actual production code, you should
– not call this function because it wastes CPU resources.
– printTouch(event)
local phase = event.phase
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.y0 = event.y - t.y
t.x0 = event.x - t.x
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.y = event.y - t.y0
t.x = event.x - t.x0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
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
object:addEventListener(“touch”,onTouch)
local button = display.newImage(“button.png”)
local buttonpress = function (event)
if event.phase == “began” then
–do stuff
end
end
button:addEventListener(“touch”,buttonpress)
[/code] [import]uid: 38001 topic_id: 12974 reply_id: 312974[/import]
[import]uid: 52491 topic_id: 12974 reply_id: 47956[/import]