Reputation: 413
I believe this question has been asked before, but it seems that I am a bit unlucky to find it. My question is how do I enabling a button/actor’s addListener
in middle of the process? Take for example, on assets loading screen in between activity screen (let assume that user finished Stage A, the loading screen is to load assets for Stage B), the continue button can only be enabled (displayed previously, but not enable) after the all assets is loaded. If I add the addListener in to the render()
section, it will create lots of anonymous inputListener as per this post. However, I don’t think it will work if I place it in show()
section. My question is where should I place this addListener
in order to make an actor touchable yet does not create those anonymous inputListener
? And what is the proper way to use it?
Upvotes: 1
Views: 3627
Reputation: 6221
I would recommend to create a ClickListener
instead of an InputListener
and add it as usual. Inside of the Listener you check if the loading is done. If it's done you do what ever you want to do. If not you return without any action.
To give you an example how to add an ClickListener
to a TextButton which should already do your task:
TextButtonStyle style = new TextButtonStyle();
style.font = new BitmapFont();
style.font.setColor(Color.WHITE);
continue= new TextButton("continue",
style);
continue.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
if(manager.update()){ //returns true if the AssetManager is done in case you use an AssetManager
basegameclass.setScreen("menu"); //sets the menu screen
}else{
//not done do nothing or do something else like showing loading not done
}
}
});
To add it to different actors or Buttons it should be simmelar. Just take care that the Stage where the Button is added also is the Inputprocessor
. So make sure you added it like this Gdx.input.setInputProcessor(stage);
In this case i don't think, that you need a whole InputListener
simply take the ClickListener for this small task. The InputListener
gives you a wider range of methods which you dont need i think. It is used to detect touchups
and touchdowns
and slide events and much more which you dont need for a button i think. Maybe for actors that you drag around.
you create this all inside of the constructor of the screen. Never do such things inside of the render method since it would create a new listener every frame. (60 per second!)
Upvotes: 3