android – libgdx background图像更改

android – libgdx background图像更改,第1张

概述我想每隔x秒更改菜单的背景图像.我正在使用libGDX scene2D.ui来制作菜单. TestScreen类扩​​展了AbstractScreen,它是一个实现libGDX的Screen类的抽象类. 问题:通过堆栈上的Table对象将 Image加载到舞台后,将图像引用更改为不同的图像. Stage.draw()不关心它是否制作了原始图像的副本.我想将背景保持为Image类并通过stage.d 我想每隔x秒更改菜单的背景图像.我正在使用libGDX scene2D.ui来制作菜单. TestScreen类扩​​展了AbstractScreen,它是一个实现libGDX的Screen类的抽象类.
问题:通过堆栈上的table对象将 Image加载到舞台后,将图像引用更改为不同的图像. Stage.draw()不关心它是否制作了原始图像的副本.我想将背景保持为Image类并通过stage.draw()进行渲染.

更复杂的是,如果我在render()方法中将图像更改为另一个图像,则image.setVisible(false)也会停止工作.

public class TestScreen extends AbstractScreen {private Stage stage;private Image background;private boolean ChangeBackground = true;private final float refreshTime = 2.0f; // refresh to new image every 2 seconds.private float counter = refreshTime;public TestScreen(Game game) {    super(game);}@OverrIDepublic voID render(float deltaTime) {    Gdx.gl.glClearcolor(0.0f,0.0f,1.0f);    Gdx.gl.glClear(GL10.GL_color_BUFFER_BIT);    if(ChangeBackground){        counter -= deltaTime;        if(counter < 0){            counter = refreshTime;            // Assets class has the 12 images loaded as "Image" objects already.            // I simple want to change the reference to other (already loaded in memory images) ...            // and make stage render the new image.            background = Assets.instance.wallpapers[(int) (Math.random()*12)];  // The image should change.            //background.setVisible(false);        }    }    stage.act(deltaTime);    stage.draw();}@OverrIDepublic voID resize(int wIDth,int height) {    stage.setVIEwport(Constants.VIEWPORT_GUI_WIDTH,Constants.VIEWPORT_GUI_HEIGHT,false);}@OverrIDepublic voID show() {    stage = new Stage();    Gdx.input.setinputProcessor(stage);    makeStage();}@OverrIDepublic voID hIDe() {    stage.dispose();}@OverrIDepublic voID pause() {   }// Builds the Background later and adds it to a stage through a stack.// This is how it's done in my game. I made this test bench to demonstrate.private voID makeStage() {    table BackGroundLayer = new table();    background = Assets.instance.wallpapers[(int) (Math.random()*12)];    BackGroundLayer.add(background);    Stack layers = new Stack();    layers.setSize(800,480);    layers.add(BackGroundLayer);    stage.clear();    stage.addActor(layers);}

}

解决方法 Image是Actor的子类.主要区别在于,Image内部有Drawable.如果调用stage.draw()调用Image的draw(),则会绘制此Drawable.您可以使用setDrawable(Drawable param)更改Drawable,而不是更改Image.
什么是Drawable?它是实现Drawable接口的任何类,例如TextureRegionDrawable.如果您使用的是TextureRegions,则可以使用此构造函数:TextureRegionDrawable(TextureRegion region);.也许将背景图像存储在可绘制数组中会更好,这样每次设置新的Drawable时都不必调用construcor.示例代码:

TextureRegionDrawable[] images = new TextureRegionDrawable[12];for (int i = 0; i<12; i++) {    images[i] = new TextureRegionDrawable(Assets.instance.textureRegions[i]);}

然后在你的渲染中:

if(changeBackground) {   counter -= delta;   if (counter < 0) {       counter = refreshtime       background.setDrawable(images[(int)(Math.random()*12)]);   }}

这应该工作

总结

以上是内存溢出为你收集整理的android – libgdx background图像更改全部内容,希望文章能够帮你解决android – libgdx background图像更改所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1122194.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存