2017-06-12 219 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是一个非常重的对象,所以你应该只在你的程序中有一个。”所以这不被推荐。