Help, problem with drag!

Hi there,

I am looking to try and implement a drag functionality into my game and I am using the sample code from Corona.

However, i also have on screen buttons at the side that you can press while dragging. Now, upon testing this on the device, these work individually - that is to say, if i just drag (without pressing anything else) it works and if i just press the buttons (without dragging) they work. The problem is that the drag stops moving as soon as i press a button, as if the drag controls loses focus… (If i were to then re-drag the object again after pressing the button, it works). It seems like i can’t do both together at the same time.

Has anyone come across this problem before and know how do i tweak the sample code to fix this?

I’m guessing its not to do with: system.activate(“multitouch”) at the top of my file. As a random guess, is it to do with the focus elements within the drag event listener, maybe: display.getCurrentStage():setFocus(t) or display.getCurrentStage():setFocus(nil) ?

Can anyone help?

Thanks,

When you use multitouch you have to use setFocus a bit differently. For example you have to add a “touch ID” onto it as well. Heres a function i sometimes use for multitouch:

[lua]

local function btnTouched(event)

    local t = event.target

    local touchId = event.id

    if event.phase == “began” then 

        display.getCurrentStage():setFocus( t, touchId )

        t.isFocus = true

            

    elseif t.isFocus then 

        if event.phase == “ended”  then 

            display.getCurrentStage():setFocus( t, nil )

            t.isFocus = false

           --Do Stuff here

        end

    end

    return true

end

[/lua]

Hope that helps!

Hi there,

Thanks for the quick reply back! Let me try this and report back, so essentially i need to add an “id” to the drag event listener code?.. I’ll check this out.

Cheers,

Thanks TandG, using the Id worked!

No problem, glad it helped :slight_smile:

When you use multitouch you have to use setFocus a bit differently. For example you have to add a “touch ID” onto it as well. Heres a function i sometimes use for multitouch:

[lua]

local function btnTouched(event)

    local t = event.target

    local touchId = event.id

    if event.phase == “began” then 

        display.getCurrentStage():setFocus( t, touchId )

        t.isFocus = true

            

    elseif t.isFocus then 

        if event.phase == “ended”  then 

            display.getCurrentStage():setFocus( t, nil )

            t.isFocus = false

           --Do Stuff here

        end

    end

    return true

end

[/lua]

Hope that helps!

Hi there,

Thanks for the quick reply back! Let me try this and report back, so essentially i need to add an “id” to the drag event listener code?.. I’ll check this out.

Cheers,

Thanks TandG, using the Id worked!

No problem, glad it helped :slight_smile: