2016-02-04 65 views
0

所以我有一個矩形,我想要改變它的精靈90度順時針而不改變它的位置。更改sprite在LIBGDX中的矩形

這裏就是我的了:

//sprites I want to use 
    Up = new Texture("left.png"); 
    Down = new Texture("right.png"); 
    Left = new Texture("down.png"); 
    Right = new Texture("up.png"); 

    //the Rectangle 
    square = new Rectangle(); 
    square.x = 630; 
    square.y = 720/2 - 32 /2; 
    square.width = 32; 
    square.height = 32; 

渲染()

batch.begin(); 
    batch.draw(Right, square.x, square.y); 
    batch.end(); 

等與我想改變矩形精靈與上面的精靈。 我會感謝一些幫助!

+0

SpriteBatch有一個方法用於繪製帶有旋轉的紋理或紋理區域(關於左下角)。因此,用旋轉角度和一些偏移量來繪製左下角應該保持所需位置的位置。 – Tenfour04

回答

0

它會幫助你實際創建你的紋理的精靈。例如:

Sprite upSprite = new Sprite(Up) 

精靈的原點將自動位於左下角,並將圍繞該點旋轉。要設置原點到精靈中間:

upSprite.setOriginCenter() 

然後以旋轉精靈90度,你可以這樣做:

upSprite.setRotation(90) 

現在你可以繪製相同的,但是我建議更新精靈位置:

upSprite.setPosition(square.x, square.y) 

這樣,你只需要做

upSprite.draw(batch) 

欲瞭解更多信息,請查閱Sprite documentation

+0

謝謝!!所以如果我想讓它在我觸摸屏幕時旋轉,我還需要添加什麼?它是這樣的: 'if(Gdx.input.isTouched()){ upSprite.setRotation(90); }' – gafflx

+0

查找touchDown或touchUp事件會比較容易,因此只能調用一次。看看https://github.com/libgdx/libgdx/wiki/Event-handling。 inputProcessor應該做你需要的東西 –

+0

yh我再次感謝它的工作!但是如果我觸摸它,它只會變成一次,我必須添加什麼才能讓它每次觸摸屏幕時都會變化? – gafflx