1
\$\begingroup\$

I've created a few TextButtons for a menu that render on top of another stage area, however the textbuttons that i've created for menu aren't clickable or don't provide any events. The MenuComponent class I created is being instantiated/rendered in another class.

The buttons show up, just unable to click anything or get any events from the buttons. Can someone take a look at the code I provided and maybe show me mistakes or something I forgot to implement to make them work?

MenuComponent Class:

   @Override
    public void create() {
        batch = new SpriteBatch();
        this.stage = new Stage();
        setRenderer(new ShapeRenderer());
        Gdx.input.setInputProcessor(stage);
        menuUp = new Texture("assets/ui/disengage.png");
        menuDown = new Texture("assets/ui/disengagePressed.png");
        
        
        Table menuWindow = new Table();
        this.menuWindow = menuWindow;
        menuWindow.setPosition(675,160);
        //Button View
        SpriteDrawable upFormat=new SpriteDrawable(new Sprite(createRoundPixMap(Color.BLACK,100, 30,10)));
        SpriteDrawable downFormat=new SpriteDrawable(new Sprite(createRoundPixMap(Color.RED,100, 30,10)));
        SpriteDrawable checkedFormat=new SpriteDrawable(new Sprite(createRoundPixMap(Color.BLACK,100, 30,10)));
        TextButtonStyle buttonStyle=new TextButtonStyle(upFormat, downFormat, checkedFormat, new BitmapFont());
        //Individual Buttons
        lobbyButton = new TextButton("Back to Lobby",buttonStyle);
        mapsButton = new TextButton("List Maps",buttonStyle);
        menuWindow.add(lobbyButton).pad(3,0,3,0).row();
        menuWindow.add(mapsButton).pad(3,0,3,0).row();
        menuWindow.setVisible(false);
        stage.addActor(menuWindow);
        stage.getRoot().setPosition(65,150);
    }
    
    @Override
    public void update() {
    }

    @Override
    public void render() {
        batch.begin();
        batch.draw((menuButtonIsDown)?menuDown:menuUp, MENU_buttonX, MENU_buttonY);
        
        batch.end();
        stage.act();
        stage.draw();
    }

Part of the class its being rendered/created in:

    @Override
    public void create() {
        generator = new FreeTypeFontGenerator(Gdx.files.internal("assets/font/Roboto-Regular.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.size = 10;
        parameter.spaceX = 0;
        parameter.shadowColor = new Color(0, 0, 0, 0.5f);
        parameter.borderColor = Color.BLACK;
        parameter.borderWidth = 1;
        parameter.borderStraight = true;
        parameter.shadowOffsetY = 1;
        parameter.shadowOffsetX = 1;
        font = generator.generateFont(parameter);

        renderer = new ShapeRenderer();
        this.batch = new SpriteBatch();
        sea = context.getManager().get(context.getAssetObject().sea,Texture.class);
        sea.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - 200);
        this.mainmenu = new MenuComponent(context, this);
        mainmenu.create();
    }
   
    @Override
    public void render() {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        Gdx.gl.glViewport(0,200, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - 200);

        mainmenu.render();
    }
\$\endgroup\$
2
  • \$\begingroup\$ What does MenuComponent inherit from? \$\endgroup\$ – bornander Aug 12 '20 at 4:58
  • \$\begingroup\$ @bornander It extends another scene component class which inherits from a custom gamescene class that has its own functions. Also implements inputprocessor \$\endgroup\$ – dree Aug 12 '20 at 13:38
0
\$\begingroup\$

Instead of having two stages, just have one stage, and have multiple tables.

That way you can set any actors in the stage (tables, buttons, labels) that are in front of your text buttons to be untouchable. Example:

Label lBasketEndlessMode = new Label("(Endless Mode)", AssetLoader.defaultSkinSmall);
lBasketEndlessMode.setPosition(0, 0);
lBasketEndlessMode.setTouchable(Touchable.disabled);        
stage.addActor(lBasketEndlessMode);

This allows you to display anything you want, and still have anything behind it be able to take input.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.