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 :
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) ?
(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.
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 !
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill