0
\$\begingroup\$

When I ran the android application, The sprite image will not move when I press the input keys. I followed a tutorial based on input keys and wish to know what's wrong. Can anyone see what's wrong? Not sure if its my android emulator.

EDIT: website where the tutorial is located http://www.gamefromscratch.com/post/2013/10/15/LibGDX-Tutorial-4-Handling-the-mouse-and-keyboard.aspx

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;

public class otherSide implements ApplicationListener
{
    private SpriteBatch batch;
    private TextureAtlas textureAtlas;
    private Texture texture;
    private Sprite sprite;
    private Animation animation;
    private float elapsedTime = 0;

    @Override
    public void create()
    {
        float h = Gdx.graphics.getHeight();
        float w = Gdx.graphics.getWidth();

        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("image.png"));

        sprite = new Sprite(texture);
        sprite.setPosition(w/2-sprite.getWidth()/2, h/2 - sprite.getHeight()/2);
        //textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheet.atlas"));
        //animation = new Animation(1/15f, textureAtlas.getRegions());
    }

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

    }

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

        if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
        {
            if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
            {
                sprite.translateX(-1f);
            }else
            {
                sprite.translateX(-10.0f);
            }
        {

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
        {
            if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT))
            {
                sprite.translateX(1f);
            }else
            {
                sprite.translateX(10.0f);
            }
        }

        batch.begin();
        //elapsedTime += Gdx.graphics.getDeltaTime();
        //batch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0);
        sprite.draw(batch);
        batch.end();

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose()
    {
        batch.dispose();
        texture.dispose();

    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Are you sure you want to handle key presses? Usually on an Android device you want to handle touch events. \$\endgroup\$
    – Xkynar
    Jul 19, 2014 at 1:27
  • \$\begingroup\$ @Xkynar: that makes sense. I'll try that right now and see if that fixes the problem. \$\endgroup\$ Jul 19, 2014 at 1:36

1 Answer 1

0
\$\begingroup\$

(Most) Android devices' input are from the touch screen. If that is your case, and you are trying to fetch touch events, you have two options:

1 - Poll every frame if the LEFT key is pressed. Basicly, instead of your

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

what you want to check is:

Gdx.input.isButtonPressed(Input.Buttons.LEFT)

From javadoc:

boolean com.badlogic.gdx.Input.isButtonPressed(int button)

Whether a given button is pressed or not. Button constants can be found in Buttons. On Android only the Button#LEFT constant is meaningful.

Parameters: button the button to check. Returns: whether the button is down or not.

Libgdx kinda sees touch as "mouse clicks". Its similar if you think about it. I will explain the only difference in point 2.

The problem with this method is, it is kinda hard to do more advanced stuff. I doubt your game's only input will be touching your screen. You might want to detect swipes, for example. You can always check if the button is down and if it is, place the position in an array, but still its kinda lame and troublesome compared to the next way.

Still, if your game's input consists only of touching your screen in different positions, you can use that method and you can get the position the cursor is on by:

Gdx.input.getX()
Gdx.input.getY()
(Screen origin is Top-Left corner)

2 - Overwrite event listener methods by extending InputAdapter.

In your case, your class would be:

public class otherSide extends InputAdapter implements ApplicationListener

You can check here which methods you can overwrite: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/InputAdapter.html

Example:

// Input Events
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
    //Do stuff
}

This method is usually better since you dont have to poll every frame if the button is pressed or not. When you touch the screen, you know that method will be called, and the parameters give you useful info about that touch.

Also, you can see an int "pointer" in the method. That is what distinguishes button clicks and android touches. That is what allows MultiTouch. Lets say you touch your screen with two fingers. Most likely, one of the finger's touch will have pointer value 0 and the second finger's touch will have pointer value 1. This way you can detect multiple touches and handle them in any way you wish. Of course, pointer is useless when you are making a Desktop game.

Ah, about the swipes, you can check here how it is done using InputAdapter: https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Finger-Swipe#Input

That is all i believe, have fun coding :)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .