2014-02-15 186 views
1

我想用LibGDX繪製精靈很困難,但我失敗了。如果你需要更多的片段,來源如下:https://github.com/reisi007/Planes2DLibGDX/blob/master/Main/src/com/reisisoft/Planes2D/Planes2D.javaLibgdx未能畫出精靈

那麼,我在做什麼? 我正在加載一個PNG(1024x1024)並創建精靈,我把它放在一個地圖。這個地圖我把地圖再(後來不同的分辨率)

private void putTextures(Resolutions resolutions) { 
    Map<GObjects, Sprite> map; 
    switch (resolutions) { 
     case MidRes: 
      map = new EnumMap<GObjects, Sprite>(GObjects.class); 
      map.put(GObjects.PlaneR, new Sprite(txtMidRes, 0, 0, 600, 278)); 
      map.put(GObjects.PlaneG, new Sprite(txtMidRes, 0, 278, 600, 278)); 
      map.put(GObjects.PlaneB, new Sprite(txtMidRes, 0, 556, 600, 104)); 
      map.put(GObjects.Grass, new Sprite(txtMidRes, 0, 834, 600, 104)); 
      map.put(GObjects.Bullet, new Sprite(txtMidRes, 600, 834, 299, 138)); 
      all.put(Resolutions.MidRes, map); 
      break; 
    } 
} 

有一個功能我從當前分辨率的精靈:

private boolean setCurrentResolutions(Resolutions resolution) { 
    if (resolution == null) 
     return false; 
    currentResolution = resolution; 
    currentResolutionSprites = all.get(Resolutions.MidRes); 
    return true; 
} 

private Sprite requestSprite(GObjects gObject) { 
    return currentResolutionSprites.get(gObject); 
} 

我測試了WINOWS建設,偉馳大小800X480。 我想提請精靈的起源是:X 300.0Ÿ139.0 寬度300,高度是140。 接下來的渲染功能:

public void render() { 
    camera.update(); 
    spriteBatch.setProjectionMatrix(camera.combined); 
    Update(); 
    spriteBatch.begin(); 
    Draw(); 
    spriteBatch.end(); 
} 

,因爲這不是真正的幫助,這裏的Draw方法:

public void Draw() { 
    s.draw(spriteBatch); 
} 

我看到的只是黑色。 我試圖用OpenGL 1,1.1,2和各種解決方案,從StackOverflow的

最後的 「創造」 的方法:

// On creation 
public void create() { 
    curH = Gdx.graphics.getHeight(); 
    curW = Gdx.graphics.getWidth(); 
    camera = new OrthographicCamera(curW, curH); 
    spriteBatch = new SpriteBatch(); 
    // Load texture 
    txtMidRes = new Texture(Gdx.files.internal("planes.png")); 
    txtMidRes.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); 
    // Create Sprites for EVERY supported resolution 
    putTextures(Resolutions.MidRes); 
    // Set resolution according to H/W 
    setCurrentResolutions(Resolutions.MidRes); 
    s = requestSprite(GObjects.PlaneR); 
    s.setSize(300, 140); 
    s.setPosition(Gdx.graphics.getWidth()/2 - s.getWidth()/2, 
      Gdx.graphics.getHeight()/2 - s.getHeight()/2); 
} 

感謝您的幫助所有的

弗洛裏安

回答

0

首先,您提供給完整源的鏈接與那裏的片段不匹配。也許有一個被遺忘的推動?現在

對您的問題的一些想法:

  • 您沒有設置攝像頭的位置。嘗試:

    camera.position.set(curH/2f,curW/2f,0);

  • 您不需要手動構建您的精靈,libgdx提供了 功能來做到這一點。在官方文檔上查看Texture packer

  • 您還可以使用AssetManager自動處理分辨率。 有關更多信息,請參閱this post

+1

謝謝, 更新攝像頭位置幫助:) –