2014-04-10 42 views
1

這是Java文件由v0.9.7如何正確使用libgdx中的設置?

sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2); 

爲什麼我們需要設置位置,負值爲中心的圖片GDX-設置-ui.jar產生的? libgdx圖片的參考基點在哪裏? (左下角?) 有人告訴我,libgdx的起源是左下角。鑑於上述數值,圖片的一部分應該已經在屏幕之外了....事實證明並非如此!我很困擾。 在此先感謝

完整列表:

package com.packtpub.libgdx.basic; 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 

public class Basic implements ApplicationListener { 
    private OrthographicCamera camera; 
    private SpriteBatch batch; 
    private Texture texture; 
    private Sprite sprite; 

    @Override 
    public void create() {  
     float w = Gdx.graphics.getWidth(); 
     float h = Gdx.graphics.getHeight(); 

     camera = new OrthographicCamera(1, h/w); 
     batch = new SpriteBatch(); 

     texture = new Texture(Gdx.files.internal("data/libgdx.png")); 
     texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 

     TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275); 

     sprite = new Sprite(region); 
     sprite.setSize(0.9f, 0.9f * sprite.getHeight()/sprite.getWidth()); 
     sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2); 
     sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     texture.dispose(); 
    } 

    @Override 
    public void render() {  
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     sprite.draw(batch); 
     batch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 
} 

回答

1

這是因爲對於正交相機的默認中心點(0,0,0),所以如果你只是在繪製圖像(0,0)其左下方的短號將位於屏幕的中央。

您可以通過使用cam.position.set改變這個中心點(W/2,H/2,0)方法

在這裏看到OrthographiCamera使用的更詳細的例子:https://github.com/libgdx/libgdx/wiki/Orthographic-camera

+0

明白了!非常感謝科林! :) – samwhs