Search code examples
libgdx

A screen full of actors or full of cells?


I'm new with libgdx (not new with java), and I try to do my first game...

Why I'm here is... I don't want at the end of my journey with this new game and found out I start with the wrong method or with the wrong object, all my life I learn by doing it (24 year doing that) and I'm too old to start again :O)

Pretty simple game, I have a screen full of cells (100x100 cell) and I drag and drop thing over those cell and upgrade them or delete them (so on...) Also I need to have a kind of popup when I drop the image over the cell or actor.

What i want to know is what is the best way to do that, I already try the libgdx examples with the drag and drop and hexes grid. the drag and drop is made with a stage and actors and the hexes grid is made only with a map and cells in it..

I think the best way is the actor thing with the stage, if this is the perfect way, do I use the same method to built it, I do a loop (like the cells) but I create actor or can I use the simple map thing and put actor into those cells ???

Also how do I process the drop on all the cells/actors, this one is bugging my head when I saw the drag and drop example.

Anyways a lot's of questions, but I just want a simple answer, which way I should continue my journey... with a stage full of actors or a map full of cells. And if you have a good example to show how to process the drop on a particular cells or actor you are welcome :O)

A big thanks from a old timer


Solution

  • If the game is 2d, the Actor approach is really the way to go. I have a hex based game where units are moved around and use the Actor class for the hexes and the units.

    Each Actor has a draw callback that is executed when the Actor has been added to the Stage and stage.draw is called in the Screen's render callback.

    Each actor can have an InputListener added to it which can handle touch events. To do this, you must first create an InputMultiplexer and add the current Stage using the .addProcessor() method. Then, Gdx needs to know about the new InputMultiplexer, so call Gdx.input.setInputProcessor(x) where x is the name of the InputMultiplexer variable.

    Now just add Actor classes to the stage and have each Actor call x.addListener(y) where y is the subclass of InputListener.

    In summary, my map class looks is a Screen sub-class and has:

    • A Stage class
    • An InputMultiplexer class to handle multiple events
    • A Group class that is added to the Stage and which I add Actor classes to
    • Lots of Actor classes that are added to the Group and each have an InputListener registered
    • The Screen had a render() callback where draw method of the Stage class is called
    • Don't forget an OrthographicCamera, to set its viewport and to call the .update() method

    Hope this helps!