So what I want to do is inject the instance of A
created in MyGame
into PlayScreen
. Here is my code so far:
class MyGame extends Game {
public A a;
public void create() {
a = new A();
Injector injector = Guice.createInjector(new GameModule(this));
setScreen(new PlayScreen());
}
}
public class GameModule extends AbstractModule {
MyGame game;
public GameModule(MyGame game){
this.game = game;
}
@Override protected void configure() {}
@Provides
@Singleton
A getA() {
return game.a;
}
}
public class PlayScreen extends Screen {
@Inject A a;
public void render() {
// Using a
}
}
But in the method render()
from PlayScreen
, batch comes out as null.
However, if in MyGame
I use injector.getInstance(A.class)
everything works, I do not get null
.
What am I doing wrong?