【问题标题】:How to add background image to my menu?如何将背景图片添加到我的菜单?
【发布时间】:2013-04-11 08:37:08
【问题描述】:

我目前正在 libgdx 中制作某种带有背景图像的菜单。我尝试将图像添加为演员,但按钮没有响应 touchDown 事件。所以我现在尝试在 render() 方法中使用 spriteBatch 绘制背景图像,然后在 show() 方法中绘制表格和按钮,但现在它只显示图像,根本没有按钮。这是代码:

   public void show()
{

    // load the texture with our image
    backgroundTexture = new Texture("fonfopiz.png");

    // set the linear texture filter to improve the image stretching
    backgroundTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    // Here we create a region of our texture whose size is 512x301
    backgroundTextureRegion = new TextureRegion(backgroundTexture, 0, 0, 512, 301);
    // TODO Auto-generated constructor stub

    crearTablaBack();

    //the general table
    table = super.getTable();
    //table.setFillParent(false);
    Label vacia = new Label("",getSkin());
    table.add(vacia).spaceBottom(45);
    table.row();
    Label lTit;
    try {

        lTit = new Label(res.getString(titulo), getSkin(), "titulos");
        table.add(lTit).spaceBottom(10).spaceLeft(50);
        //stage.addActor(lTit);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    table.row();


  Label lText = new Label(res.getString(titulo + "_TXT"),getSkin());
  lText.setWrap(true);
   table.add(lText).width(375).spaceBottom(55);
   //lText.setAlignment(100);
   //lText.setWidth(200);
   table.row();


   table.setPosition(110, 0);
   //table.setSize(200, 200);


}

public void render(
        float delta )
    {
        super.render( delta );
        batch = super.getBatch();
        // we use the SpriteBatch to draw 2D textures (it is defined in our base
        // class: AbstractScreen)
        batch.begin();

        // we tell the batch to draw the region starting at (0,0) of the
        // lower-left corner with the size of the screen
        batch.draw( backgroundTextureRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );

        // the end method does the drawing
        batch.end();
    }


private void crearTablaBack() {
    //the table for the back button
    Table tabla = new Table( getSkin() );
    tabla.debug();
    //table.debugTable();

    stage.addActor( tabla );

    tabla.setPosition(40, 35);

     TextButton botonBack = new TextButton( "<<", getSkin() );
     tabla.add( botonBack ).size(60, 40).spaceBottom(20);
      tabla.row();
        botonBack.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int   pointer, int button) {
               //super.touchUp(event, x, y, pointer, button);
                System.out.println("back");
                //game.setScreen(new ExperimentosScreen(game));
                //game.setScreen(new PruebaScreen(game));
                return true;
        }

});
}

【问题讨论】:

    标签: java android user-interface libgdx


    【解决方案1】:

    对 libgdx 中的 scene2d 不太了解,但从您的代码来看,如果 show() 方法的代码是用 render() 方法编写的,在绘制背景之后,它可能会起作用。我得到的是你正在绘制表格和按钮,然后在它上面绘制背景,所以所有的东西都被你的背景隐藏了。 另一件事 Show 方法将被调用一次(第一次创建方法时您的屏幕类),并且在您的类的生命周期中将多次调用 render 方法。而且我还没有深入阅读您的代码,但是您是否在任何地方都调用了 stage.draw() 或 stage.act() ???

    【讨论】:

    • 好的,在我的代码中转了一圈之后,我意识到我之前使用的代码(添加背景作为演员)没问题,但是我忘了调用我的超类的 show()。
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多