2017-06-12 62 views
0

您好,我在libGdx中有閃爍紋理的問題。我到處尋找,我找不到解決方案。更改對話框時,java gdxlib閃爍

當im使用show(Stage stage)方法改變對話框時,紋理只會閃爍。基本上有兩個渲染調用來自繪製紋理的渲染,第二個渲染調用是Stage類中的繪製方法。當我改變對話框時,第一個肩膀閃爍。這非常煩人,我不知道如何解決這個問題。

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.Dialog; 
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; 
import com.badlogic.gdx.scenes.scene2d.ui.Skin; 
import com.badlogic.gdx.scenes.scene2d.ui.Table; 
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; 
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle; 
import com.badlogic.gdx.scenes.scene2d.utils.Drawable; 
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; 
import com.badlogic.gdx.utils.Scaling; 
import com.badlogic.gdx.utils.viewport.ScalingViewport; 
import com.badlogic.gdx.utils.viewport.StretchViewport; 

public class FlickerGame implements ApplicationListener {         

    private SpriteBatch batch;                
    private StretchViewport viewport;              
    private HUD hud;                  
    private Texture texture;                
    private BitmapFont font;                

    @Override                    
    public void create() {                 

     // use only one SpriteBatch in game           
     batch = new SpriteBatch();               
     viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
     texture = new Texture(Gdx.files.local("badlogic.jpg"));      
     font = new BitmapFont();               
     hud = new HUD(batch, getSkin());             

     Gdx.input.setInputProcessor(hud);            
    }                      

    @Override                    
    public void dispose() {                

     batch.dispose();                 
     hud.dispose();                  
     texture.dispose();                 
     font.dispose();                 
    }                      

    @Override                    
    public void resize(int width, int height) {           

     viewport.update(width, height, true);           
     hud.getViewport().update(width, height, true);         
    }                      

    @Override                    
    public void render() {                 

     float delta = Gdx.graphics.getDeltaTime();           

     Gdx.gl.glClearColor(0, 0, 0, 1);             
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);          

     // draw the texture on the whole screen           
     batch.setProjectionMatrix(viewport.getCamera().combined);      
     batch.begin();                  
     batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
     batch.end();                  

     hud.act(delta);                 
     hud.draw();                  
    }                      

    @Override public void pause() {}              
    @Override public void resume() {}              

    private Skin getSkin(){                

     Skin skin = new Skin();               

     // just add a part of the badlogic.jpg as button looks awful its just for testing 
     Drawable up = new TextureRegionDrawable(new TextureRegion(texture, 0, 0, 50, 8)); 

     skin.add(DEFAULT, new WindowStyle(font, Color.WHITE, null), WindowStyle.class); 
     skin.add(DEFAULT, new LabelStyle(font, Color.WHITE), LabelStyle.class);  
     skin.add(DEFAULT, new TextButtonStyle(up, null, null, font), TextButtonStyle.class); 

     return skin;                  
    }                      

    private static final String DEFAULT = "default";          
}                       

的HUD類,其中i實施的對話框:

App類。

class HUD extends Stage {                 

    private Dialog menu;                 
    private Dialog about;                 
    private Dialog exit;                 

    public HUD(SpriteBatch batch, Skin skin){           

     super(new ScalingViewport(Scaling.stretch, 0.3f * Gdx.graphics.getWidth(), 0.3f * Gdx.graphics.getHeight(), new OrthographicCamera()), batch); 

     final Stage stage = this;               

     menu = new Dialog("menu", skin){             

      @Override                  
      public void result(Object object){           

       if (object instanceof Integer){           
        switch ((Integer) object){           
         case 0:               
          // play button doesn't do anything at the moment    
          Gdx.app.log("Button", "play");        
          break;               
         case 1: about.show(stage); break;        
         case 2: exit.show(stage); break;         
        }                  
       }                   
      }                    
     };                     
     Table menuButtons = menu.getButtonTable();           
     menu.button("play", 0);               
     menuButtons.row();                 
     menu.button("credits", 1);              
     menuButtons.row();                 
     menu.button("exit", 2);               

     about = new Dialog("credits", skin){            

      @Override                  
      public void result(Object object){           

       menu.show(stage);              
      }                    
     };                     
     about.text("made by me!");              
     about.button("back");               

     exit = new Dialog("", skin){              

      @Override                  
      public void result(Object object){           

       if (object instanceof Boolean){           
        if ((Boolean) object){            
         Gdx.app.exit();             
        } else {                
         menu.show(stage);            
        }                  
       }                   
      }                    
     };                     
     exit.text("Are you sure you want to exit?");          
     exit.button("yes", true);              
     exit.button("no", false);              

     menu.show(this);                 
    }                      
}                       
+0

使用斷點來查看您在哪裏調用渲染/顯示方法並實際繪製圖像。 – Nathan

+0

你能詳細說說你的意思嗎?它是帶有導致閃爍的stage.getActionsRequestRendering()和Gdx.graphics.requestRendering()的addAction方法。 – Tejay

+0

什麼是這條線'final stage = this;'?你永遠不會使用它。 **編輯**:你的代碼有明顯的編譯缺陷,比如缺少括號。你能仔細檢查你沒有粘貼錯誤嗎? – Nathan

回答

0

對於封閉的解決方案只是建立在HUD類這樣的新SpriteBatch:

super(new ScalingViewport(
    Scaling.stretch, 
    0.3f * Gdx.graphics.getWidth(), 
    0.3f * Gdx.graphics.getHeight(), 
    new OrthographicCamera()), 
    new SpriteBatch() 
); 

的閃爍根本不發生。維基解釋說:「SpriteBatch是一個非常重的對象,所以你應該只在你的程序中有一個。」所以這不被推薦。