Based on the Space Shooter demo app by Belén Albeza González, I've been using a Singleton instance of Gosu::Window. However, I ran into and issue when trying to access the attributes of the window, specifically mouse_x and mouse_y. I can't seem to find a way to access those instance variables while using the Singleton mixin. Has anyone else run into this problem?
I've not run into any problems with singleton classes. and just to test the mouse_x and mouse_y coords
class Game < Gosu::Window
super(800, 600, false)
@test_mouse = TestMouse.new
@colour = Gosu::red
mouse_x = Game.instance.mouse_x
mouse_y = Game.instance.mouse_y
Game.instance.draw_quad(mouse_x, mouse_y, @colour,
mouse_x + 2, mouse_y, @colour,
mouse_x, mouse_y + 2, @colour,
mouse_x + 2, mouse_y + 2, @colour, 1)
this works for me
Thanks for the help. I tried game.instance but my game locked up so I assumed that was not the correct syntax. Thanks again.
I think Ruby's singleton doesn't solve circular dependencies. I used it in one game and had the lockup too. My Window called SomeGameObject.new in its constructor, which in turn called Image.new(MyWindow.instance, ...), and so it ended up being an endless loop. Kind of sad that the singleton lib didn't even check for this very common problem :(
My workaround was to just create the first "state" after the Window was constructed (and accessible via MyWindow.instance). The future, supercool workaround will of course happen in Gosu itself, once Image.new doesn't need the Window arg anymore. The next release might already be very close to that.
nothing gets past you jlnr
Thanks for the help. I haven't had a chance to get back into the code today, but I'll get back on it tomorrow and try to access the mouse position.
Somehow I missed that operation lambda was open source, or I would have been looking at it for tips :)
Alright, just jumped into the code for a minute, turns out I was calling Game.instance inside a constructor which was getting called inside the constructor for the play state, which was getting initialized by my Gosu::Window. So, my solution was as simple as initializing my variables to zero, then setting them based on the mouse position in the update loop. Hopefully I won't make that mistake again.
Thanks for the quick help!
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill