6

I want to make a level screen for my game similar to the ones in Angry Birds, Farm Tower, and Cut the Rope (the part where you select worlds, or the part similar to the gallery widget). I wanted to know what is the easiest way to attack this.

How would I modify the Gallery view to work for this?

divibisan
  • 11,659
  • 11
  • 40
  • 58

2 Answers2

4

Views form a hierarchy. Make a Gallery of GridView's.

If you wish to adapt the code from the Gallery tutorial, change the ImageView to LevelSetView, and create a LevelSetAdapter that extends BaseAdapter, and override its getView method. Here's a start.

public class HelloLevelsGalleryActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new LevelSetAdapter(this));
    }

To understand better adapters: reference or this video at 2 minutes.

Also, the question was asked here.

Community
  • 1
  • 1
galath
  • 5,717
  • 10
  • 29
  • 41
2

Here's an idea for making a level selector using the Gallery view.

Let's follow this example just so that you have a code base: http://developer.android.com/resources/tutorials/views/hello-gallery.html

So at the top you'll have your level screens. When a user clicks on it, this method is fired (taken straight from the example).

gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        startLevel(position);
    }
});

Maybe your startLevel will look something like this:

public void startLevel(int position){
    Resources res = getResources();
    String[] levels = res.getStringArray(R.array.level_classes);
    try{           
        Intent i = new Intent(this, Class.forName(levels[position]));
        startActivity(i);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

Again, a very basic example since I have no idea how you are storing your levels, if you're using a database or not, etc. Furthermore, your classes for each level will probably reside in different packages, (e.g. com.game.levelone, com.game.leveltwo) and you'll need to import the class packages so as not to get a ClassNotFoundException But this should get you started in the right direction.

Otra
  • 8,108
  • 3
  • 34
  • 49