touch event is triggered false...

local terrain\_texture = display.newImage( "world\_media/media\_terrain.jpg" ) terrain\_texture:toBack() terrain\_texture:addEventListener( "touch", function( event ) ... end )

But the function is called even if I hit other buttons.

It only should called if I touch the terrain_texture not the other buttons…

In the touch listeners for your other buttons, do you return true at the end?  If you don’t, then the touch will “bleed” through to objects below it, like your background texture.

  • Andrew

Will try it tomorrow. So it is possible to return true… never knew that^^

Anyway thank you for your answer.

Just as an FYI… I would not recommend using anonymous functions like this.  The way :removeEventListener works, you have to pass in the same function handle.  When you addEventListener like this, you never get the address of the function…

Now being a touch listener, you are not likely going to need to remove it, but if you do need to, you cannot using functions inline like that.

so???

any ideas???

addEventHandler( "touch", terrain\_texture, function( event ) setElementTarget( actor, event.x, event.y ) setElementFiring( actor, 1 ) end ) button\_fw:addEventListener( "touch", function( event ) if ( event.phase == "began" ) then moving = setTimer( function() setElementPosition( actor, actor.x, actor.y + 2.5 ) end, 1, 0 ) else killTimer( moving ) end end )

f47x6ijb.jpg

hmm, you got all the information you need in the comments above.

  1. i am with Rob here, having those anonymous functions looks like playing with fire you cant extinguish.
    and it makes your code a bit harder to read (for me at least, sorry).

  2. if you dont want touch events to go through objects (means, if there are multiple touch objects overlapping but you just want the one on top to fire the event) you need to make it return true at the end. cant see you implementing that in your code after aukStudios pointed that out already.
     
    [lua]

button_fw:addEventListener( “touch”,

function( event )

if ( event.phase == “began” ) then

    moving = setTimer( function() setElementPosition( actor, actor.x, actor.y + 2.5 ) end, 1, 0 )

else

    killTimer( moving )

end

    return true --here we go

end )

[/lua]

 

hmm I’ve tried

return true

but it’s rotating and shooting like before…

CORRECTION: works fine my wrong:D thanks!

OTHER:

How can i prevent button functions to work if the player slide their finger off the buttons.

if i swipe my finger off the button while touch the player moves on constantly…

[lua]

local function stopWhateverThisButtonMakesYouDoing()

    print(“no more pew pew pew”)

end

local function buttontouch(event)

    if ( event.phase == “began” )then

        display.getCurrentStage():setFocus( event.target )

        event.target.isFocus = true

        

        event.target.movedOutside   = false

        event.target.boundLeft      = event.target.x - (event.target.width*0.5)

        event.target.boundRight     = event.target.x + (event.target.width*0.5)

        event.target.boundTop       = event.target.y - (event.target.height*0.5)

        event.target.boundBottom    = event.target.y + (event.target.height*0.5)

    end

    if ( event.phase == “moved” and not event.target.movedOutside)then

    

        if(event.x >= event.target.boundRight or event.x <= event.target.boundLeft or event.y <= event.target.boundTop or event.y >= event.target.boundBottom)then 

            print(“moved outside”)

            display.getCurrentStage():setFocus( nil ) – clear touch focus

            event.target.focus = false

            event.target.movedOutside   = true

            

            stopWhateverThisButtonMakesYouDoing()

            --event.phase = “cancelled” 

        end

    end

    if ( event.phase == “ended”  and not event.target.movedOutside)then

        

            print(“ended”)

            display.getCurrentStage():setFocus( nil ) – clear touch focus

            event.target.focus = false

            

            stopWhateverThisButtonMakesYouDoing() 

    end

end

button_fw:addEventListener( “touch”, buttontouch)

[/lua]

i always was asking myself if there isnt any easier solution than this workaround, so if anyone knows it please reply.

by the way, it would be way easier if you would assign the same touchlistener to all your objects,
then you could just check if the target from the “began” part is still the same with target from the “moved” part, and just trigger stopWhateverThisButtonMakesYouDoing() if not (of course you should remove the set focus part then).
but if you want to go along with your anonymous assigned listeners this wont be a choice and you decided on your own to make stuff painfully complicated.

cheerio,
fox
 

lad this is more than a workaround.

doesn’t corona api has an easier solution to prevent buttons to activate when the finger is moved off the button-image.

we should create anonymous functions which make it easier to work with. especially for newbies and beginners.

like a function library -> see mta:sa functions

http://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDAQFjAA&url=http%3A%2F%2Fwiki.multitheftauto.com%2F&ei=lzDJUdvxCKmJ0AWZ0oCQDg&usg=AFQjCNHw8dE6MFHMwgx477a8JOyrY2CQRg&sig2=wldWRakZAv-DMbuo_1Cy_w&bvm=bv.48293060,d.d2k

It’s not hard to do that.  In your touch listener, you have to use the object:setFocus() method (http://docs.coronalabs.com/api/type/StageObject/setFocus.html) to set the touch’s focus onto the object, which makes that touch not report events to any other objects.

But even easier would be to just create your buttons using the widget API (http://docs.coronalabs.com/api/library/widget/newButton.html), which does all of this automatically for you.

  • Andrew

whats wrong? and any correction to make it better?

local table = { { fw, 1.1, 4.6 }, { bw, 1.1, 2.6 }, { lw, 1.2, 3.4 }, { rw, 1.02, 3.4 } } for i, v in ipairs( table ) do v[1] = createImage( "root\_media/media\_button.png", display.contentWidth - ( display.contentWidth / v[2] ), display.contentHeight - ( display.contentHeight / v[3] ) ) addEventHandler( "touch", v[1], movePlayer ) end function movePlayer( event ) if ( event.phase == "began" ) then if ( event.target == fw ) then moving = setTimer( function() setElementPosition( player, player.x, player.y + 2.5 ) end, 1, 0 ) elseif ( event.target == bw ) then moving = setTimer( function() setElementPosition( player, player.x, player.y - 2.5 ) end, 1, 0 ) elseif ( event.target == lw ) then moving = setTimer( function() setElementPosition( player, player.x + 2.5, player.y ) end, 1, 0 ) elseif ( event.target == rw ) then moving = setTimer( function() setElementPosition( player, player.x - 2.5, player.y ) end, 1, 0 ) end display.getCurrentStage():setFocus( event.target ) event.target.alpha = 0.5 elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) event.target.alpha = 1 killTimer( moving ) end return true end

Can you post your code for your addEventListener() function

Other general observations:

local _ table _ = { { fw, 1.1, 4.6 }, { bw, 1.1, 2.6 }, { lw, 1.2, 3.4 }, { rw, 1.02, 3.4 } }

You will not be able to use any of the table.* API calls in this module after this definition.  You should avoid common names that our’s and Lua’s API’s use.

if ( event.target == _ fw _ )

What are fw, bw, lw, and rw?  Are they globals? Where are they defined?

You also asked “What’s wrong?”  We don’t know what is wrong?  Why is what you are doing a problem?

The buttons gets created but the addEventHandler won’t be attached to.

so i can’t use them to move arround.

the buttons which will be created in the loop should be for example fw = createImage and so on;)

Can you post the code for your createImage and your addEventListener functions please?

function createImage( path, px, py, align ) if ( path and px and py ) then element = display.newImage( path, px, py ) if ( align == "center" ) then element:setReferencePoint( display.CenterReferencePoint ) element.x = display.contentCenterX element.y = display.contentCenterY end return element end end function addEventHandler( event, element, code ) if ( event and element and code ) then element:addEventListener( event, code ) end end

I think the point is, that the eventhandler doesn’t get the v[1] which means the element like “fw” which is used in function

Should:  v[1]   be   v[i]?  (that is v [eye] not v [one])

In the touch listeners for your other buttons, do you return true at the end?  If you don’t, then the touch will “bleed” through to objects below it, like your background texture.

  • Andrew

Will try it tomorrow. So it is possible to return true… never knew that^^

Anyway thank you for your answer.