如何从Blank Constructor中绘制LibGDX Sprite

时间:2013-02-10 22:49:49

标签: java android constructor sprite libgdx

我正在尝试在LibGDX中绘制Sprite。如果我使用指定要使用的纹理的构造函数,我可以这样做,例如

Sprite sprite = new Sprite(new Texture(Gdx.files.internal("path")));

但如果我改为使用Sprite();并尝试使用setTexture和/或setRegion,则不会绘制图片。 API表示在绘制任何内容之前需要设置“纹理,纹理区域,边界和颜色”。我已拨打setTexturesetRegionsetColor,但没有任何内容。

主要问题:如果我使用默认的Sprite()构造函数,之后我必须做些什么来确保它绘制到屏幕上(在SpriteBatch中)?

1 个答案:

答案 0 :(得分:3)

我认为代码需要执行与Sprite(Texture) ctor does完全相同的步骤:

public Sprite (Texture texture) {
    this(texture, 0, 0, texture.getWidth(), texture.getHeight());
}

public Sprite (Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) {
    if (texture == null) throw new IllegalArgumentException("texture cannot be null.");
    this.texture = texture;
    setRegion(srcX, srcY, srcWidth, srcHeight);
    setColor(1, 1, 1, 1);
    setSize(Math.abs(srcWidth), Math.abs(srcHeight));
    setOrigin(width / 2, height / 2);
}

这些都是公共方法。