我创建了以下自定义actor以在libgdx上绘制线条
public class LineWidget extends Actor implements Disposable {
private Position posA;
private Position posB;
public final Vector2 _A;
public final Vector2 _B;
private TextureRegion lineTexture;
private final TextureRegion lineTextureGreen = Assets.instance.Images.lineGreen;
private final TextureRegion lineTextureOrange = Assets.instance.Images.lineOrange;
private final TextureRegion lineTextureRed = Assets.instance.Images.lineRed;
public LineWidget(Position A, Position B) {
this._A = A.getPositionVector();
this._B = B.getPositionVector();
this.posA = A;
this.posB = B;
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
setLineColor(posA, posB);
float xdif = _B.x-_A.x;
float ydif = _B.y-_A.x;
float l2 = xdif*xdif+ydif*ydif;
float invl = (float)(1/Math.sqrt(l2));
//dif is vector with length linewidth from first to second vertex
xdif*=invl*5;
ydif*=invl*5;
float floatBits = batch.getColor().toFloatBits();
//draw quad with width of linewidth*2 through (x1, y1) and (x2, y2)
float[] verts = new float[]{_A.x+ydif, _A.y-xdif, floatBits, lineTexture.getU(), lineTexture.getV(),
_A.x-ydif, _A.y+xdif, floatBits, lineTexture.getU2(), lineTexture.getV(),
_B.x-ydif, _B.y+xdif, floatBits, lineTexture.getU2(), lineTexture.getV2(),
_B.x+ydif, _B.y-xdif, floatBits, lineTexture.getU(), lineTexture.getV2()};
batch.draw(lineTexture.getTexture(), verts, 0, 20);
}
@Override
public void setSize(float width, float height) {
super.setSize(width, height);
}
@Override
public void dispose() {
}
private void setLineColor(Position A, Position B) {
if(A.get_value() != null && B.get_value() != null) {
if(A.get_value() == 1 || B.get_value() == 1) {
lineTexture = lineTextureGreen;
return;
}else {
lineTexture = lineTextureRed;
}
} else {
lineTexture = lineTextureRed;
}
}
}
当我创建演员时,内存消耗不断上升,但我无法检测到这种泄漏发生的位置。如何修复此actor以阻止内存泄漏?
编辑:我在另一个扩展了Group Actor类的actor中绘制了这个actor。组actor类不泄漏内存,它现在唯一要做的就是通过调用addActor()来绘制LineWidget。然后将此actor添加到屏幕内的舞台。我不认为这些信息很重要,只是添加它以防万一。我测试了所有这些actor,当我绘制LineWidget Actor时,内存泄漏就开始了。