Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / using the mouse with gosu
- - By Daidalos Date 2011-04-03 19:29
Hi everybody !
I'm new in programming, and have decided to develop a little game with Ruby + Gosu (a bejeweled clone) on Windows 7 and I need use of the mouse.
I've already figured that the best way to use the mouse clicks in the game is in the update method, with a case statement.
The problem is when I want to set the reaction of clicking on a pawn here is my code :

case mouse_x
  when (5..517)
    case mouse_y
      when (5..517)
      if button_down?(Gosu::MsLeft)
        then self.selectpion((mouse_x-5)/64, (mouse_y-5)/64)
    #other instructions that works well

selectpion is a method that require the line and column index for a 2D array as arguments. But it seems that in this code, (mouse_x-5)/64 don't return an integer, but a range : (0..7) (and the same for mouse_y).
And it would be fastidious to detail the 64 cases for the selection of the pawns.
Can anyone tell me how to deal with it (or provide me a link for a code that would solve my problem) ?
Parent - - By jlnr (dev) Date 2011-04-04 00:57
(mouse_x-5)/64 should not be an integer, but a floating point number. You can use mouse_x.to_i to turn it into an Integer.

Also, usually you would detect mouse clicks in the window's "button_down(id)" method where id==MsLeft. What you are doing is a bit different because the check will run every frame, so if you hold the mouse while going over the window, it would keep selecting different units.
Parent - By Daidalos Date 2011-04-04 08:44
Ok, thank you, it seems that the to_i method was the solution.... but there should be other bugs in my code (in other methods), as it still don't work.

And thank you also to inderictly told me why the rest of the code didn't worked when I tried to pass it in the button_down(id) method => I hadn't realized that the "id==ButtonConstant" must be the first condition tested, then the case statement could be tested.

Thank you again, and 'til next time if ever I can't figure out by myself how to make my game work !
Up Topic Gosu / Gosu Exchange / using the mouse with gosu

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill