更改对话框时,Java gdxlib会闪烁

时间:2017-06-12 12:42:09

标签: java android libgdx rendering flicker

您好我在libGdx中出现闪烁纹理问题。我到处寻找,找不到解决办法。

当我使用show(舞台舞台)方法更改对话框时,纹理只会闪烁。基本上有2个渲染调用。一个来自我绘制纹理的渲染,第二个是Stage类中的draw方法。

当我改变对话框时,第一个不应该闪烁。这非常烦人,我不知道如何解决这个问题。

应用类:

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";                                      
}                                                                                         

我实现Dialog的HUD类。

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 );                                                                
    }                                                                                     
}                                                                                         

1 个答案:

答案 0 :(得分:0)

对于闭包,解决方案只是在HUD类中创建一个新的SpriteBatch,如下所示:

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

闪烁根本没有发生。 wiki解释说" SpriteBatch是一个非常重的对象,所以你应该只在你的程序中有一个。"所以不建议这样做。