2013-04-11 60 views
1

我目前正在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; 
     } 

}); 
} 

回答

0

不要有太多的想法有關scene2d在libgdx但是從你的代碼可能是,如果()方法寫在顯示的代碼render()方法,繪製背景後,那麼它可能工作。我得到的是你正在繪製表格和按鈕,然後在它上面繪製背景,所以所有的東西都被你的背景隱藏起來。 還有一件事Show方法將被調用一次(第一次創建方法你的屏幕類),並且渲染方法將在你的課程生命中多次調用。我深深地閱讀了你的代碼,但是你有沒有在任何地方調用stage.draw()或stage.act()?

+0

好的,在我的代碼中轉身後,我意識到我之前使用的代碼(將背景添加爲演員)其okey,但我忘記調用我的超類show()。 – 2013-04-12 18:13:23