如果在libgdx中触摸图像,我该如何制作图像按钮并切换到另一个屏幕



这是我使用libgdx框架的第二个游戏应用程序,这是我第一次在Android中使用框架。我正在尝试了解如何使用它。我已经对主菜单中的设计完成了,我的问题是要切换屏幕的每个按钮的功能。任何链接教程或示例代码都非常感谢,谢谢并提前

这是我的代码

mainscreen.java

public class MainScreen  extends ApplicationAdapter implements Screen {
//Screen Size
private static final int WIDTH= 720;
private static final int HEIGHT= 1280;
Viewport viewport;
private Camera camera;
private Stage stage;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton playBtn;
private Texture Background,logo,exit,credits,help,option,play;
SpriteBatch spriteBatch;
Sprite sprite;
MyGdxGame game;
public MainScreen(final MyGdxGame game) {
    this.game = game;
    stage = new Stage(new ScreenViewport());
    Gdx.input.setInputProcessor(stage);
    spriteBatch = new SpriteBatch();
    camera = new PerspectiveCamera();
    viewport = new ScreenViewport(camera);
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Background = new Texture(Gdx.files.internal("backgroundimage.png")); //background image
    //Menu Buttons
    exit  =  new Texture(Gdx.files.internal("menu/exit.png"));
    logo =  new Texture(Gdx.files.internal("menu/logo.png"));
    option =  new Texture(Gdx.files.internal("menu/options.png"));
    help =  new Texture(Gdx.files.internal("menu/help.png"));
    credits =  new Texture(Gdx.files.internal("menu/credits.png"));
    play =  new Texture(Gdx.files.internal("menu/play.png"));
    sprite = new Sprite(play);
    sprite.setPosition(130,360);
    sprite.setSize(0,0);
    myTextureRegion = new TextureRegion(play);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    playBtn = new ImageButton(myTexRegionDrawable); //Set the button up
    stage.addActor(playBtn); //Add the button to the stage to perform rendering and take input.
    Gdx.input.setInputProcessor(stage);
    playBtn.addListener(new InputListener(){
        @Override
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Button Pressed");
            game.setScreen(new IngamedayOne());
        }
        @Override
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }
    });
    stage.addActor(playBtn);
}

@Override
public  void create(){
}
@Override
public void show() {
    Gdx.input.setInputProcessor(stage); //Start taking input from the ui
}
@Override
public void render(float delta) {
}
@Override
public void resize(int width, int height) {
//        viewport.update(width, height);
}
@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
    spriteBatch.begin();
    spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, WIDTH, HEIGHT);
    spriteBatch.draw(Background,0,0);
    spriteBatch.draw(exit,590,1140);
    spriteBatch.draw(logo,125,600);
    spriteBatch.draw(play,130,360);
    spriteBatch.draw(option,170,100);
    spriteBatch.draw(help,350,100);
    spriteBatch.draw(credits,470,100);
    stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
    spriteBatch.end();
    //stage.draw(); //Draw the ui
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
    Background.dispose();
    logo.dispose();
    exit.dispose();
}
}

我的第二个屏幕

 public class IngamedayOne extends ApplicationAdapter implements Screen {
// Constant rows and columns of the sprite sheet
private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
private boolean peripheralAvailable;
// Objects used
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
private Texture cat ,left_paw,right_paw,progressbar_background,progressbar_knob,pause,meter;
Texture carpet,desk,plants,square_carpet,shoes;
SpriteBatch spriteBatch;
Sprite sprite;
private  Texture Background;
Viewport viewport;
private Camera camera;
private Stage stage;
// A variable for tracking elapsed time for the animation
float stateTime;
//Screen Size
private static final int WIDTH= 720;
private static final int HEIGHT= 1280;
public IngamedayOne() {
}
public IngamedayOne(MyGdxGame game) {
}

@Override
public void create() {
    stage = new Stage();
    spriteBatch = new SpriteBatch();
    // Load the sprite sheet as a texture
    cat = new Texture(Gdx.files.internal("cat.png"));
    sprite = new Sprite(cat);
    sprite.setPosition(0,0);
    sprite.setSize(0,0);
    peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
    camera = new PerspectiveCamera();
    viewport = new ScreenViewport(camera);
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    //Display Items
    carpet = new Texture("equip/carpet2.png");
    desk = new Texture("equip/Desk.png");
    square_carpet = new Texture("equip/Carpet.png");
    plants = new Texture("equip/Plants.png");
    shoes = new Texture("equip/Shoes.png");
    // Progressbar
     progressbar_background = new Texture("progression_map.png");
     progressbar_knob = new Texture("cat_head.png");
    //pause
     pause = new Texture("pause.png");
     meter = new Texture("meter.png");
    //background
    Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
    //button controller
    left_paw = new Texture(Gdx.files.internal("left_paw.png"));
    sprite = new Sprite(left_paw);
    right_paw = new Texture(Gdx.files.internal("right_paw.png"));
    sprite = new Sprite(right_paw);

    // Use the split utility method to create a 2D array of TextureRegions. This is
    // possible because this sprite sheet contains frames of equal size and they are
    // all aligned.
    TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);
    // Place the regions into a 1D array in the correct order, starting from the top
    // left, going across first. The Animation constructor requires a 1D array.
    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
    int index = 0;
    for (int i = 0; i < FRAME_ROWS; i++) {
        for (int j = 0; j < FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
        }
    }
    // Initialize the Animation with the frame interval and array of frames
    walkAnimation = new Animation<TextureRegion>(0.200f, walkFrames);
    // Instantiate a SpriteBatch for drawing and reset the elapsed animation
    // time to 0
    spriteBatch = new SpriteBatch();
    stateTime = 0f;
}
@Override
public void show() {
}
@Override
public void render(float delta) {
}
@Override
public void resize(int width, int height) {
    viewport.update(width, height);
}
@Override
public void render() {
    // clear previous frame
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
    // Get current frame of animation for the current stateTime
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    spriteBatch.begin();
    spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, WIDTH, HEIGHT);
    spriteBatch.draw(Background,0,0);
    spriteBatch.draw(square_carpet,150,2,408,800);
    spriteBatch.draw(carpet,230,980,250,260);
    spriteBatch.draw(desk,10,1150,160,260);
    spriteBatch.draw(plants,500,700,200,260);
    spriteBatch.draw(shoes,300,500,110,110);
    spriteBatch.draw(meter,190,990);
    spriteBatch.draw(progressbar_background,20,1170);
    spriteBatch.draw(progressbar_knob,18,1170);
    spriteBatch.draw(pause,580,1150);
    spriteBatch.draw(left_paw,10,25);
    spriteBatch.draw(right_paw,517,25);
    spriteBatch.draw(currentFrame, 260, 120  ); // Draw current frame at (50, 50)
    spriteBatch.end();
    stage.act(); //acting a stage to calculate positions of actors etc
    stage.draw(); //drawing it to render all
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() { // SpriteBatches and Textures must always be disposed
    spriteBatch.dispose();
    cat.dispose();
    left_paw.dispose();
    right_paw.dispose();
    stage.dispose();
    Background.dispose();
    progressbar_background.dispose();
    progressbar_knob.dispose();

}

}

您的游戏应仅具有一个ApplicationListener(在创建应用程序时调用)。

让我们假设那是您的MainScreen,它扩展了ApplicationAdapter(ApplicationListener接口的适配器类),然后不再通过ApplicationAdapter再次扩展您的IngamedayOne类。您只需要使用Screen接口实现。

 public class IngamedayOne implements Screen {

 }

create()方法来自ApplicationListener接口,因此在create()中迁移您的代码到show()

看看这个Wiki以获取更多清关。
https://github.com/libgdx/libgdx/wiki/extering-the-simple-game

在然后使用setScreen()函数更改按钮上的屏幕addClickListener()

您可以在此处阅读更多有关它的信息:

https://github.com/libgdx/libgdx/wiki/scene2d

最新更新