2017-03-08 37 views
0

,所以我有一個非常簡單的秒殺質感,我想重複在屏幕的底部多次,因此始終充滿屏幕的全尺寸,無論寬度。 當然,我現在已經瞭解了setWrap()函數,但我無法得到我期待的結果。libgdx - 簡單重複的紋理(setWrap())

This is the texture

And I'd like to get something like this

的更具體而言,我有給出的屏幕的寬度,並計算顯示爲屏幕高度的百分比紋理的高度。 現在我想將紋理置於0,0並重復它,直到它到達屏幕的右側(當然,它可能會被屏幕切斷一點,但這不是問題,我只是不想要質地得到拉伸)

我「已經」得到這個代碼到目前爲止

Texture spikesTex; 

spikesTex = new Texture("spike.png"); 
spikesTex.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat); 

然後,我已經嘗試過不同的參數

batch.draw(spikesTex, ...); 

但他們沒有真正解決。

回答

0

我試過用這種方法來完成你的要求。可能會幫助你。

public class MAIN extends ApplicationAdapter { 
    SpriteBatch batch; 
    Texture img; 
    TextureRegion textureRegion; 
    float drawingWidth,drawingHeight; 

    @Override 
    public void create() { 
     batch = new SpriteBatch(); 
     img = new Texture("spike.png"); 

     img.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.ClampToEdge); 
     int width=Gdx.graphics.getWidth(); 

     textureRegion=new TextureRegion(img,0,0,width,img.getHeight()); 

     drawingWidth=width; 
     drawingHeight=Gdx.graphics.getHeight()*.2f; 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.begin(); 
     batch.draw(textureRegion,0,0,drawingWidth,drawingHeight); 
     batch.end(); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     img.dispose(); 
    } 
} 
+0

您的解決方案並沒有完全做什麼,我想作爲drawingHeight與img.getHeight()相比更小,紋理仍然在x軸上伸展(或更好:在y軸上收縮)。 但是,當我將「寬度」乘以與「drawingHeight」相同的因子時,它就起作用。非常感謝! – alektron

0

沒有必要創建其他TextureRegion,可以得出重複本身的質感,獲得所需的寬度和高度PARAMS:

img = new Texture("spike.png"); 
    img.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat); 
    ... 
    int imgPosX = 0;// x position where to draw 
    int imgPosY = 0;// y position where to draw 
    batch.draw(img, imgPosX, imgPosY, 0, 0, drawingWidth, drawingHeight);