GuiComponent and ImageComponents not interacting with "onClicked" mouse events #655
Replies: 4 comments
-
This post has been migrated automatically from the old LITIENGINE forum (2018 - 2022). |
Beta Was this translation helpful? Give feedback.
-
This post has been migrated automatically from the old LITIENGINE forum (2018 - 2022). |
Beta Was this translation helpful? Give feedback.
-
This post has been migrated automatically from the old LITIENGINE forum (2018 - 2022). |
Beta Was this translation helpful? Give feedback.
-
This post has been migrated automatically from the old LITIENGINE forum (2018 - 2022). |
Beta Was this translation helpful? Give feedback.
-
This post has been migrated automatically from the old LITIENGINE forum (2018 - 2022).
Posted: 2019-03-22 17:41:53
User: CalvinMT [] (age: 1451 days 🔥; posts: 114)
Hi!
I'm running into a problem where some text "ImageComponents" won't react with the mouse event "onClicked()". They don't even activate when the mouse hovers on top of them.
I'm trying to do some kind of pause menu that floats on top of the game screen.
Basically, the InGameScreen class creates a new instance of PauseMenu which extends GuiComponent and is then added to the InGameScreen components.
The PauseMenu class creates new ImageComponent instances which are all added to its own components. Most of those newly created ImageComponents have "onClicked()" events.
Everythings in place. When I press Escape, the menu pops-up, but nothings interacting with the mouse.
Here's parts of the code:
InGameScreen:
public class InGameScreen extends Screen { private PauseMenu pauseMenu; /*[...]*/ @Override public void render (final Graphics2D graphics2D) { /*[...]*/ if (gameState == GameState.RUNNING) { Game.window().getRenderComponent().setCursor(imageCursorInvisible, 0, 0); this.pauseMenu.setVisible(false); } else if (gameState == GameState.PAUSE) { Game.window().getRenderComponent().setCursor(imageCursorVisible, 0, 0); this.pauseMenu.setVisible(true); } } @Override protected void initializeComponents () { /*[...]*/ this.pauseMenu = PauseMenu.getInstance(0.0, 0.0, this.screenWidth, this.screenHeight); this.pauseMenu.setVisible(false); /*[...]*/ this.getComponents().add(this.pauseMenu); } }
PauseMenu:
public class PauseMenu extends GuiComponent { private static PauseMenu instance; private ImageComponent containerComponent; private ImageComponent worldInfoComponent; private ImageComponent levelInfoComponent; private ImageComponent resume; private ImageComponent guide; private ImageComponent settings; private ImageComponent menu; /*[...]*/ private PauseMenu(double x, double y, double width, double height) { super(x, y, width, height); } public static PauseMenu getInstance (double x, double y, double width, double height) { if (instance == null) { instance = new PauseMenu(x, y, width, height); } return instance; } @Override protected void initializeComponents () { /*[...]*/ this.containerComponent = new ImageComponent(this.containerX, this.containerY, this.containerWidth, this.containerHeight, this.containerImage, this.containerWidth, this.containerHeight)); this.worldInfoComponent = new ImageComponent(this.worldInfoX, this.worldInfoY, this.worldInfoWidth, this.worldInfoHeight, null, null, null); this.levelInfoComponent = new ImageComponent(this.levelInfoX, this.levelInfoY, this.levelInfoWidth, this.levelInfoHeight, null, null, null); this.resume = new ImageComponent(this.containerComponent.getX(), this.containerComponent.getY()+(this.containerComponent.getHeight()/2.0), this.containerComponent.getWidth(), this.textHeight, null, "Resume", null); this.guide = new ImageComponent(this.containerComponent.getX(), this.resume.getY()+this.textHeight, this.containerComponent.getWidth(), this.textHeight, null, "Guide", null); this.settings = new ImageComponent(this.containerComponent.getX(), this.guide.getY()+this.textHeight, this.containerComponent.getWidth(), this.textHeight, null, "Settings", null); this.menu = new ImageComponent(this.containerComponent.getX(), this.settings.getY()+this.textHeight, this.containerComponent.getWidth(), this.textHeight, null, "Back To Menu", null); this.initEvents(); this.getComponents().add(this.containerComponent); this.getComponents().add(this.levelInfoComponent); this.getComponents().add(this.worldInfoComponent); this.getComponents().add(this.resume); this.getComponents().add(this.guide); this.getComponents().add(this.settings); this.getComponents().add(this.menu); } private void initEvents () { this.resume.onClicked(e -> { gameState = GameState.RUNNING; }); this.guide.onClicked(e -> { gameState = GameState.GUIDE; }); this.settings.onClicked(e -> { gameState = GameState.SETTINGS; }); this.menu.onClicked(e -> { gameState = GameState.MENU; }); } }
I tried putting "this.pauseMenu.onClicked()" somewhere in the InGameScreen class, but still no response.
What I'd like to know is if what I wrote is correct and/or if I'm missing something?
Beta Was this translation helpful? Give feedback.
All reactions