2016-05-18 110 views
0

我正在开发一个游戏,其中我正在制作一个屏幕游戏,但组件未正确重新排列。在某些屏幕上屏幕显示不正确Libgdx

什么我得到1024×720的屏幕是: enter image description here

,它应该是什么样子:

enter image description here

而且代码:

@Override 
public void show() { 
    stage = new Stage(new ScreenViewport()); 
    camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 
    Gdx.input.setInputProcessor(stage); 
    font = new BitmapFont(Gdx.files.internal("newfont.fnt")); 
    verysmallfont = new BitmapFont(Gdx.files.internal("verysmallfont.fnt")); 
    smallfont = new BitmapFont(Gdx.files.internal("smallfont.fnt")); 

    atlas = new TextureAtlas("ui/buttons.pack");//add atlas 

    skin = new Skin(atlas); 

    table = new Table(skin); 
    actiontable = new Table(skin); 
    actionbar = new Table(skin); 
    actionbar2 = new Table(skin); 

    table.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 

    actiontable.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 

    actionbar.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 

    actionbar2.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 


    TextButton.TextButtonStyle buttonback = new TextButton.TextButtonStyle(); 
    buttonback.up = skin.getDrawable("back"); 
    buttonback.pressedOffsetX = 1; 
    buttonback.pressedOffsetY = -1; 
    buttonback.font = font; 

    buttonBack = new TextButton("",buttonback); 

    TextButton.TextButtonStyle lifebuttonstyle = new TextButton.TextButtonStyle(); 
    lifebuttonstyle.up = skin.getDrawable("life"); 
    lifebuttonstyle.pressedOffsetX = 1; 
    lifebuttonstyle.pressedOffsetY = -1; 
    lifebuttonstyle.font = font; 

    buttonlife = new TextButton("",lifebuttonstyle); 

    TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); 
    textButtonStyle.up = skin.getDrawable("heart_game_continue"); 
    textButtonStyle.pressedOffsetX = 1; 
    textButtonStyle.pressedOffsetY = -1; 
    textButtonStyle.font = font; 

    buttonPlay = new TextButton("",textButtonStyle); 

    TextButton.TextButtonStyle adsfreeStyle = new TextButton.TextButtonStyle(); 
    adsfreeStyle.up = skin.getDrawable("Video"); 
    adsfreeStyle.pressedOffsetX = 1; 
    adsfreeStyle.pressedOffsetY = -1; 
    adsfreeStyle.font = font; 

    buttonVideo = new TextButton("",adsfreeStyle); 



    TextButton.TextButtonStyle sharebuttonStyle = new TextButton.TextButtonStyle(); 
    sharebuttonStyle.up = skin.getDrawable("Replay"); 
    sharebuttonStyle.pressedOffsetX = 1; 
    sharebuttonStyle.pressedOffsetY = -1; 
    sharebuttonStyle.font = font; 

    buttonReplay = new TextButton("",sharebuttonStyle); 



    Label.LabelStyle headingstyle = new Label.LabelStyle(font,Color.WHITE); 

    label = new Label("Game Over",headingstyle); 
    label.setFontScale(1.7f); 

    Label.LabelStyle contstyle = new Label.LabelStyle(smallfont,Color.WHITE); 
    cont = new Label("Continue?",contstyle); 
    cont.setFontScale(2f); 

    Label.LabelStyle replaystyle = new Label.LabelStyle(verysmallfont,Color.WHITE); 
    replay = new Label("Replay",replaystyle); 
    shortVideo = new Label("(Short Video)",replaystyle); 
    replay.setFontScale(2f); 
    shortVideo.setFontScale(2f); 


    table.align(Align.top); 
    table.padTop(197f); 
    table.add(label); 
    table.getCell(label).spaceBottom(150f); 
    table.row(); 
    table.add(cont); 
    table.getCell(cont).spaceBottom(80f); 
    table.row(); 
    table.add(buttonPlay).size(200f,200f); 


    actiontable.add(buttonVideo).size(200f,200f); 
    actiontable.add(buttonReplay).size(200f,200f); 
    actiontable.align(Align.bottom); 
    actiontable.getCell(buttonVideo).spaceBottom(20f).padRight(100f); 
    actiontable.getCell(buttonReplay).spaceBottom(20f).padLeft(100f); 
    actiontable.row(); 
    actiontable.add(shortVideo).padRight(100f); 
    actiontable.add(replay).padLeft(100f); 
    actiontable.padBottom(197f); 

    actiontable.setFillParent(true); 

    actionbar.align(Align.topLeft).setWidth(Gdx.graphics.getWidth()); 
    actionbar.add(buttonBack).align(Align.left).size(90f,90f); 

    actionbar2.align(Align.topRight); 
    actionbar2.add(buttonlife).align(Align.right).size(90f,90f); 

    actionbar.getCell(buttonBack).pad(43f); 
    actionbar2.getCell(buttonlife).align(Align.right).pad(43f); 

    stage.addActor(actionbar2); 
    stage.addActor(actionbar); 
    stage.addActor(table); 
    stage.addActor(actiontable); 

    buttonPlay.addListener(new ClickListener(){ 
     @Override 
     public void clicked(InputEvent event, float x, float y) { 
      ((Game) Gdx.app.getApplicationListener()).setScreen(new Main(level,a,start,sweep,collide,innerarcs,out,mid,arcsleft,left,pointerColor)); 
     }; 
    }); 

    buttonReplay.addListener(new ClickListener(){ 
     @Override 
     public void clicked(InputEvent event, float x, float y) { 
      int total = out+mid+innerarcs; 
      ((Game) Gdx.app.getApplicationListener()).setScreen(new Main(level,a,total,pointerColor)); 
     } 
    }); 

    buttonBack.addListener(new ClickListener(){ 
     @Override 
     public void clicked(InputEvent event, float x, float y) { 
      ((Game) Gdx.app.getApplicationListener()).setScreen(new Menu(level)); 
     } 
    }); 


} 
SpriteBatch batch; 
@Override 
public void render(float delta) { 
    Gdx.gl.glClearColor(0.184f,0.184f,0.184f,1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    stage.act(delta); 
    stage.draw(); 

} 
private OrthographicCamera camera; 

@Override 
public void resize(final int width, final int height) { 
    table.setTransform(true); 
    table.setSize(width,height); 
    actiontable.setTransform(true); 
    actiontable.setSize(width,height); 
    actionbar.setTransform(true); 
    actionbar.setSize(width,height); 

} 

我我是libgdx的新手请帮助我没有关于如何使用相机和视口的任何知识。

在此先感谢..

回答

0

€DIT:对不起我的坏 - 没有什么能像“stage.resize” 但我会让的答案,因为它会解决你的问题,但没有那么快如你所愿,但以更清晰的方式。

以下文字不是为了ANWSER NECESSARRY和仅仅是建议

(如果你按照下面的步骤,你会时间救自己在&头痛将来)

我建议视口,因为它们会消除很多缩放问题,并且有很多,您可以根据需要选择。 LibGdx Viewports

视口负责针对不同屏幕尺寸与不同的技术的帮助下(例如缩放比例,黑条) 另外一个视口是超级容易实现。

你会做以下(e.g FitViewport(延伸内容,如果需要的话)):

stage = new Stage(); 
OrthographicCamera cam = new OrthographicCamera(); 
cam.setToOrtho(false, 800, 600); 
stage.setViewport(new FitViewport(800, 600, cam)); 

TADA。你实现了一个视口。不那么难。 (但请确保调用stage.getViewport()。在调整大小更新(W,H)(W,H)的方法!! ,不要忘了IT !!)

的第二个建议我会做的是: 使用主表。 这样你就以下

Table myMothershipTable = new Table(); 
myMothershipTable.setFillParent(true); 
myMothershipTable.add(add); 
myMothershipTable.add(all); 
myMothershipTable.add(content); 
stage.addActor(myMothershipTable); 

现在你只有一个演员,这是表,你可以诅咒肯定的是,此表将填满整个屏幕。

您的屏幕是10999x3?是的。该表将为10999x3。

好。如果你已经重构你的整个的东西到一个表,我建议下一步:

stage.setDebugAll(true); //this will help you to see the actual buildup from your actors & elements 

因此,继承人的原因:

  1. 将视需要照顾你的舞台尺寸
  2. 单表格确保您的完整内容将在屏幕内==>现在您可以添加一个接一个的项目,并确保与..
  3. stage.setDebugAll(true)..您的内容被正确放置。

如果您遵循这些简单的步骤,您将为设计界面节省很多时间和头痛。

+0

感谢您的建议,我现在要实现它,但我不知道它是否会解决调整我的资产的问题,正如您在上面的图片中看到的那样,按钮大小不会受到影响......? – Ashwani

+0

我认为这将有助于编写干净的代码。 我想你的问题是,多种因素一起玩。但我需要说,我从来没有像你这样的问题。这就是为什么我喜欢我的方法。你唯一不应该忘记的是:*** stage.getViewport()。update(w,h)*** – Keey

+0

感谢您的指导.. :)我解决了它使用视口和相机..现在workin .. – Ashwani