Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
get mouse position (without eventlistener)? — Gideros Forum

get mouse position (without eventlistener)?

HolonistHolonist Member
edited February 2015 in General questions
Hi,

I'm writing a button class.

the standard procedure seems to be
function Button:init()
 	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
end
 
function Button:onMouseUp(e)
	if self:hitTestPoint(e.x, e.y) then
		--do something or dispatch a custom event
	end
end
Now, what I'd like to have is a function
Button:checkTouch()

I want to check if the button is touched on mouse_up, on mouse_over, on mouse_down and what not.
And I don't want to nest all of this in an event listener. I generally try to avoid them as much as possible (because they are too complicated to manage in most cases, or I'm simply too stupid to use them properly).


my checktouch would look like this, but afaik it's not done like this:
function Button:checkTouch()
	if self:hitTestPoint(mouse.x, mouse.y) then 
		return true
	else
		return false
	end
end
Do I really have to create an EventListener just to get the mouse position?

Thanks,
Holo

Comments

  • talistalis Guru
    edited February 2015
    Without the event listener your touch is not registered, and function not fired (checktouch function in your case) so it means that variables are not updated (x,y location of the mouse) .

    In every programming logic there are listeners, hooks . (onMouseMove, OnMouserelease vs vs..)

    So my answer : Yes, you need listener.

  • I find it irritating that I have to check if the mouse is being released in order to get its position.

    From other frameworks I know functions like mouse.getPosition(), for which you don't have to take 3 extra steps.

    Whatever, I'll see if I can wrap my head around that...
  • HolonistHolonist Member
    edited February 2015
    would this work?
    game.mouse ={["x"]=0,["y"]=0}
     
    stage:addEventListener(Event.MOUSE_MOVE, updateMouse, self) --not sure if this line is correct
     
    function updateMouse(e)
      game.mouse.x = e.x
      game.mouse.y = e.y
    end
     
     
    if button:checkTouch(game.mouse.x, game.mouse.y) == true then
      print("touched")
    end
  • talistalis Guru
    edited February 2015
    I didn't check the syntax hence i am on a computer where Gideros is not installed.

    But my logic says it will work .

    In other sdk's beleive me that listeners are also registered behind. I am sure that it is hard-coded into the framework itself. You can easilly do it in Gideros also. Just write the code in "init.lua" hence "init.lua" is fired always first by Gideros.

    Likes: Holonist

    +1 -1 (+1 / -0 )Share on Facebook
  • @Holonist sure your example should work
    And if framework provides such functionality, it means they are doing the same as your example internally.
    And they do it even if you don't need coordinates currently, what a waste of resources :)
  • @ar2rsawseen good point, I didn't realize that this is actually an improvement. No wonder Phaser is laggy as hell when it updates all kinds of unnecessary stuff all the time
  • why you need such functionality as in android or ios device there is no mouse so no default mouse position and this device gives you mouse position only after you touch the screen with your finger and hence no need to get mouse position by default right? the example link you provided is for html or browser in which there is use of mouse for some functionality like do something on hover but there is no need of such thing for mobile

    :)
Sign In or Register to comment.