Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Error - Can getTarget() from "click" but event.x returns nil. Any idea? — Gideros Forum

Error - Can getTarget() from "click" but event.x returns nil. Any idea?

MellsMells Guru
edited July 2012 in General questions
Hi community,

My code is - I believe - really basic and I don't see where the error comes from.
I have tried to isolate the part that raises an issue. Error could be located somewhere else but I have no idea where it could be :
function Interface:init()
        -- FB
	local fbButton
	fbButton = Button.new(Bitmap.new(...))	
	fbButton:addEventListener("click", self.onFbClick, self)
	stage:addChild(fbButton)
        fbButton.debugName = "bob"
end
and
function Interface:onFbClick(event)
	local target = event:getTarget()
        print (target.debugName)  -- prints "bob" correctly
        print (event.x, event.y)      -- prints "nil, nil"
 
	if target:hitTestPoint(event.x, event.y) then	-- And then here the error happens -> bad argument #1 to 'hitTestPoint' (number expected, got nil)	
		event:stopPropagation()					
		print ("Fb sharing")
	end
end
I don't understand why I can get the target from the event, but not event.x and event.y.

Has it ever happened to you and how did you solve it?
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • talistalis Guru
    edited July 2012
    Hi Mells,

    You want to get x,y coordinate of the place on the fbButton?
    event:getTarget() return the button, not the listener function.
  • atilimatilim Maintainer
    While Button class dispatches "click" event, it doesn't set event.x and event.y fields. And Button class already does the hitTestPoint check and stopPropagation. I think your function should be just:
    function Interface:onFbClick(event)
    	print ("Fb sharing")
    end

  • MellsMells Guru
    edited July 2012
    Thank you all :)
    About the Button class -> Wow, I love it when Gideros already does the work for me (I should have checked the code more).

    My mistakes :
    1. I assumed that *all* click events come with event.x and event.y fields set
    2. Lack of understanding of what the Button class is doing under the hood
    So here is what I had to do :
    function Interface:init()
            -- FB
    	local fbButton
    	fbButton = Button.new(...)	
    	stage:addChild(fbButton)
    	fbButton:addEventListener("click", self.onFbClick)
    end
     
    function Interface:onFbClick(event)	
    		print ("Fb sharing")
    end
    Thank you community.

    Likes: atilim

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.