【问题标题】:Can anyone till me how to make a button In libgdx谁能告诉我如何在 libgdx 中制作一个按钮
【发布时间】:2017-10-27 03:15:26
【问题描述】:

嗨,谁能告诉我如何在 Libgdx 中制作一个可点击的按钮 对于一个只要你触摸它就会做某事的移动设备

【问题讨论】:

  • 不,真的不能说是
  • For a mobile device that does something if you touch it ?
  • 如果你触摸一个纹理/Sprite 它应该是什么东西
  • 所以你希望有人提供代码?

标签: button mobile libgdx touch


【解决方案1】:

这是您尝试使用 cmets 实现的目标的最小示例

package com.mygdx.gtest;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

public class Test extends ApplicationAdapter{


    private Skin skin;
    private TextButton button;
    private Stage stage;

    @Override
    public void create() {
        //make a stage for your button to go on
        stage = new Stage();

        //load a skin(a collection of styles for objects)
        // skin is from gdx-skins (https://github.com/czyzby/gdx-skins)
        skin = new Skin(Gdx.files.internal("neon-ui.json"));

        //create your button
        button = new TextButton("Button1", skin);

        //add it to your stage
        stage.addActor(button);

        // add a listener to your buttons so it does something when clicked
        button.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                System.out.println("I was clicked");
            }
        });

        // set the sgae as the input processor so it will respond to clicks etc
        Gdx.input.setInputProcessor(stage);

    }

    @Override
    public void render() {
        //clear the screen
        Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // tell stage to do action and then draw itself
        stage.draw();
        stage.act();
    }
}

【讨论】:

  • 这看起来不错,但我现在没时间做,我会尽快通知您
  • 不,不要做我想做的事
  • 有什么不好的?它在我的点击时打印出“我被点击”效果很好
【解决方案2】:

这是在不添加任何外部皮肤的情况下添加按钮的方法

TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = yourCustomFont;
textButtonStyle.fontColor = Color.WHITE;
stage.add(new TextButton("Custom Btn ", textButtonStyle));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2018-07-24
    • 2012-11-14
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多