onRelease not called on button widget if user drags finger off

hi everyone(again)

I have a few widget buttons that have both onPress and On Release functions.  When they press it, it adds 10 to a variable, but when they release it it takes 10 away.  I have a problem though…When i was trying it out, if i i clicked on one of them, the score would go up, but when i drug my mouse away from it, it would de-select, but the onRelease function (subtract 10) wouldn’t get called…Anyway to fix this???

thanks as always

Why are you using both functions?

its part of the game :ph34r: …

does it make a difference?

You can get better control if you use the onEvent function instead.  Then you can specify in each event phase (“began”, “moved”, “ended”) what you need to do exactly.

i need to run a function when the button is pressed and a different function if it gets released

here is an example:

[lua]

local function onbtwPress()

    

    score = score+10

    --DIFFERENT CODE HERE INSTEAD OF SCORE

    score1.text = score

    

    return true    – indicates successful touch

end

local function onbtwRelease()

    

    score = score-10

    --DIFFERENT CODE HERE INSTEAD OF SCORE

    score1.text = score

    

    return true    – indicates successful touch

end

btw = widget.newButton{

        label=btnTxt,

        labelColor = { default={255}, over={128} },

        fontSize = btnFont,

        defaultFile=“btw_btn.png”,

        overFile=“btw_btn-over.png”,

        width=btnH, height=btnW,

        onPress = onbtwPress,

        onRelease = onbtwRelease    – event listener function

    }

    btw:setReferencePoint( display.CenterReferencePoint )

    btw.x = display.contentWidth*0.15

    btw.y = 350

[/lua]

Yes I know, that’s why I suggested to use the onEvent function.  I suggest you read into what event phases are.

so I would replace onPress = onbtwPress and onbtwRelease = onbtwRelease with onEvent = onbtwEvent()???

then under function onbtwEvent() say [lua]

if event.phase == “began” then

    

score = score+10

–DIFFERENT CODE HERE INSTEAD OF SCORE

score1.text = score

 

elseif event.phase == “ended” then

   score = score-10

–DIFFERENT CODE HERE INSTEAD OF SCORE

score1.text = score

 

end

 

[/lua]

 

Is this correct?

Yes, except it should be onEvent = onbtwEvent

Then you can also add:

if event.phase == "began" then score = score+10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score elseif event.phase == "moved" then return true -- this prevents the ended phase from firing elseif event.phase == "ended" then score = score-10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score end

it still doesn’t trigger the negative function if i drag my mouse off the button while it is clicked.  still functions the same as before.

Oh I misread your first post.  Try this instead:

if event.phase == "began" then score = score+10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score elseif event.phase == "ended" or event.phase == "cancelled" then score = score-10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score end

thanks

im going to take a guess that there isn’t a way to disable the touch tracking if it gets too far away from the button or not on the button?

or perhaps there is a way to tell if the button is on the over file or not?

Why are you using both functions?

its part of the game :ph34r: …

does it make a difference?

You can get better control if you use the onEvent function instead.  Then you can specify in each event phase (“began”, “moved”, “ended”) what you need to do exactly.

i need to run a function when the button is pressed and a different function if it gets released

here is an example:

[lua]

local function onbtwPress()

    

    score = score+10

    --DIFFERENT CODE HERE INSTEAD OF SCORE

    score1.text = score

    

    return true    – indicates successful touch

end

local function onbtwRelease()

    

    score = score-10

    --DIFFERENT CODE HERE INSTEAD OF SCORE

    score1.text = score

    

    return true    – indicates successful touch

end

btw = widget.newButton{

        label=btnTxt,

        labelColor = { default={255}, over={128} },

        fontSize = btnFont,

        defaultFile=“btw_btn.png”,

        overFile=“btw_btn-over.png”,

        width=btnH, height=btnW,

        onPress = onbtwPress,

        onRelease = onbtwRelease    – event listener function

    }

    btw:setReferencePoint( display.CenterReferencePoint )

    btw.x = display.contentWidth*0.15

    btw.y = 350

[/lua]

Yes I know, that’s why I suggested to use the onEvent function.  I suggest you read into what event phases are.

so I would replace onPress = onbtwPress and onbtwRelease = onbtwRelease with onEvent = onbtwEvent()???

then under function onbtwEvent() say [lua]

if event.phase == “began” then

    

score = score+10

–DIFFERENT CODE HERE INSTEAD OF SCORE

score1.text = score

 

elseif event.phase == “ended” then

   score = score-10

–DIFFERENT CODE HERE INSTEAD OF SCORE

score1.text = score

 

end

 

[/lua]

 

Is this correct?

Yes, except it should be onEvent = onbtwEvent

Then you can also add:

if event.phase == "began" then score = score+10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score elseif event.phase == "moved" then return true -- this prevents the ended phase from firing elseif event.phase == "ended" then score = score-10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score end

it still doesn’t trigger the negative function if i drag my mouse off the button while it is clicked.  still functions the same as before.

Oh I misread your first post.  Try this instead:

if event.phase == "began" then score = score+10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score elseif event.phase == "ended" or event.phase == "cancelled" then score = score-10 --DIFFERENT CODE HERE INSTEAD OF SCORE score1.text = score end