【发布时间】:2015-02-25 14:20:05
【问题描述】:
我有一个例程在渲染方法的每个循环中绘制一个 Sprite。 我的第一种方法是在每个循环中创建一个 Sprite,但这会导致垃圾,并且当 gc 发生时,它可能会影响游戏性能(FPS 从 60 变为 5* 或更差)。 因此,我在渲染循环期间删除了所有堆分配,但我找不到精灵的解决方案。 我尝试在类中声明一个 Sprite 对象(下面代码中的 currentSprite)并在构造函数期间对其进行初始化。然后在draw方法中为对应的图像索引(动画)设置TextureRegion。这没用。但是如果我在 draw 方法中实例化 currentSprite 它会(但会产生垃圾)。 奇怪....有什么想法吗? 提前谢谢你
JP
*private boolean draw(float stateTime) {
//Sprite sprite;
TextureRegion currentFrame;
float OriginX=0;
float OriginY=0;
WorldEntityEstate childEstate;
WorldEntityEstate currentEstate = this.estateHandler.getCurrentEstate();
if(currentEstate.anim==null) return(true);
Vector2 spritePos = this.body.getPosition();
//System.out.println("currentEstate.animFrameIndex = " + currentEstate.animFrameIndex);
currentFrame = currentEstate.anim.getKeyFrames()[currentEstate.animFrameIndex];
this.currentSprite = new Sprite(); // If I comment this out the Sprite is not drawn
this.currentSprite.setRegion(currentFrame);
this.currentSprite.setSize(currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
OriginX = this.currentSprite.getWidth()*this.bodyOrigin.x / (this.properties.worldScale * (this.currentSprite.getWidth()/ Definitions.PIXELS_PER_METER));
OriginY = this.currentSprite.getHeight()*this.bodyOrigin.y / (this.properties.worldScale * (this.currentSprite.getHeight()/ Definitions.PIXELS_PER_METER));
this.currentSprite.setOrigin(OriginX, OriginY);
this.currentSprite.setScale(1 / Definitions.PIXELS_PER_METER * this.properties.worldScale) ;
this.currentSprite.setRotation(this.body.getAngle()*MathUtils.radiansToDegrees);
this.currentSprite.translate(-1 * OriginX + spritePos.x + currentEstate.animOffsetX, -1 * OriginY + spritePos.y+currentEstate.animOffsetY);
this.checkBlink();
this.currentSprite.setColor(this.currentColorAmount);
this.currentSprite.draw(this.spriteBatch);
return(true);
}*
【问题讨论】:
标签: android garbage-collection libgdx