繁体   English   中英

LibGdx-Android-渲染期间的Sprite分配

[英]LibGdx - Android - Sprite allocation during render

我有一个例程在render方法的每个循环中绘制一个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);

} *

为什么需要精灵?

您只能使用TextureRegion进行旋转和缩放。

请参考:

https://github.com/libgdx/libgdx/wiki/2D-动画

https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites

draw(TextureRegion region,
     float x, float y,
     float originX, float originY,
     float width, float height,
     float scaleX, float scaleY,
     float rotation)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM