当我启动应用程序时 - 一切都还可以,UI,播放器和磁贴都很合适。当我尝试恢复应用程序后,例如锁定和解锁屏幕时,会出现问题。
的UIButton
public class UiButton extends Sprite
{
private boolean touched = false;
public UiButton( Texture texture, float posX, float posY )
{
super( texture );
setPosition( posX, posY );
}
@Override public void setSize( float toWidth, float toHeight )
{
super.setSize( toWidth, toHeight );
setOriginCenter( );
}
public void setTouched( boolean touched ) { this.touched = touched; }
public boolean getTouched( ) { return touched; }
public boolean isTouched( float touchX, float touchY )
{
float xDiff = touchX - getX( ) - getWidth( ) / 2;
float yDiff = touchY - getY( ) - getHeight( ) / 2;
float distance = ( xDiff * xDiff ) + ( yDiff * yDiff );
if( distance <= Calculation.square( getWidth( ) / 2 ) )
return true;
return false;
}
}
GameScreen
public class GameScreen implements Screen
{
private final static int INITIAL_TILE_SIZE = 32;
// Display variables
private OrthographicCamera camera;
private SpriteBatch spriteBatch;
private SpriteBatch uiBatch;
private Player player;
// Map variables
private OrthogonalTiledMapRenderer mapRenderer;
private TiledMap tiledMap;
// User Interface variables
private UiButton arrow_right;
private UiButton arrow_left;
private UiButton arrow_down;
private UiButton arrow_up;
private UiButton action;
// FPS control variables
int sleep_time = 0;
int frames_skipped = 0;
@Override public void show( )
{
/** Setting InputProcessor **/
InitializeInputProcessor( );
/** Initializing Camera **/
camera = new OrthographicCamera( );
camera.setToOrtho( false );
/** Initializing spriteBatch **/
spriteBatch = new SpriteBatch( );
spriteBatch.setProjectionMatrix( camera.combined );
spriteBatch.maxSpritesInBatch = 5;
/** Initializing uiBatch **/
uiBatch = new SpriteBatch( );
uiBatch.maxSpritesInBatch = 5;
/** Creating map **/
InitializeMap( );
/** Creating Player **/
player = new Player( Gdx.graphics.getWidth( ) / 2, Gdx.graphics.getHeight( ) / 2 );
player.setSize( 32, 32 );
/** Creating UI **/
GenerateUI( Gdx.graphics.getHeight( ) / 60, Gdx.graphics.getHeight( ) / 5 );
}
public void doUpdating( )
{
/** Updating Player **/
player.update( ( TiledMapTileLayer ) tiledMap.getLayers( ).get( 1 ) );
/** Updating our Camera **/
camera.position.set( player.getX( ), player.getY( ), 0 );
camera.update( );
}
public void doRendering( )
{
/** Clearing the screen **/
Gdx.gl.glClearColor( 1, 1, 1, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
mapRenderer.setView( camera );
mapRenderer.render( );
/** spriteBatch rendering **/
spriteBatch.setProjectionMatrix( camera.combined );
spriteBatch.begin( );
player.draw( spriteBatch );
spriteBatch.end( );
/** uiBatch rendering **/
uiBatch.begin( );
arrow_right.draw( uiBatch );
arrow_left.draw( uiBatch );
arrow_down.draw( uiBatch );
arrow_up.draw( uiBatch );
action.draw( uiBatch );
uiBatch.end( );
}
@Override public void dispose( )
{
spriteBatch.dispose( );
uiBatch.dispose( );
tiledMap.dispose( );
mapRenderer.dispose( );
}
private void InitializeMap( )
{
tiledMap = new TmxMapLoader( ).load( "maps/test.tmx" );
mapRenderer = new OrthogonalTiledMapRenderer( tiledMap );
}
private void GenerateUI( float padding, float button_size )
{
arrow_right = new UiButton( new Texture( Gdx.files.internal( "ui/arrow_right_small.png" ) ), button_size * 1.4f + padding, button_size * 0.8f + padding );
arrow_right.setSize( button_size, button_size );
arrow_left = new UiButton( new Texture( Gdx.files.internal( "ui/arrow_left_small.png" ) ), padding, button_size * 0.8f + padding );
arrow_left.setSize( button_size, button_size );
arrow_up = new UiButton( new Texture( Gdx.files.internal( "ui/arrow_up_small.png" ) ), button_size * 0.8f, button_size * 1.6f + padding );
arrow_up.setSize( button_size, button_size );
arrow_down = new UiButton( new Texture( Gdx.files.internal( "ui/arrow_down_small.png" ) ), button_size * 0.8f, padding );
arrow_down.setSize( button_size, button_size );
action = new UiButton( new Texture( Gdx.files.internal( "ui/ic_target_small.png" ) ), Gdx.graphics.getWidth( ) - padding - button_size, padding + button_size );
action.setSize( button_size * 1.5f, button_size * 1.5f );
}
@Override public void render( float delta )
{
final int MAX_FRAME_SKIPS = 5;
final int DESIRED_FPS = 60;
final int FRAME_PERIOD = 1000 / DESIRED_FPS;
if( sleep_time >= 0 || frames_skipped == MAX_FRAME_SKIPS )
{
long start_time = System.currentTimeMillis( );
frames_skipped = 0;
doUpdating( );
doRendering( );
long time_diff = System.currentTimeMillis( ) - start_time;
sleep_time = ( int ) ( FRAME_PERIOD - time_diff );
if( sleep_time > 0 )
{
try { Thread.sleep( sleep_time ); }
catch( InterruptedException e )
{
System.out.println( "InterruptedException while trying to execute Thread.sleep( " + sleep_time + " )\n" + e.getMessage( ) );
}
}
}
else
{
doUpdating( );
sleep_time += FRAME_PERIOD;
frames_skipped++;
}
}
@Override public void resize( int width, int height )
{
camera.viewportHeight = 320;
camera.viewportWidth = Math.round( 320 * Gdx.graphics.getWidth( ) / Gdx.graphics.getHeight( ) );
}
@Override public void resume( ) { }
@Override public void pause( ) { }
@Override public void hide( ) { }
}