1
\$\begingroup\$

In my project I have my main character body. I want it to move down on the y-axis as long as the screen is pressed. I tried it with

public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        jetski.applyForce(new Vector2(0,-1000), jetski.getWorldCenter(), true);
        return true;
    }

but that does it only once.

\$\endgroup\$
1
\$\begingroup\$

Well for one this is in the touchUp method, which is called once when the screen is no longer being touched. A good way to do it would be to create a boolean "touchDown" that is accessible to the whole class and turn it to true in the touchDown method, and false in the touchUp method. Then you would apply the force to your jet ski while touchDown is true.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Currently developing a mobile game myself, using Box2D, I have no libgdx experience but for touch input on Android you can acquire a tap, a persistent touch, and a release!

public void gameControls(final RelativeLayout gameBackground)
    {
        // Touch input.
        // Set up the touch listener for touch input on the screen.
        // Use the whatever background you are using for displaying the game.
        // In my case, I use Relative background.
        gameBackground.setOnTouchListener(new View.OnTouchListener() 
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                int eventAction = event.getAction();

                switch (eventAction) 
                {
                    // If the player touches the screen.
                    case MotionEvent.ACTION_DOWN: 
                    {
                        //
                        // Whatever you want to happen on a TAP.
                        //

                        break;
                    }

                    // If the player holds down on the screen.
                    // The player can move the touch around while holding down.
                    case MotionEvent.ACTION_MOVE:
                    {
                        //
                        // Whatever you want to happen on a HOLD.
                        //

                        break;
                    }

                    // If the player releases the screen.
                    case MotionEvent.ACTION_UP:
                    {
                        //
                        // Whatever you want to happen on a RELEASE.
                        //

                        break;
                    }

                    // If none of the above cases are met.
                    default:
                    {
                        //
                        // Whatever you want to happen if none of the above are met, possibly nothing?
                        //

                        break;
                    }
                }
            }

Hope this helps! Don't hesitate to contact me/comment on this if you need any clarification or any more help.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.