0
votes

[SOLVED] Thanks for looking, but I figured it out. I needed to un-nest the return true statements in some of my if conditions.

I have just started learning Lua this week, and I have started to program a 2D side-scroller game using Corona SDK. In this game, the player's character is moved by pressing buttons displayed on the screen much like a virtual game pad. These buttons work perfectly, however, the problem is that I have a

Runtime:addEventListener("tap", onScreenTap)

event listener that then calls the shoot() function to fire a projectile from the player whenever a tap is registered. This results in a projectile firing every time I lift the touch from one of the movement buttons.

Is there any way I can stop the shoot function from calling when I finish touching one of the movement keys? I have tried

display.getCurrentStage:setFocus()

and also putting

return true 

at the end of the movement functions but nothing seems to work.

1

1 Answers

2
votes

You can use this basics in every touch function you have.. Or just this for all touch events. Combining touch events in a single function may solve your problem.

function inBounds( event )
    local bounds = event.target.contentBounds
    if event.x > bounds.xMin and event.x < bounds.xMax and event.y > bounds.yMin and event.y  < bounds.yMax then
        return true
    end
    return false
end 


function touchHandler( event )
    if event.phase == "began" then
        -- Do stuff here --
        display.getCurrentStage():setFocus(event.target)
        event.target.isFocus=true
    elseif event.target.isFocus == true then
        if event.phase == "moved" then
            if inBounds( event ) then
                -- Do stuff here --
            else
                -- Do stuff here --
                display.getCurrentStage():setFocus(nil)
                event.target.isFocus=false
            end
        elseif event.phase == "ended" then
            -- Do stuff here --
            display.getCurrentStage():setFocus(nil)
            event.target.isFocus=false
        end
    end
    return true
end

By the way, if you try to use this in Runtime, it will throw and error. You can add an event listener to background or just put some control mechanisnms like

if event.target then
-- blah blah
end