简体   繁体   English

在libgdx中更改图像

[英]Changing images in libgdx

So im new with libGDX and im making a game where if you press the image it rotates, witch means it changes the images for another. 因此,我是libGDX的新手,并且正在制作一个游戏,在该游戏中,如果您按图像旋转,则女巫表示它将更改另一个图像。 But i dont know who to do it, and i would like some help. 但是我不知道该由谁来做,我想寻求帮助。

@Override
public void create () {

    spriteBatch = new SpriteBatch();
    font = new BitmapFont(true);
    camera = new OrthographicCamera();

    texture = new Texture(Gdx.files.internal("cable_side.png"));
    textureRegion = new TextureRegion(texture);
    textureRegion.flip(false, true);

    texture2 = new Texture(Gdx.files.internal("cable_l_1.png"));
    textureRegion2 = new TextureRegion(texture2);
    textureRegion2.flip(false, true);

    camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


}

@Override
public void render () {


    Gdx.gl.glClearColor(0.4f, 0.4f, 0.4f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.update();
    spriteBatch.setProjectionMatrix(camera.combined);

    spriteBatch.begin();
    //font.draw(spriteBatch, "Hacking Time", 0, 0);
    spriteBatch.draw(textureRegion2, 0, 0);
    spriteBatch.draw(textureRegion, 32, 0);
    spriteBatch.draw(textureRegion, 64, 0);
    spriteBatch.draw(textureRegion, 96, 0);
    spriteBatch.draw(textureRegion, 128, 0);
    spriteBatch.draw(textureRegion, 160, 0);
    spriteBatch.draw(textureRegion, 192, 0);
    spriteBatch.draw(textureRegion, 224, 0);
    spriteBatch.end();
    }

Here's what i have, the second textureRegion on the horizontal and i want to put it on the vertical, removing the previous one. 这就是我所拥有的,水平的第二个textureRegion,我想将其垂直放置,删除前一个。 Please help! 请帮忙!

There are lots of ways to do what you want so i'd like to advise you to try and read some of the documentation available at Libgdx also Games From Scratch have a couple of good tutorials. 有很多方法可以满足您的需求,因此我建议您尝试阅读Libgdx上的一些文档,《 Games Scratch》也提供了一些不错的教程。

But this isn't what you asked so... One of the most simple ways to accomplish what you want is using Stage and Actors . 但这不是您要的。。。实现目标的最简单方法之一就是使用Stage和Actors

Steps to do it: 步骤如下:

  1. Create a Scene; 创建一个场景;
  2. Associate the Gdx input processor with the stage; 将Gdx输入处理器与平台相关联;
  3. Create a Skin to store your UI resources; 创建皮肤来存储您的UI资源;
  4. Create ImageButtonStyle with the up parameter with your original image and cheked with the image you want to swap with; 创建带有原始图片的up参数的ImageButtonStyle,并使其与要交换的图片相吻合;
  5. Create ImageButtons with the previous ImageButtonStyle. 使用先前的ImageButtonStyle创建ImageButtons。

Here in another Stackoverflow Topic user DannyBit provides a code sniplet that does something similiar with a textbutton. 在这里的另一个Stackoverflow主题用户中,DannyBit提供了一个代码片段,该片段与textbutton相似。

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

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