Troubles with button touches

Howdy,
I’m trying to have 2 move-character buttons next to each other for a side-scroller. I had button widgets working perfectly so that if I started a new tap on it it would run and if I moved off of the button it’d stop, and if (in the same touch event) the touch slid back on it’d reactivate. That was great but I was missing the ability to slide from one button onto the other and have it activate the other button. 
My first question is, is there some sort of slick way to do this(slide off of one button on the same touch event and have it activate the other button)? 

If the first isn’t possible, I’ve also integrated a method that creates my own button with a spritesheet. That is fine and dandy using button:addEventListener(“touch”,button) and a touch function in button; the other button will activate when slid onto. However, I can’t seem to get a moved event once the touch drags off of the button like I could with the button widget, and hence can’t cancel the stuff happening when I slide off of the buttons.
I realize that the event is local to the button and tried doing a touch event at the runtime level, but I can’t get anything to work short of hard-coding in every single coordinate set which doesn’t really work in my implementation. Is there a slick way for a Runtime touch event to realize what object it is currently in?

I’d prefer a solution to the first scenario because the implementation is much easier with that.
Also, I would post the code itself but it’s incredibly modularized and… sprawling  :wacko:. It would only give you a headache to sort through it hehe.

Thanks

It would definitely assist if you could just post the button functionality code so that we could parse that and help with your implementation. Suffice it to say, the above is very possible. You might want to take look at this template; maybe you can convert some of the code:

http://thatssopanda.com/corona-sdk-tutorials/moving-a-character-left-and-right-with-corona-sdk/

I doctored the code up and commented it some more for your sanity :stuck_out_tongue:

[lua]

–set up the button in a backwards kinda way, but it works nonetheless

    rightBtnImage = display.newImage(“move_right_btn.png”,0,0)

    rightBtnImage.isVisible=false

    rightBtn=widget.newButton({

        left=leftBtn.width,

        top=screenY-rightBtnImage.contentHeight,

        defaultFile=“move_right_btn.png”,

        overFile=“move_right_btn_ACTIVE.png”,

        onEvent=rightBtnEvent

    })

    screenGroup:insert(rightBtn)

    rightBtnImage = nil

–called on button event

function rightBtnEvent(event)

–if touch ends, is cancelled, or a move event is outside the button…   

    if (event.phase==“ended” or event.phase==“cancelled”) or (event.phase==“moved” and ((math.abs(event.x-rightBtn.x)>rightBtn.contentWidth/2) or (math.abs(event.y-rightBtn.y)>163/2))) then

        --stop using enterframe to move soldier

            soldier.enterFrame=nil

    

–if the touch event is “moved” and is WITHIN the button; used to reactivate if

–a held touch leaves then comes back

    elseif event.phase==“moved” and ((math.abs(event.x-rightBtn.x)<rightBtn.contentWidth/2) or (math.abs(event.y-rightBtn.y)<rightBtn.contentHeight/2)) then

        

        soldier.enterFrame=fullMoveRight

        if math.abs(speedX)<20 then  --if the soldier is not moving, essentially, restart animate; prevents glitchy animation

            soldier:setSequence(“run-fwd-right”)

            soldier:play()

        end

        

– if the touch is BRAND NEW, activate the move right function

    elseif event.phase==“began” then

        activateMoveRight() --function call sets move animation, sets up some physics and camera movement, etc

        jumpBtn:shift(“right”) --moves a jump button to float above currently held direction button

    end

end

[/lua]

that was the button widget one I was using. And I do have the runtime event listener for the enterframe set up.
The issue is, described in simpler terms, that I need a button widget to activate when a touch is slid onto it so I can slide off of one directional button onto the other next to it to change directions.
Let me know if you need further clarification on anything.

Well, off the bat I’d say that using widgets for this functionality isn’t going to get you the results you want. I say that because they aren’t really meant for on-screen controls, but more for interactive, descriptive buttons for “stationary apps”, or non-action games. You can definitely try to get them to work, but I’ve long since given up on widget buttons as being useful in controlling characters.

If I were you, I’d suggest modifying the above to something more vanilla regarding on-screen buttons and adding your own phase listeners. I keep a quick button template for when I want to throw together a proof-of-concept for a game concept. This normally includes a right and left button, a jump button and an “action” button. The right and left buttons are coded such that, if you slide your finger onto one, it fires the movement, and if you slide your finger outside of a specified range, it fires the end of the movement. Anyone following my posts (not that I think those people exist) will know i pimp out the DMC touch library found in the code exchange like it’s going out of style. It’s fantastic for on-screen control multi-touch. 

On screen buttons pale in comparison to a pad, and Corona’s out-of-the-box multi-touch functionality isn’t ideal, so me just say that I can’t find anyone (except for the Ravenous Games folks) who have done it well.

However, you can try setting variables that are listened for when the buttons are touched and are reset to the default function when the action stops firing; you can try using a counter with a number that changes depending on the touch count and button and have a Runtime function that fires actions based on the number; you can try one of the examples from the DMC library (I don’t want to get into sprite controls because you don’t seem to require it, but that adds another layer of complexity to the code)

Let me know how you get on and if you need any more help.

That’s what I figured about the buttons… I was just wondering if there was some hidden slick little trick I could use to make that work because the buttons are just simpler  :P 

As for the usability of the actual controls, yes, controls almost always suck! That’s why I’m coding the game to use the accelerometer but an option to switch to a controller. It’s also going to have a customizable UI and such, so that everyone is pleased and nobody can complain about the controls being annoying  ^_^ 

I have also created a similar writeup that uses spritesheet display objects for the same purpose and uses event phases and made it my own custom button, but it has a whole different set of issues.

I’m going to go play in my code jungle for a little while longer and if I can’t figure it out I’ll post the source back here and see what can be done. There must be sometime simple my Java-addled mind is missing here!

Thanks for your input mate

It would definitely assist if you could just post the button functionality code so that we could parse that and help with your implementation. Suffice it to say, the above is very possible. You might want to take look at this template; maybe you can convert some of the code:

http://thatssopanda.com/corona-sdk-tutorials/moving-a-character-left-and-right-with-corona-sdk/

I doctored the code up and commented it some more for your sanity :stuck_out_tongue:

[lua]

–set up the button in a backwards kinda way, but it works nonetheless

    rightBtnImage = display.newImage(“move_right_btn.png”,0,0)

    rightBtnImage.isVisible=false

    rightBtn=widget.newButton({

        left=leftBtn.width,

        top=screenY-rightBtnImage.contentHeight,

        defaultFile=“move_right_btn.png”,

        overFile=“move_right_btn_ACTIVE.png”,

        onEvent=rightBtnEvent

    })

    screenGroup:insert(rightBtn)

    rightBtnImage = nil

–called on button event

function rightBtnEvent(event)

–if touch ends, is cancelled, or a move event is outside the button…   

    if (event.phase==“ended” or event.phase==“cancelled”) or (event.phase==“moved” and ((math.abs(event.x-rightBtn.x)>rightBtn.contentWidth/2) or (math.abs(event.y-rightBtn.y)>163/2))) then

        --stop using enterframe to move soldier

            soldier.enterFrame=nil

    

–if the touch event is “moved” and is WITHIN the button; used to reactivate if

–a held touch leaves then comes back

    elseif event.phase==“moved” and ((math.abs(event.x-rightBtn.x)<rightBtn.contentWidth/2) or (math.abs(event.y-rightBtn.y)<rightBtn.contentHeight/2)) then

        

        soldier.enterFrame=fullMoveRight

        if math.abs(speedX)<20 then  --if the soldier is not moving, essentially, restart animate; prevents glitchy animation

            soldier:setSequence(“run-fwd-right”)

            soldier:play()

        end

        

– if the touch is BRAND NEW, activate the move right function

    elseif event.phase==“began” then

        activateMoveRight() --function call sets move animation, sets up some physics and camera movement, etc

        jumpBtn:shift(“right”) --moves a jump button to float above currently held direction button

    end

end

[/lua]

that was the button widget one I was using. And I do have the runtime event listener for the enterframe set up.
The issue is, described in simpler terms, that I need a button widget to activate when a touch is slid onto it so I can slide off of one directional button onto the other next to it to change directions.
Let me know if you need further clarification on anything.

Well, off the bat I’d say that using widgets for this functionality isn’t going to get you the results you want. I say that because they aren’t really meant for on-screen controls, but more for interactive, descriptive buttons for “stationary apps”, or non-action games. You can definitely try to get them to work, but I’ve long since given up on widget buttons as being useful in controlling characters.

If I were you, I’d suggest modifying the above to something more vanilla regarding on-screen buttons and adding your own phase listeners. I keep a quick button template for when I want to throw together a proof-of-concept for a game concept. This normally includes a right and left button, a jump button and an “action” button. The right and left buttons are coded such that, if you slide your finger onto one, it fires the movement, and if you slide your finger outside of a specified range, it fires the end of the movement. Anyone following my posts (not that I think those people exist) will know i pimp out the DMC touch library found in the code exchange like it’s going out of style. It’s fantastic for on-screen control multi-touch. 

On screen buttons pale in comparison to a pad, and Corona’s out-of-the-box multi-touch functionality isn’t ideal, so me just say that I can’t find anyone (except for the Ravenous Games folks) who have done it well.

However, you can try setting variables that are listened for when the buttons are touched and are reset to the default function when the action stops firing; you can try using a counter with a number that changes depending on the touch count and button and have a Runtime function that fires actions based on the number; you can try one of the examples from the DMC library (I don’t want to get into sprite controls because you don’t seem to require it, but that adds another layer of complexity to the code)

Let me know how you get on and if you need any more help.

That’s what I figured about the buttons… I was just wondering if there was some hidden slick little trick I could use to make that work because the buttons are just simpler  :P 

As for the usability of the actual controls, yes, controls almost always suck! That’s why I’m coding the game to use the accelerometer but an option to switch to a controller. It’s also going to have a customizable UI and such, so that everyone is pleased and nobody can complain about the controls being annoying  ^_^ 

I have also created a similar writeup that uses spritesheet display objects for the same purpose and uses event phases and made it my own custom button, but it has a whole different set of issues.

I’m going to go play in my code jungle for a little while longer and if I can’t figure it out I’ll post the source back here and see what can be done. There must be sometime simple my Java-addled mind is missing here!

Thanks for your input mate