当我使用纹理的原始大小(256,256)在(0,0)处绘制精灵时,它可以正常工作,但是当我改变位置或大小时,精灵就会搞砸了。 这是代码:
private SpriteBatch batch;
private Sprite sprite;
private OrthographicCamera camera;
@Override
public void create () {
camera=new OrthographicCamera(600,600);
camera.position.set(0,0,0);
camera.update();
batch=new SpriteBatch();
sprite=new Sprite(new Texture("TestBlock_01.png"),0,0,256,256);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.setProjectionMatrix(camera.combined);
batch.draw(sprite,sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
batch.end();
}
但现在当我将位置和大小更改为(-100,-100,100,100)时:
我该如何解决?
答案 0 :(得分:0)
答案是替换
application.html.slim
与
sprite=new Sprite(Texture,x,y,width,height);
答案 1 :(得分:0)
我认为您必须将精灵原点设置为与缩放和位置变化完美配合。如上所述here
void setOrigin(float originX, float originY)
Sets the origin in relation to the sprite's position for scaling and rotation.
如果需要完美旋转,还可以使用参数originX,OriginY调用draw方法。如上所述here
void draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.