2016-02-04 64 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