How to handle resume for libGDX Image with Pixmap texture

断了今生、忘了曾经 提交于 2020-01-04 04:57:06

问题


I have a com.badlogic.gdx.scenes.scene2d.ui.Image that is built from a Pixmap. The Pixmap is only one pixel because I'm using it to build an Image that functions as a background that can fade in and out.

Pixmap pmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pmap.setColor(1.0f, 1.0f, 1.0f, 1.0f);
pmap.drawPixel(0, 0);
bgImage = new Image(new Texture(pmap));
pmap.dispose();
bgImage.setSize(MyGame.VIRUAL_WIDTH, MyGame.VIRUAL_HEIGHT);
bgImage.getColor().a = 0.0f;
bgImage.addAction(Actions.sequence(Actions.fadeIn(1.0f),Actions.delay(3.0f),Actions.fadeOut(1.0f)));
stage.addActor(bgImage);

It works great but my concern is that the game might be paused while the actions are taking place. I assume the actions will continue upon resuming so I need to keep the same Image but the underlying Pixmap is not managed and therefore needs to be recreated upon resume. I'm having trouble figuring out how to reattach the Texture/Pixmap to the Image. Building the Pixmap itself is easy but getting an existing Image to use it is the problem.


回答1:


There is no setTexture method on an Image. Looking at the Image(Texture) constructor it wraps the texture in a drawable region:

this(new TextureRegionDrawable(new TextureRegion(texture)));

So you can do something similar and use the Image.setDrawable method to change the drawable object used. Something like this:

bgImage.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(pmap))));

I think you can probably reuse the TextureRegion and all of its containers (so after generating a new pixmap, just wrap it in a Texture and push that into the saved TextureRegion from before the pause.




回答2:


My solution was as follows

Assume Pixmap pixmap = ...

TextureData texData = new PixmapTextureData(pixmap, Format.RGBA8888, false, false, true);
Texture texture = new Texture(texData);
Image image = new Image(texture);

The last flag in PixmapTextureData is "managed". It runs on mine.

You can check out the first portion of the following link to see what is managed and what is not. http://www.badlogicgames.com/wordpress/?p=1513



来源:https://stackoverflow.com/questions/21522946/how-to-handle-resume-for-libgdx-image-with-pixmap-texture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!