Libgdx:将2个演员放在另一个演员身上

时间:2014-11-30 17:44:10

标签: java libgdx scene2d

我使用libgdx的scene2d内置UI库在我的游戏中制作UI。我有兴趣如何在一张桌子上画一张又一张的2张图片(或演员)?我在Android中寻找类似LayerDrawable的类似内容。有什么东西可以制作吗?

2 个答案:

答案 0 :(得分:5)

libGDX UI库中有一个Stack小部件用于这些目的。

更新:如果您想将一个小部件放在另一个小部件之上并且这些小部件具有不同的大小,那么您应该包装较小的小部件。类似的东西:

//First add actual content
stack.add(content);

//Second add wrapped overlay object
Table overlay = new Table();
overlay.add(overlayWidget).expand().fillX().bottom().left();
stack.add(overlay);

stack变量是一个Stack实例

这是我项目的实际摘录。你应该根据你的情况调整它。

答案 1 :(得分:0)

你可以使用这样的东西:

    //create stage
    stage = new Stage(new StretchViewport(game.width, game.height));

    //create table
    Table table = new Table();
    table.setSize(game.width,game.height); //fullscreen

    //add Image 1
    table.add(new Image(game.skin.getDrawable("image1")));
    //add Image 2 (create new col)
    table.add(new Image(game.skin.getDrawable("image2"))).width(xx).height(xx);

    //add new row
    table.row();

    //add Image 3 - padding to draw over image1/2 && colspan
    table.add(new Image(game.skin.getDrawable("image3"))).width(xx).height(xx).padTop(-50f).colspan(2);

    //add table to stage
    stage.addActor(table);

    /*
     * Add action
     *
     */

    @Override
    public void render(float delta) {
        ...
        //don't forget to draw ur stage
        stage.act(delta); // or stage.act(Gdx.graphics.getDeltaTime())
        stage.draw();
    }

您还可以为舞台/桌面或图像等添加动作......:

   stage.addAction(Actions.sequence(
                           Actions.fadeOut(0.0001f),
                           Actions.fadeIn(0.5f),
                           Actions.delay(0.1f), 
                           Actions.run(new Runnable() {
                               @Override
                               public void run() {
                                   //mmmm new screen 
                                   dispose();
                                   game.setScreen(new BoobsScreen(game));
                               }
                           })));

当然,您可以绘制多个图像:按钮,文本按钮,标签等,或者为表添加scrollPane,但在查看Skin on libgdx之前 例如,以编程方式的九个文本按钮:

   private NinePatch processNinePatchFile(String fname) {
        final Texture t = new Texture(Gdx.files.internal(fname));
        final int width = t.getWidth() - 2;
        final int height = t.getHeight() - 2;
        return new NinePatch(new TextureRegion(t, 1, 1, width, height), 3, 3, 3, 3);
    }

    //create skin
    skin = new Skin();
    ...
    //create font
    ...
    //create ninepatch button from android/assets/style/button
    NinePatch ninepatch = processNinePatchFile("style/button.9.png");
    skin.add("button.9", ninepatch);

    //create textbuttonstyle
    TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = skin.getFont("font_black_38");
    textButtonStyle.fontColor = Color.DARK_GRAY;
    textButtonStyle.up = skin.getDrawable("button.9");
    skin.add("buttonTextStyle", textButtonStyle);


    //now you can create textbutton
    TextButton btn = new TextButton("Hello", game.skin.get("buttonTextStyle", TextButton.TextButtonStyle.class));

    //add a clicklistener
    btn.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            //mmmmmm add action for exemple
            stage.addAction(Actions.sequence(Actions.fadeOut(0.5f), Actions.run(new Runnable() {
                @Override
                public void run() {
                    //dispose and load screen
                    dispose();
                    game.setScreen(new MoreBoobsScreen(game));
                }
            })));
        }
    });
    table.add(btn);

要创建位图,请查看Gdx freetype,它非常易于使用。