2016-05-18 161 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