Cyph3r__ Cyph3r__ - 7 months ago 25
Android Question

Clickable textures

I'm kind of new to LibGdx and android studio.
I'm trying to create clickable textures, one for play and one for credits.
Both should be opening a new empty screen/event.

public void create() {

batch = new SpriteBatch();
img = new Texture("Main_Screen.png");
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
credits = new Texture("credits.png");
play = new Texture("play.png");
}


@Override
public void render() {

Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(play, 340, 1400);
batch.draw(credits, 340, 400);
batch.end();
}


However i'm unsure on how to do this since i'm also creating the background with a texture, so i'd be very happy if someone could assist me with helping me out.

Answer Source

Use Sprite instead of Texture, that holds the geometry, color, and texture information for drawing 2D sprites.

MyGdxGame

public class MyGdxGame extends Game {

    public Screen menuScreen,creditsScreen,playScreen;

    @Override
    public void create () {

        menuScreen=new MenuScreen(this);
        creditsScreen=new CreditsScreen();
        playScreen=new PlayScreen();

        setScreen(menuScreen);
    }
}

MenuScreen

public class MenuScreen extends InputAdapter implements Screen  {

    SpriteBatch batch;
    Texture background,play,credits;

    Sprite backgoundSprite,playSprite,creditsSprite;
    private ExtendViewport extendViewport;

    OrthographicCamera cam;
    private float w=480;
    private float h=800;
    private Vector3 vector3;

    MyGdxGame game;
    Music music;

    public MenuScreen(MyGdxGame game){
        this.game=game;
    }

    @Override
    public void show() {

        batch = new SpriteBatch();
        cam = new OrthographicCamera();
        extendViewport=new ExtendViewport(w,h,cam);

        vector3=new Vector3();

        background = new Texture("Main_Screen.png");
        play=new Texture("play.png");
        credits=new Texture("credits.png");

        backgoundSprite=new Sprite(background);
        backgoundSprite.setSize(w,h);       // If resources are not in context of your viewport
        backgoundSprite.setPosition(0,0);  //Default Position

        playSprite=new Sprite(play);
        playSprite.setSize(100,100);
        playSprite.setPosition(w/2-playSprite.getWidth()/2,h/2+100);

        creditsSprite=new Sprite(credits);
        creditsSprite.setSize(100,100);
        creditsSprite.setPosition(w/2-creditsSprite.getWidth()/2,h/2-100);

        Gdx.input.setInputProcessor(this);

        music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
        music.play();
        music.setLooping(true);   
    }

    @Override
    public void render(float delta) {

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

        batch.setProjectionMatrix(cam.combined);
        batch.begin();
        backgoundSprite.draw(batch);
        playSprite.draw(batch);
        creditsSprite.draw(batch);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {

        extendViewport.update(width,height);
        cam.position.x = w /2;
        cam.position.y = h/2;
        cam.update();
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        vector3.set(screenX,screenY,0);
        Vector3 position=cam.unproject(vector3);
            if(playSprite.getBoundingRectangle().contains(position.x,position.y)) {
            game.setScreen(game.playScreen);
        }
        if(creditsSprite.getBoundingRectangle().contains(position.x,position.y)){
            game.setScreen(game.creditsScreen);
        }

        return super.touchDown(screenX, screenY, pointer, button);
    }

    @Override
    public void pause() { }

    @Override
    public void resume() { }

    @Override
    public void hide() { }

    @Override
    public void dispose() {
        batch.dispose();
        background.dispose();
        play.dispose();
        credits.dispose();
    }
}

CreditsScreen

public class CreditsScreen implements Screen {

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

    @Override
    public void resize(int width, int height) { }

    @Override
    public void pause() { }

    @Override
    public void resume() { }

    @Override
    public void hide() { }

    @Override
    public void dispose() { }
}

PlayScreen

public class PlayScreen implements Screen {

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    }

    @Override
    public void resize(int width, int height) { }

    @Override
    public void pause() { }

    @Override
    public void resume() { }

    @Override
    public void hide() { }

    @Override
    public void dispose() { }
}