I have finally fixed my problem by writing:
module MyGame
def self.game
return Java::MyNamespace::Game::getInstance
end
end
$game = MyGame::game
Question
I'm writing a game engine with JRuby, and something goes wrong when I use a global variable. I have only these scripts:
main.rb:
$CLASSPATH << "src.rb"
require 'modules'
require 'SceneMenu'
require 'SceneMap'
$game.setScene(SceneMenu.new)
modules.rb:
$game = Java::MyNamespace::Game::getInstance
module MyGame
def self.cache
return $game.cache # << ERROR OCCURS HERE
end
end
SceneMenu.rb:
class SceneMenu
def initialize
@count = 0
end
def update
if @count == 100
$game.setScene(SceneMap.new)
end
@count += 1
end
end
SceneMap.rb:
class SceneMap
def initialize
@logoTexture = MyGame::cache.load("mylogo.png")
end
end
My problem is that when I launch the game, it always is fine, but when my @count
reaches 100, and SceneMap
is created, an error occurs saying:
undefined method 'cache' for nil:NilClass
while I have called $game.setScene( ... )
just before.
I do not modify my $game
variable at all, so I don't know what happens.
Does someone have an idea of what is going on?
Solution 2
I have finally fixed my problem by writing:
module MyGame
def self.game
return Java::MyNamespace::Game::getInstance
end
end
$game = MyGame::game
OTHER TIPS
Maybe try changing the code to:
$game = Java::MyNamespace::Game::getInstance
module MyGame
def cache
return $game.cache
end
end