Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let a user choose a picture from the gallery to use in game ,LibGDX

Tags:

android

libgdx

I'm a beginner learning LibGDX. I'm developing my first game in LibGDX where you can replace the enemy ball with selected picture (e.g. someones head) and then you dodge the enemy . In my mainmenu i have stage2d buttons and i want to have one button which lets a user choose the picture he wants to use in game. So i created a button and added a CLickListner to it. Now where do i go from here ? Is there a way to open the gallery in LibGDX and let the user choose a picture he wants to use ?

Edit :

So let's say I have an interface in my core project:

   public interface OpenGallery {
public void openGallery();
}

And in my android directory I create a Platform specific class that launches the intent

import android.content.Intent;

import com.kubas.zaidimas.OpenGallery;

public class GalleryOpener extends AndroidLauncher implements OpenGallery {

    private static final int SELECT_PICTURE = 1;

    @Override
    public void openGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);     
    }

What should i do from this point ? So I can call it inside my mainmenu ?

like image 573
user3802649 Avatar asked Jul 03 '14 17:07

user3802649


1 Answers

To call the the platform-scpefic object (galleryOpener) from other classes you need to pass a reference to it using a constructor or setter:

public class MyGdxApp extends Game {
        MainMenuScreen mainMenuScreen;

       @Override
        public void create() {
                mainMenuScreen = new MainMenuScreen(this);
                setScreen(mainMenuScreen);              
        }
} 

Then in your menu screen:

public class MainMenuScreen implements Screen {

   MyGdxApp app; 


   // constructor to keep a reference to the main Game class
    public MainMenuScreen(MyGdxApp app){
            this.app = app;
    }

    @Override
    public void render(float delta) {
            // update and draw stuff
         if (Gdx.input.justTouched()) 
             app.galleryOpener.getFilePath()
    }
    ...
like image 184
swbandit Avatar answered Nov 15 '22 13:11

swbandit