i made a sprite but how do i set up the controls? (android)

ok so i made my sprite i have the texture atlas now how do i get some buttons on screen and set them up to control the character is there any examples out there

only game examples i found were ones that make a character move with arrow keys on the keyboard im trying to make a game for android so i need to have buttons on screen (like left and right arrows and jump)

i know this might seem like a dumb question but this is the newbie section :stuck_out_tongue:

Logic should be separate from the input.

Have booleans like movingLeft, movingUp etc. and move based on that.

Then, in the input class, have pressing the key change movingX to true and releasing changing it to false.

Second part: buttons on the screen.

I’ve never used LibGDX, so I don’t know how touchscreen input works.

However it works, if the button is touched, treat it like a key press, and use the above technique.

the part about the booleans i understand. i remember i did something like this in app inventor but as far as writing the code in java how do i set everything up

do i set up the textures of the buttons as variables or something and the character sprite is that just a bunch of if and if else statements?

You detect if the touch is within the button’s bounds, then you set the Boolean accordingly. The libGDx wiki is your friend here.


Gdx.input.isKeyPressed(Input.Keys.LEFT);
Gdx.input.isTouched();

ok so i looked at the wiki and nothing about setting the buttons bounds

I don’t know if scene2d works on android but you could try it out. Else, you will have to check it like clicking a button with your mouse. You can set up a rectangle with the button dimensions and check if the touch is within with the [icode]rect.contains(x,y);[/icode] method which return a Boolean.

Err scene2d works flawlessly on android. My MAGNET is full composed by scene2d. Yet it is not static, you can see I put some animation like move and ā€œtoggleā€ effect on it.

If by button bounds you mean button click, a quickest way is to register an InputListener to it and override the touchDown method. Make sure you use Stage-based on your screen for easier debug.

do i use scene2d.ui.Button or scene2d.ui.ImageButton … do i need to use an actor somewhere?

Button and ImageButton, man, those names are so cryptic. ::slight_smile:
Use whichever you need.

There is Button: You can include any kind of content into a button. I know two predefined versions of the inner content:

TextButton: The inner widget is a Label. There’s text on the Button.
ImageButton: The inner widget is an Image. There’s an image on the Button.

ImageButton? Button class sure has image through ButtonStyle.